jeudi 10 mars 2016

Laravel: Is it good practice to use model relationships within a view?

I have a Laravel 5 view and I was wondering whether using model relationships inside of the views is actually a good thing to do, regarding perfomance.

Let's say a Car model has a one-to-one relationship with a Windshield.

<div class="col-lg-4">
    <div class="form-group">
        <label class="col-lg-4 control-label">Windshield Model</label>
        <div class="col-lg-8">
            <p class="form-control-static">{{$car->windshield->model}}</p>
        </div>                        
    </div>                        
</div>                                 
<div class="col-lg-4">
    <div class="form-group">
        <label class="col-lg-4 control-label">Windshield Height</label>
        <div class="col-lg-8">
            <p class="form-control-static">{{$car->windshield->height}}</p>
        </div>                        
    </div>                        
</div>    

My question here is, everytime I do a $car->windshield->someProperty Eloquent performs a query. Is this a good thing to do? I mean, what about performance? If I want to show the n properties of the car's windshield, Eloquent will perform N queries, right?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire