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

Jdubstep1357's avatar

Combing models into a query

I am trying to combine models into a single query.

I made a dependent dropdown upon click shows other tables. However, for whatever reason, after the page displays the other table, the only data being displayed is whatever data is inside the current model.

Here is what everything looks like BEFORE anything is pressed:

https://imgur.com/WO1aVyc

Here is my first backend query:

https://imgur.com/VifNZAq

Here is the result:

https://imgur.com/GvM5Sgx

If I switch out the models, the other table's data is displayed:

https://imgur.com/W2Vq0GM

https://imgur.com/5Um5SFw

Interestingly enough, if I enable BOTH queries, it takes the bottom-most query, as shown below:

https://imgur.com/YYZ8jwQ

https://imgur.com/thPma0T

Here is everything inside of the function, if it helps gather more information, as well as the blade file itself:

https://imgur.com/6PZGF90

https://imgur.com/EPXr57H

Is there any way to combine two models in a single query? Obviously, the joins for whatever reason are not holding up, and am wondering if am at the limits of what can be done with 3 levels of dependent dropdowns.

0 likes
22 replies
Jdubstep1357's avatar

@vincent15000 I believe I have to use only one instance of $this, as inside of my blade file, @foreach only supports on argument passed in.

I tried merging both of the queries as a separate variable, and got a Laravel error:

Querying collections with multiple model types is not supported.

Here is how I mered my queries.

https://imgur.com/18VAJez

1 like
vincent15000's avatar

@Jdubstep1357 To merge two collections, both collections have to contain items of exactly the same type.

What's the content of $a and $b ?

1 like
Jdubstep1357's avatar

@vincent15000 This should help clarify matters.

https://imgur.com/iNWSqx2

The first dropdown triggers what is displayed in the table below. If one clicks on the radio button that will then display the 3rd set of data at the bottom of the page.

My application works fine as a one-to-one dependant dropdown, as shown here:

https://imgur.com/q3ieJ0M

It works perfectly with one model.

The problem is, the second i click the radio button, only the data associated with the current model is shown in the first table. Meaning, for whatever reason, the joining attributes do not seem to work.

$a and $b are the same query, just flipped with their models alongside the first joined table referenced.

Let me know if this makes sense, or if you need further clarification.

1 like
vincent15000's avatar

@Jdubstep1357 To be sure.

Blue => you select an option and red appears.

Red => you click on a radio button and black appears.

The problem is that black displays only the data related to the blue or red, but you need that black displays the data related to blue and red together.

Is that ok ?

vincent15000's avatar

@Jdubstep1357 You say that both are the same query, but it's not true, because you don't get the same models in both collections.

Jdubstep1357's avatar

@vincent15000 No. The black data works fine.

The red table displays normally at first. After clicking on the radio button, it then does a query to display all of the black data based upon the id of the red table. But for whatever reason, on 'reload' or reset I believe, only the data associated from the referenced model gets displayed.

The reloaded query (from clicking on the radio button) for the red table seems to ignore the joins.

Hence why:

https://imgur.com/VifNZAq

https://imgur.com/GvM5Sgx

Only displays the Date data,

and

https://imgur.com/5Um5SFw

https://imgur.com/W2Vq0GM

Displays the Location data.

And the last question you posed is what I'm aiming for:

how to get the same model in both collections without any joins

1 like
Jdubstep1357's avatar

@vincent15000 Just to clarify, are the links Im posting working? I took screenshots of both my code and my screens

1 like
vincent15000's avatar

@Jdubstep1357 Ok I have understood the problem. The table isn't complete, the date data are missing on each line.

Anyway both classes are binded by a relationship. Why don't you use Eloquent to retrieve your datas ?

$datas = Class_DateModel::with('class_locations')->get();

Sure this won't work immediately because you don't use the Laravel naming conventions and I don't know the exact name of the relationship you have defined in the models.

But you have the idea.

1 like
Jdubstep1357's avatar

@vincent15000 I now get this error:

Call to undefined relationship [class_location] on model [App\Models\Class_DateModel].

I double checked, it should be the correct relationship.

I even tried doing it with the join included and got the same result.

1 like
Jdubstep1357's avatar

@vincent15000 Here is my code:

$value is the id from the dropdown above.

public function updatedCategory($value): void { $this->sizes = collect(); $this->reset('size', 'product');

    // Displays all data normally, but after click of radio button only class data
    $this->products = Class_DateModel::
        join('class_location', 'class_date_ID', '=', 'class_date.id')
        ->where('Course_ID', $value)->get();

        // SECOND MODEL - Ignore this for now
    // $this->products = Class_LocationModel::
    //     join('class_date', 'class_date_ID', '=', 'class_date.id')
    //     ->where('Course_ID', $value)->get();

} 

$this->products = is what happens to the value from the first dropdown.

I have 2 models in this relationship: Class_DateModel, which has a table of class_date Class_LocationModel, which has a table of class_location

Here is my code from the blade file:

            <div>
                <label class="block text-sm font-medium text-gray-700" for="category"> FLA </label>
                <select wire:model="category" name="category"
                x-on:change="show = !show" :aria-expanded="show ? 'true' : 'false'" :class="{ 'active': show }"    
                    class="rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" required>
                    <option value="">-- Pick a FLA --</option>
                    @foreach ($categories as $category)
                        <option value="{{ $category->id }}">{{ $category->FLA }}</option>
                    @endforeach
                </select>
            </div>
                <div>
                    <!-- TABLE 1 -->
                    <h1>Table</h1>
                    <table class="table-auto">
                        <thead>
                            <tr>
                                <th class="font-bold py-2 px-4 border-b border-l text-left"></th>
                                <th class="font-bold py-2 px-4 border-b border-l text-left">Date</th>
                                <th class="font-bold py-2 px-4 border-b border-l text-left">FLA</th>
                                <th class="font-bold py-2 px-4 border-b border-l text-left">Location Name</th>
                                <th class="font-bold py-2 px-4 border-b border-l text-left">Max Students</th>

                         </tr>
                        </thead>
                            <tbody>
                                <div wire:model="product" name="product">
                                

                                    @foreach ($products as $product)
                                        <tr>
                                            <td>
                                                <input type="radio" name="displayBtn" wire:click="display({{ $product->id }})" />
                                            </td>
                                            <td class="p-2 border-b border-l text-left" value="{{ $product->id }}">{{ $product->Date }}</td>
                                            <td class="p-2 border-b border-l text-left" value="{{ $product->id }}">{{ $product->FLA }}</td>
                                            <td class="p-2 border-b border-l text-left" value="{{ $product->id }}">{{ $product->Location_Name }}</td>
                                            <td class="p-2 border-b border-l text-left" value="{{ $product->id }}">{{ $product->Max_Students }}</td>

                                        </tr>
                                    
                                        @endforeach
                                </div>
                            </tbody>
                    </table>
                
                </div>
        </div>```

1 like
Jdubstep1357's avatar

@vincent15000 I just have a simple model with a $fillable key, and a $table. I was not aware that one could define relationships in models. I assumed that all done with eloquent, and on the spot.

Here is my Class_LocationModel:

class Class_LocationModel extends Model
{
    use HasFactory;
    protected $fillable = [];

    protected $table = "class_location";
}```

This could be a big reason as to why all of these hiccups are happening
1 like
vincent15000's avatar
Level 63

@Jdubstep1357 Ok ... well ... class_location is not the relationship but the table name.

I suggest you to follow some tutorials to discover Laravel from scratch. Here on Laracast you have a lot of great series.

https://laracasts.com/series/laravel-8-from-scratch

And sure you have the Laravel documentation.

https://laravel.com/docs/10.x

And the part which explains the relationships.

https://laravel.com/docs/10.x/eloquent-relationships

1 like
Snapey's avatar

You might get more support if you didn't post pictures.

1 like
Jdubstep1357's avatar

@Snapey I see. I didn't want to overwhlem people with lines and lines of code. But now that thinking upon it, it might be better to post snippets of code and if you need more lines i can post that as well

1 like

Please or to participate in this conversation.