1
我在laravel中定義了一個簡單的一對多關係。用戶擁有許多積分,而積分則屬於用戶。Laravel與學分名稱之間的一對多關係問題
用戶模型
/**
* Relationship to the users credits
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function credits()
{
return $this->hasMany(Credit::class);
}
誠信示範
/**
* Relationship to the user that this credit belongs to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
控制器
/**
* Return a user
*
* @param $user_id
*
* @return User
*/
public function getUser($user_id)
{
$user = $this->user->findOrFail($user_id);
// Dumping and Dieing works fine, but when i return
// the User instance i get an error.
// dd($user->toArray());
return $user;
}
錯誤,我越來越:
「錯誤」:0, 「消息」: 「Illumin類的對象ate \ Database \ Eloquent \ Collection無法轉換爲int「