$protocols = Protocol::with('houses')->get();
Read this: https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to get the data for the administrative page to which house the protocol belongs to (on the protocols page). Created a function protocolMain
$protocols = Protocol::all();
$houses = $protocol->houses()->get;
return #code compact('protocols', 'houses');
Created a model in Protocol.php
public function houses()
{
return $this->HasMany(Houses::class);
}
Created a model in House.php
public function protocols()
{
return $this->belongsTo(Protocol::class);
}
But it displays an error
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::houses does not exist.
Where did I make the fatal mistake that led to the crash? The fact is that the table that I create has three fields (name, link, house_id), in the table house (house_number, street_id), in the table street (name) I wanted to display the following information in a table in a view / street, house / name of the protocol / actions with the file (buttons). But first I would like to get a list of houses to which the files belong
Please or to participate in this conversation.