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

nanadjei2's avatar

Eloquent or Query Builder Select all from multiple tables

I want to form a query where I can select from multiple tables with aliases; Likes so;

select all from 'configurations' as 'configs', 'shoes as shoes', 'categories as cats'.

Can anyone help me achieve this? Thank you.

0 likes
12 replies
tykus's avatar

Use an appropriate join.

https://laravel.com/docs/5.8/queries#joins

Aliases can be included in the query (even the redundant ones!), e.g.

DB::table('configurations as config')
    ->join('shoes as shoes', 'shoes.col1', '=', 'config.col1')
    ->//...
1 like
NOMGUY's avatar

Hey @nanadjei2 You can try like:

Configuration::selectRaw('configurations.* as configs', 'shoes.* as shoes', 'categories.* as cats')
                ->where('configurations.product_id', '$product->id')        // Please use your relation
                ->join('shoes', 'shoes.config_id', 'configurations.id')
                ->join('categories', 'categories.shoe_id', 'shoes.id')
                ->get();
1 like
nanadjei2's avatar

@tykus Config has no relationship with shoes. I just want separate tables with aliases no relationships needed.

1 like
NOMGUY's avatar
NOMGUY
Best Answer
Level 16

@nanadjei2 Then you must take them in different variables and merge at last. Like:

$configs = Configuration::all();
$shoes = Shoe::all();
$mix = array_merge($configs, $shoes);

$cats = Category::all();
$data = array_merge($mix, $cats);

Or you could try:

$dataArr = [];
$configs = Configuration::all();
$shoes = Shoe::all();
$cats = Category::all();

$dataArr = [
    'configs'=> $configs,
    'shoes' => $shoes,
    'cats'  => $cats
];

This one might help you achieve the solution.

2 likes
nanadjei2's avatar

@nomguy configurations have no relationship with shoes. I just want the explicit tables and give them my aliases that's all.

nanadjei2's avatar

I was thinking there was a way to do it with Query Builder in other to join tables all at once. Thank you.

tykus's avatar

I was thinking there was a way to do it with Query Builder in other to join tables all at once.

You can do it with the query builder:

DB::select('SELECT * FROM configurations as configs, shoes as shoes, categories as cats');

For my interest, what is the intended result of this query?

NOMGUY's avatar

Hey @tykus I believe what @nanadjei2 wanted to get was, if you have three tables with data. First Configuration table like:

'id' => 1,
'name'  => 'test'

Shoe table like:

'id'    => 1,
'name'  => 'dummy'

and Category table like:

'id'    => 1,
'name ' => 'demo'

All he wanted is data like:

[
    [configs]   => [
        [id]    => 1
        [name]  => test
    ],
    [shoes] => [
        [id]    => 1
        [name]  => dummy
    ],
    [cats]  => [
        [id]    => 1
        [name]  => demo
    ],
];

Something like this.

nanadjei2's avatar

DB::select('SELECT * FROM configurations as configs, shoes as shoes, categories as cats');

   Mix the results up and don't have a specified key to hook onto.
tykus's avatar

Well, that was my point, the pseudo-query in the first post produces exactly this result.

Please or to participate in this conversation.