php – Laravel 5.1未定义的变量:评论
发布时间:2020-12-14 19:39:19 所属栏目:大数据 来源:网络整理
导读:我正在尝试构建一个评论系统,用户可以在用户项目帖子上留下评论. 我可以保存并显示项目,并在特定的项目页面上(/ projects / {id})我有一个表单,用户可以在其中留下评论.我可以将注释保存在数据库中,但是当我尝试显示注释时,我得到此错误未定义变量:comments
我正在尝试构建一个评论系统,用户可以在用户项目帖子上留下评论.
我可以保存并显示项目,并在特定的项目页面上(/ projects / {id})我有一个表单,用户可以在其中留下评论.我可以将注释保存在数据库中,但是当我尝试显示注释时,我得到此错误未定义变量:comments(查看:/var/www/resources/views/projects/show.blade.php). 我的文件: class Comment extends Model { //comments table in database protected $guarded = []; public function user() { return $this->belongsTo('AppUser'); } // returns post of any comment public function post() { return $this->belongsTo('AppProject','project_id'); } public $timestamps = false; } 项目模型: class Project extends Model { protected $fillable = [ 'user_id','title','tags','summary','file_name','published_at' ]; public function User() { return $this->belongsTo('AppUser'); } } 我的用户模型 class User extends Model implements AuthenticatableContract,AuthorizableContract,CanResetPasswordContract { use Authenticatable,Authorizable,CanResetPassword; /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['name','email','password']; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = ['password','remember_token']; public function Projects() { return $this->hasMany('AppProject'); } public function comments() { return $this->hasMany('AppComment'); } } 我的评论控制器 public function index() { $comments = Comment::all(); return view('projects.show',compact('comments')); } public function store() { $input = Request::all(); $comment = new Comment; $comment->body = $input['body']; $comment->on_projects = $input['project_id']; $comment->from_user = Auth::user()->id; $comment->save(); return redirect('projects/'.$input['project_id']); } 我的看法 @section('content') <a href="/projects">Terug naar alle projecten</a> <h1>Werkje: {{ $project->title }}</h1> <h3>Gemaakt door: <a href='/student/{{ $project->User->id }}'>{{ $project->User->name }}</a></h3> <img src="{{URL::to('/')}}/uploads/projects/{{ $project->file_name }}"> <h5>Tags: {{$project->tags}}</h5> <hr /> <h1>Reageer</h1> @if (Auth::check()) <article> <!--Add comment --> <br/> {!! Form::open() !!} {!! form::text('body',null,['class' => 'form-control']) !!} <br/> {!! Form::Submit('Reageer',['class' => 'btn btn-primary form-control']) !!} {!! Form::hidden('project_id',$project->id) !!} {!! Form::close() !!} <br/> </article> <article> @foreach ($comments as $comment) <article> <p>Body: {{ $comment->body }}</p> <p>{{ $comment->user->name }}</p> </article> @endforeach </article> @else <p>Gelieve in te loggen om te kunnen reageren.</p> @endif 我的路线: // Student routes REST methode Route::resource('student','StudentController'); Route::get('student/profile','StudentController@getProfile'); // add comment Route::post('projects/{id}','CommentController@store'); // show comments Route::get('projects/{id}','CommentController@index'); //Project routes REST methode Route::post('projects/store','ProjectsController@store'); Route::resource('projects','ProjectsController'); // Authentication routes Route::get('auth/login','AuthAuthController@getLogin'); Route::post('auth/login','AuthAuthController@postLogin'); Route::get('auth/logout','AuthAuthController@getLogout'); // Registration routes Route::get('auth/register','AuthAuthController@getRegister'); Route::post('auth/register','AuthAuthController@postRegister'); // Password reset link request routes Route::get('password/email','AuthPasswordController@getEmail'); Route::post('password/email','AuthPasswordController@postEmail'); // Password reset routes Route::get('password/reset/{token}','AuthPasswordController@getReset'); 我现在收到此错误: 解决方法
我的CommentController中的index()函数现在为空.
我在show {id}函数中查询了ProjectsController中的注释 public function show($id) { $input = Request::all(); $project = Project::all()->load("User"); $project_comments = DB::table('comments') ->select('body','name') ->where('on_projects','=',$id) ->join('users','users.id','comments.from_user') ->get(); return view('projects.show',['project' => Project::findOrFail($id),'comments' => $project_comments]); } 这就是我解决路线冲突的方法 // add comment Route::post('projects/{id}','CommentController@store'); // add project Route::post('projects/store','ProjectsController@store'); //Project routes REST methode Route::resource('projects','ProjectsController'); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |