Eloquent join using "USING" clause with N query Eloquent join using "USING" clause with N query php php

Eloquent join using "USING" clause with N query


In Eloquent (and I think that was already available in 2018) the feature is not named using but with and should give something like :

    ForumActor::with(['film', 'actor'])->get();

Of course this has to be adapted to your cases, you may even nest relationships :

   ForumActor::with('actor.contacts')->get();

For instance.

Have a look : https://laravel.com/docs/8.x/eloquent-relationships#eager-loadingEven though it's labelled as "Eager Loading" (which is great btw) it also works without eager loading, and moreover, when foreign keys are properly set (e.g. with migrations), then it uses only ONE query, so that keeps away the N+1 Problem.


You can try find in code is possible to make USING JOIN, or add some proxy dictionary:

$kat_dict =  ["film_id" => "film.film_id", "title"=> 'title', "first_name" => 'first_name', "last_name" => 'last_name'];$kat = $kat_dict[$request->getParam("kat","first_name")];

BTW: better way is using function like Arr::get($arr, $index, $default) (See at code example)


You can simply call raw query via select

$query = "SELECT     film_id,title,first_name,last_name FROM     film_actor INNER join film USING(film_id)INNER join actor USING(actor_id)WHERE     film.film_id = :filmId ORDER BY film_actor.actor_idLIMIT 0, 100";$data = DB::select($query, [    'filmId' => 1]);// or like this, if not using default connection/**$data = DB::connection('test')->select($query, [    'filmId' => 1]);*/