我在我的數據庫中的這些關係laravel口才不行
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」,看看它是否有效。 – tompec