Michele Della Mea
Michele Della Mea

Reputation: 1002

Property [] does not exist on the Eloquent builder instance using one to many relationships with laravel

I am using Laravel 7. I have two tables: request_properties and requests. request_properties may have many requests (The FK in requests is request_properties_id). I would like to return all the id of the table requests starting from an id of the table Request_properties using one to many relationship in the model but I receive the following error:

Property [reqs] does not exist on the Eloquent builder instance.

This is the code in my controller:

return Request_property::where('id', 71979)->reqs->id;

These are the methods in my models:

Req.php

public function request_property()
    {
        return $this->belongsTo('App\Request_property', 'id', 'request_properties_id');
    }

Request_property.php

public function reqs()
    {
        return $this->hasMany('App\Req', 'request_properties_id', 'id');
    }

Upvotes: 0

Views: 847

Answers (1)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

use first()

Request_property::where('id', 71979)->first()->reqs()->pluck('id');

then use pluck() get all the id of reqs()

Upvotes: 1

Related Questions