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

atlaz's avatar

Best practice for calling eloquent data inside view

Hi,

I recently upgraded to Laravel 5 from Laravel 4.

I've got three tables in my multilanguage app.

ORDER(id,shipping_id,language_id)

SHIPPING(id,number)

SHIPPING_LANGUAGE_DATA(id,shipping_id,language_id, number)

In laravel 4, i did this from inside the view (i know, extremely hackish)

<select id="shipping_id" name="shipping_id">
    @foreach(Shipping::all() as $shipping)
        <option value="{{$shipping->id}}" @if ($shipping->id == $order->shipping_id) selected = "selected"  @endif>
            {{$shipping->lang()->where('languages_id','=',$order->language)->first()->name}}
        </option>
    @endforeach
</select>

What is the best practice for me to do this in Laravel5? Should i create a two-level array of the Shippingmethods (in the Controller) and foreach them in the view?

0 likes
5 replies
bestmomo's avatar

There is no difference with Laravel 4 for this point, I think best practice is to elaborate all data in controller.

jekinney's avatar

Agreed, calling queries in your view is not the preferred way. Your view becomes reliant on your model. Prefered way would be calling a method on your controller that behind the scenes calls the query. And pass that variable to your view.

Doesn't have to, but updating can be a nightmare otherwise.

pmall's avatar

This is strange. The shipping is the same locale than the order so why didn't you directly link the shipping and the shipping lang at time of order ? Then just $shipping->lang->name.

atlaz's avatar

Thanks for your answers!

This will not work on L5, since it will die on the view not having Class Shipping (or Shipping_language_data when it comes to querying for the language data).

pmall: This is how i would like it to be done, but I have not yet figured out how to build the correct relationship for this to work. As for now my two models look like this:

Shipping model

namespace App\Models;
use DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Shipping extends Model{
    protected $guarded = []; 

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'SHIPPING';

    public function lang(){
        return $this->hasMany('Shipping_language_data','shipping_id','id');
    }
    public function shipping_costs(){
        return $this->hasMany('Shipping_costs');
    }
    public function allowed_payment(){
        return $this->hasMany('Shipping_allowed_payment');
    }

}

Shipping_language_data model


namespace App\Models;
use DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Shipping_language_data extends Model {
    protected $guarded = []; 
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'SHIPPING_LANGUAGE_DATA';



}

atlaz's avatar

Okay, I found the error. I had to specify in the Shipping-model

public function lang(){
        return $this->hasMany('App\Models\Shipping_language_data','shipping_id','id');
    }

Please or to participate in this conversation.