Edit3:可能是因为两个控制器都指向同一个页面吗?
编辑2:在我得到答案后仍然不工作
编辑:错误解决了,现在我得到:
未定义变量:project(查看:/var/www/resources/views/pages/showProject.blade.php)
这可能是因为两个变量都指向同一个页面吗?项目变量在注释系统出现之前工作得很好。
public function index()
{
$projects = Project::all();
return view('pages.projects', compact('projects'));
}
项目变量声明。
我正试图从我的数据库中得到我的评论,以显示在我正在进行的laravel 5项目的特定"项目页面"上。这个想法是,用户可以添加艺术项目,其他用户可以评论它们,但每当我试图访问该页面时,我都会得到
未定义变量:comments(查看:/var/www/resources/views/pages/showProject.blade.php)
这是我的控制器
public function index()
{
$comments = Comment::all();
return view('pages.showProject', compact('comments'));
}
public function store()
{
$input = Request::all();
$comment = new Comment;
$comment->body = $input['body'];
$comment->project_id = $input['project_id'];
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect('projects/'.$input['project_id']);
}
这些是我的路线
// add comment
Route::post('projects/{id}','CommentController@store');
// show comments
Route::post('projects/{id}','CommentController@index');
And my view
@if (Auth::check())
<article> <!--Add comment -->
<br/>
{!! Form::open() !!}
{!! form::text('body', null, ['class' => 'form-control']) !!}
<br/>
{!! Form::Submit('Post Comment', ['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>Author: {{ $comment->user->name }}</p>
</article>
@endforeach
</article>
@else
<p>Please log in to comment</p>
@endif
模型class Comment extends Model
{
//comments table in database
protected $guarded = [];
// user who has commented
public function author()
{
return $this->belongsTo('App'User','user_id');
}
// returns post of any comment
public function post()
{
return $this->belongsTo('App'Project','project_id');
}
public function comments()
{
return $this->hasMany('App'Comment');
}
public $timestamps = false;
}
有什么办法可以解决这个问题吗?
Thanks in advance
首先,你需要确保你在控制器中混叠了你的'Comment'模型。这是用use
语句完成的。
use App'Comment;
class CommentController extends Controller
{
public function index()
{
$comments = Comment::all();
return view('pages.showProject', compact('comments'));
}
}
第二,您需要将显示注释的路由从POST请求更改为GET请求。目前,您正在制作相同的路由,而且GET是检索数据的正确请求类型。
Route::get('projects/{id}','CommentController@index');
第三,你在视图中引用了一个$project变量,但从来没有从控制器传递它。需要引用一些东西
我觉得你们的关系不对。试试这个:
评论模型:
class Comment extends Model
{
public function user()
{
return $this->belongsTo('App'User');
}
}
用户模式:class User extends Model
{
public function comments()
{
return $this->hasMany('App'Comment');
}
}
在视图中可以使用:
@foreach($comments as $comment)
<p>{{ $comment->user->name }}</p>
@endforeach
我需要清空commentsController中的show,并且只使用它来保存评论。为了显示它们,我在我的projectsController中添加了一个额外的函数。结果是这样的;
public function show($id)
{
$project = Project::findOrFail($id)->load("User");
$input = Request::all();
//-----------------------DB-----------------------------//
$project_comments = DB::table('comments')
->select('body', 'name')
->where('project_id', '=', $id)
->join('users', 'users.id', '=', 'user_id')
->get();
//-----------------------DB-----------------------------//
return view('pages.showProject', ['project' => Project::findOrFail($id), 'comments' => $project_comments]);
}