使用这个简单的示例,我可以获取表“comments”中的列,但无法获取表“post”中的列。我知道我可以使用“Fluent join”,但它在 Eloquente 中可行吗?谢谢!
class Post extends Eloquent {
public function comments()
{
return $this->has_many('Comment');
}
}
$comments = Post::find(1)->comments()->get();
利用with()
Post::with('comments')->get()
您将得到一个comments带有所有posts 的对象。
你必须写这样的迁移
public function up()
{
Schema::table('comments', function($table){
$table->foreign('post_id')
->references('id')
->on('post')
->on_delete('cascade')
->on_update('cascade');
});
}
我相信它是不言自明的
这就是我要找的:(对不起,如果我的问题不清楚)
$data = Odt::find(1);
$post_date = $data->c_date;
$comments_arr = $data->comments;
$comment1_date = $comments_arr[0]->c_date;
我认为你需要以另一种方式来做。
您的评论模型:
class Comment extends Eloquent {
public function post()
{
return $this->belongs_to('Post');
}
然后在你的代码中你会想要类似的东西。
$comments = Comment::with('post')->where('post_id','=',$YourPostId)->get()
我相信这应该为您提供帖子的信息及其所有相关评论。