Support the ongoing development of Laravel.io →
Eloquent

can someone help me convert this sql query in eloquent query

$q  =   'select c.id, c.created_at,  c.title, '.
	                  'p.name as place,	'.
                    'd.name as departament, '.
	                  'm.name as  mode, '.
	                  's.name as status '.
            'from calls c '.
            'inner join places p on c.place_id = p.id '.
            'inner join departaments d on p.departament_id = d.id '.
            'inner join callmodes m on c.mode_id = m.id '.
            'inner join ( '.
			                   'select call_id, status_id '.
			                   'from callhistories '.
			                   'group by call_id '.
			                  ') h on  h.call_id = c.id '.
            'inner join callstatuses s on s.id = h.status_id ';

I got up to the part below

$calls = \App\Call::join('places', 'places.id', '=', 'calls.place_id')
                  ->join('departaments', 'departaments.id', '=', 'places.departament_id')
                  ->join('callmodes', 'callmodes.id', '=', 'calls.mode_id')
                  ...
                  ->join('callstatuses', 'callstatuses.id', '=', '.status_id')
                  ->get('calls.is, calls.created_at, calls.title,
                         places.name as place,
                         departaments.name as departament,
                         callmodes.name as mode,
                         callstatuses.name as status');

Last updated 2 years ago.
0

http://laravel.com/docs/5.1/eloquent-relationships Define relationships in your models, e.g.

// App/Call
...
    function place() {
        return $this->belongsTo('App\Place');
    }

You should be able to end up with:

$calls = \App\Call::with([
     'places' => function ($query) { $query->select('id', 'name'); },
     'departments' => function ($query) { $query->select('id', 'name'); },
     'callStatuses' => function ($query) { $query->select('id', 'name'); },
     ...
])->get(['title', ...]);

$calls[0]->place->name;
$calls[0]->callStatus->name;
0

tanerkucukyuruk said:

http://laravel.com/docs/5.1/eloquent-relationships Define relationships in your models, e.g.

// App/Call
...
   function place() {
       return $this->belongsTo('App\Place');
   }

You should be able to end up with:

$calls = \App\Call::with([
    'places' => function ($query) { $query->select('id', 'name'); },
    'departments' => function ($query) { $query->select('id', 'name'); },
    'callStatuses' => function ($query) { $query->select('id', 'name'); },
    ...
])->get(['title', ...]);

$calls[0]->place->name;
$calls[0]->callStatus->name;

calls is related to places, and places is related to departaments.

with the work in this case?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.