Level 17
You are not returning anything from user() function in your Collection. Seems like you forgot to place return that I often do. :D
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello.
I try to build a relationship between my tables users (original from laravel) and collectionson Laravel 5.6.
Schema collections:
Schema::create('collections', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('location');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->timestamps();
});
The Collection model:
public function user() {
$this->belongsTo('\App\User');
}
The User model:
public function collection() {
$this->hasMany('\App\Collection');
}
I try to get the user->name using artisan tinker:
>>> $collection->user()->get();
PHP Error: Call to a member function get() on null on line 1
>>> $collection->user()->name;
PHP Notice: Trying to get property of non-object on line 1
>>> $collection->user->name;
LogicException with message 'App\Collection::user must return a relationship instance.'
Where is my mistake? Thanks!
You are not returning anything from user() function in your Collection. Seems like you forgot to place return that I often do. :D
Please or to participate in this conversation.