skip to Main Content

I have a problem with my laravel 8 project, everything in the foreach column raises an error when I test the order, but when I don’t order, the error doesn’t show up.

Controller

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 = CarbonCarbon::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'));
}

welcome.blade.php

<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">Nomer studio : {{ $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>

2

Answers


  1. Differentiate the array item name from the array name

    @foreach($studios as $studio)
    @if($studio->photo)
       <img src="{{ $studio->photo->getUrl() }}" class="card-img-top" alt="...">
    
    Login or Signup to reply.
  2. In your controller file, I think
    $studios = Studios::where('status',1)->get(); in this line $studios var getting a false value somehow, that’s why in your blade file you are getting an error that you want to read property called photo on that var but its already in the false state, it neither any object to read.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search