【发布时间】:2014-11-24 20:04:53
【问题描述】:
我 2 天前开始学习。我做了一个简单的用户登录和博客发布系统。
这是我的代码,它将获取所有博客文章,但也应该将表格连接在一起
public function index()
{
$blog = new Blog;
$view = View::make('blogs.index');
$view->posts = $blog->get()->user;
return $view;
}
这是对的吗?查看文档和搜索,似乎是对的,但我不断收到以下错误:
未定义属性:Illuminate\Database\Eloquent\Collection::$user
我的博客表结构如下:
像这样的用户表
博客模型
class Blog extends Eloquent {
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
}
用户模型
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function blog()
{
return $this->hasMany('Blog', 'id', 'user_id');
}
}
【问题讨论】: