【发布时间】:2017-07-28 15:55:12
【问题描述】:
我的数据库中有这些关系
Schema::create('my_users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('fullName');
$table->string('userName');
$table->string('password');
$table->string('photo');
$table->string('email');
});
和
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('photo');
$table->text('caption');
$table->integer('like');
$table->integer('my_user_id');
});
我的模型:
class myUser extends Model
{
public function post()
{
return $this->hasMany('App\post');
}
}
class post extends Model
{
public function user()
{
return $this->belongsTo('App\myUser');
}
}
现在我正在尝试使用以下代码获取用户的数据:
$post = post::find(1);
$user = $post->user;
但它不起作用并且 $user 为 NULL。 我确定在帖子表中有一条 id 为 1 的记录。 奇怪的是,当我像这样更改方法的名称时,我没有收到任何错误:
$post = post::find(1);
$user = $post->somethingBullshit;
我使用的是 laravel 5.3。
请帮忙。
【问题讨论】:
-
我不确定,但我想我已经遇到了这个问题。我认为这是因为您的“帖子”课程的名称。尝试将模型和表重命名为“myPost”之类的名称,看看它是否有效。
标签: php laravel-5 eloquent relationship