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

ilhm's avatar
Level 1

Attempt to read property "photo" on bool

hi i have problem with my laravel 8 project ,everything in the foreach column raises an error when I test the order, the error will appear but when I don't order the error doesn't appear

this my controller

public $sources = [
    [
        'model'      => Booking::class,
        'date_field' => 'time_from',
        'date_field_to' => 'time_to',
        'field'      => 'user_id',
        'names'      => 'studios_id',
        'prefix'     => '',
        'suffix'     => '',
    ],
];

public function index(Request $request){

    $bookings = [];
    $studios = Studios::where('status',1)->get();

    foreach ($this->sources as $source) {
        $models = $source['model']::where('status', '0')
            ->get();
         foreach ($models as $model) {
            $crudFieldValue = $model->getOriginal($source['date_field']);
            $crudFieldValueTo = $model->getOriginal($source['date_field_to']);
            $studios = Studios::findOrFail($model->getOriginal($source['names']));
            $user = User::findOrFail( $model->getOriginal($source['field']));
            $timeBreak = \Carbon\Carbon::parse($crudFieldValueTo)->format('H:i');
    
            if (!$crudFieldValue && $crudFieldValueTo) {
                continue;
            }

            $bookings[] = [
                'title' => trim($source['prefix'] . "($studios->names)" . $user->name
                    . " " ). " " . $timeBreak,
                'start' => $crudFieldValue,
                'end' => $crudFieldValueTo,
            ];
        }
    }

    return view('welcome', compact('studios', 'bookings'));
}

and this is my blade and sorry if my welcome.blade is a mess I don't know why if I don't use '' then the code doesn't appear

                <div class="row">
                    @foreach($studios as $studios)      
                        <div class="col-lg-4 mb-5">
                            <div class="card" style="width: 18rem;">
                            @if($studios->photo)
                                <img src="{{ $studios->photo->getUrl() }}" class="card-img-top" alt="...">
                            @endif
                                <div class="card-body">
                                    <h5 class="card-title">{{ $studios->names }}</h5>
                                    <p class="card-text">Harga : Rp{{ number_format($studios->price,2,',','.') }} / Jam</p>
                                    <p class="card-text"> Rp{{ number_format($studios->org,2,',','.') }} / Orang</p>
                                    <a href="{{ route('booking', ['studio' => $studios->names])  }}" class="btn btn-primary">Booking</a>
                                </div>
                            </div>
                        </div>
                    @endforeach 
                </div>
            </div>```
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

format code here with three backticks ``` on their own line before and after the code blocks

Have you installed Laravel debugbar? I'm concerned how many database queries you have inside a foreach loop.

You are continually overwriting $studios inside your foreach loop. Put this line after the foreach loop;

   $studios = Studios::where('status',1)->get();
ilhm's avatar
Level 1

@Snapey thanks it work:) and i have revised the code above using ``` so people can understand it

Please or to participate in this conversation.