Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

YuraLons's avatar

Doesn't list streets and houses

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

0 likes
11 replies
tykus's avatar

The Collection returned by $protocols = Protocol::all(); does not have a houses method. You can eager-load the houses relation so that every Protocol instance will have its houses relation loaded:

$protocols =  Protocol::with('houses')->get();

Then you can iterate:

foreach ($protocols as $protocol) {
	foreach ($protocol->houses as $house) {
		// do something with $house
	}
}
YuraLons's avatar
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'house.protocol_id' in 'where clause' (SQL: select * from `house` where `house`.`protocol_id` in (16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
<tr class="h-10">
                    @foreach($protocols->houses as $house)
                        <td>{{$house->home_number}}</td>
                    @endforeach
                    <td width="60%" class="text-center">{{$protocol->protocol_name}}</td>
YuraLons's avatar

As I understand it, he is trying to find the protocol column in the table with houses.

And I don't have such a field there, that's why it gives an error.

table house

|   id   |   home_number   | other |

table Protocols

|   id   |   protocol_name   |   protocol_id   |   house_id   |

I have tables made this way When filling out the protocols, the house id is selected, and inserted into the house_id cell

tykus's avatar

Then the relations you have defined are wrong. A Protocol belongsTo a House, a House hasMany/hasOne Protocol(s)

YuraLons's avatar

I changed model protocol return $this->belongsTo(Houses::class); and model House return $this->hasMany(Protocol::class); In the controller indicated

$protocol = Protocol::with('houses')->get();

In the view indicated

@foreach($protocols as $protocol)
   <tr class="h-10">
@foreach($protocols->houses as $house)
   <td>{{$house->home_number}}</td>
@endforeach
   <td width="60%" class="text-center">{{$protocol->protocol_name}}</td>

And got a new error

ErrorException
Attempt to read property "home_number" on bool (View: D:\loc\testes\resources\views\dashboard\shouse\protocol\main.blade.php)
tykus's avatar

There is only one House... it’s a belongsTo relationship. So no nested foreach in the view template.

YuraLons's avatar

I'm confused. Model Houses

<?php

namespace App\Models;

use App\Models\Fillable\Report;
use App\Models\Fillable\Dogovor;
use App\Models\Fillable\Protocol;
use App\Models\Fillable\Passport;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class Houses extends Model implements HasMedia
{
    use HasFactory, InteractsWithMedia;

    protected $table = 'house';

    protected $fillable = [
        'home_number',
        'street_id',
        'levels',
        'apps',
        's_nofull',
        's_life',
        's_full',
        'status',
        'year',
    ];

    public function street()
    {
        return $this->belongsTo(Street::class);
    }
    public function dogovor()
    {
        return $this->belongsTo(Dogovor::class);
    }
    public function protocols()
    {
        return $this->hasMany(Protocol::class);
    }

    public function registerMediaConversions(Media $media = null): void
    {
        $this->addMediaConversion('thumb')
            ->greyscale();
        $this->addMediaConversion('optimized');
        $this->addMediaConversion('non-optimized')
            ->nonOptimized();
    }

    public function getOtchetsForHouse()
    {
        return $this->hasMany(Otchet::class);
    }
    public function getDogovorsForHouse()
    {
        return $this->hasMany(Dogovor::class);
    }
    public function getProtocolsForHouse()
    {
        return $this->hasMany(Protocol::class);
    }
    public function getPassportsForHouse()
    {
        return $this->hasMany(Passport::class);
    }
}

Model Protocols

<?php

namespace App\Models\Fillable;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Houses;

class Protocol extends Model
{
    use HasFactory;

    protected $table = 'protocols';

    protected $fillable = [
        'protocol_name',
        'protocol_link',
        'houses_id'
    ];


    public function house()
{
    return $this->belongsTo(Houses::class);
}
}

Controller Apps@protocolMain

        $protocols = Protocol::with('houses')->get();
        return view('dashboard.shouse.protocol.main', compact('protocols'));

View Main@protocolMain

@foreach($protocols as $protocol)
                <tr class="h-10">
                    @foreach($protocol->houses as $house)
                        <td>{{$house->home_number}}</td>
                    @endforeach
                    <td width="60%" class="text-center">{{$protocol->protocol_name}}</td>
                    <td>
                        <a class="rounded py-1 px-2 bg-green-600 text-white" href="#">Изменить</a>
                        <a class="rounded py-1 px-2 bg-red-600 text-white" href="#">Удалить</a>
                    </td>
                </tr>
            @endforeach
tykus's avatar

When you are coming from the Protocol model, there is only one House, so the relationship should be singular (house), and in the view:

@foreach($protocols as $protocol)
	<tr class="h-10">
		<td>{{ optional($protocol->house)->home_number }}</td>
		<td width="60%" class="text-center">{{$protocol->protocol_name}}</td>
		<td>
			<a class="rounded py-1 px-2 bg-green-600 text-white" href="#">Изменить</a>
			<a class="rounded py-1 px-2 bg-red-600 text-white" href="#">Удалить</a>
		</td>
	</tr>
@endforeach
YuraLons's avatar

I did, but it does not display the name, there are no errors


    1 => App\Models\Fillable\Protocol {#1667 ▼
      #table: "protocols"
      #fillable: array:3 [ …3]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:6 [ …6]
      #original: array:6 [ …6]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [ …1]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [ …1]
    }
tykus's avatar

Which name; the protocol_name property?

Please or to participate in this conversation.