Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get access to collection laravel

I am a new learner of the laravel framework working on 3 models
customer,
accidents,
License
where the relations are as follows:

public  function License()
        {
            return $this->hasOne(license::class,'driver_id');
        }

    public function cars()
        {
            return $this->hasMany(car::class);
        }

In the controller I do like this:

public function AdminCustomers()
    {
        $customers = Customer::with('cars')->with('License')->get();
        return view('admin.AdminCustomers',compact('customers'));
    }

now I want to display all 3 models’ data on view.blade page

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<tbody>
                    @foreach($customers as $customer)

                                    <tr>
                                        <td>{{$customer->id}}</td>
                                        <td>{{$customer->name}}</td>
                                        <td>{{$customer->adderss}}</td>
                                        <td>{{$customer->mobileNo}}</td>
                                        <th>{{$customer->License->id}}</th>
                                        <th>{{$customer->License->Exp}}</th>
                                        <td>{{$customer->cars->id}}</td>
                                        <td>{{$customer->cars->color}}</td>
                                        <td>{{$customer->cars->model_no}}</td>
                                        <td>{{$customer->cars->company}}</td>
                                    </tr>
                               
                    @endforeach
                    </tbody>

But it doesn’t work and I didn’t know where is the problem
the error Exception

Property [id] does not exist on this collection instance.

>Solution :

$customer->cars will return a collection as the relationship is hasMany so you need to loop over the collection in the view

@foreach($customers as $customer)

    <tr>
        <td>{{$customer->id}}</td>
        <td>{{$customer->name}}</td>
        <td>{{$customer->adderss}}</td>
        <td>{{$customer->mobileNo}}</td>
        <th>{{$customer->License->id}}</th>
        <th>{{$customer->License->Exp}}</th>
            @foreach($customer->cars as $car)
                <td>{{$car->id}}</td>
                <td>{{$car->color}}</td>
                <td>{{$car->model_no}}</td>
                <td>{{$car->company}}</td>
            @endforeach
    </tr>
                               
@endforeach
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading