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

uloncl's avatar

eloquent model query in component class - getting: Cannot declare class App\View\Components\componentName, because the name is already in use

so i want to pass a value to a component and in the components class i want to do a query on a model depending on the value that was sent to the class and then have the results of the query sent to the component blade.php

0 likes
9 replies
uloncl's avatar

@Sinnbeck so im using tabs for the component so thats just this

<div class="tab-pane show active" id="no-filter-content" role="tabpanel" aria-labelledby="no-filter">
    <x-dashboard.bookingListTable filter="no-filter"/>
</div>
<div class="tab-pane" id="flagged-bookings-content" role="tabpanel" aria-labelledby="flagged-bookings">
    <x-dashboard.bookingListTable filter="flagged-filter"/>
</div>
<div class="tab-pane" id="incomplete-bookings-content" role="tabpanel" aria-labelledby="incomplete-bookings">
    <x-dashboard.bookingListTable filter="incomplete-filter"/>
</div>

the component class has the 2 variables and a switch statement for the queries

public $filter;
public $bookings;
public function __construct($filter)
{
    $this->bookings = "bookings";
    switch ($filter) {
        case "no-filter":
            $this->bookings = Booking::where(...)
            break;
        case "flagged-filter":
            $this->bookings = Booking::where(...)
            break;
        case "incomplete-filter":
            $this->bookings = Booking::where(...)
            break;
    }
    dd($this->bookings);
    $this->filter = $filter;
}
public function render()
{
    return view('components.bookingListTable');
}

then in the component blade it has a foreach loop to display it

uloncl's avatar

i think some of the problem was that i use dashes in the component names and the class name cant have dashes

uloncl's avatar

@Sinnbeck and its not because the component has already been loaded in the view, the error occurs the first time it is loaded, dd() in the class does noting, the class is not loaded once

uloncl's avatar

i guess ill just do all the queries in the controller, itll be slower but since that class isnt working i guess i wont use it

MohamedTammam's avatar

@nebulous75 The error might be due to a conflict with namespaces, please share the whole component class. And according to the docs you should call the component with kabab-case. I didn't try the way you do it before.

<x-dashboard.booking-list-table></x-dashboard.booking-list-table>
kokoshneta's avatar

@nebulous75 Why would doing the queries in the controller be slower?

As it is, you’re running three separate queries; doing the querying in the controller, you could bring it down to one – depending on the filtering logic, which you haven’t shown – and that will only make it faster.

For the sake of demonstration, I’ll assume that your ‘flagged’ and ‘incomplete’ filters just go by the value in some simple columns; probably more complex in your app, but the principle should still apply.

Controller

$bookings = Booking::where('...')->get();

return view('booking-view-name', [
	'bookings' => $bookings
];

Blade view

<div class="tab-pane show active" id="no-filter-content" role="tabpanel" aria-labelledby="no-filter">
    <x-dashboard.bookingListTable :bookings="$bookings" />
</div>

<div class="tab-pane" id="flagged-bookings-content" role="tabpanel" aria-labelledby="flagged-bookings">
    <x-dashboard.bookingListTable :bookings="$bookings->where('is_flagged', TRUE)" />
</div>

<div class="tab-pane" id="incomplete-bookings-content" role="tabpanel" aria-labelledby="incomplete-bookings">
    <x-dashboard.bookingListTable :bookings="$bookings->where('status', 'incomplete') />
</div>

If you don’t actually use the component class for anything more than you’ve shown here, you can just delete it altogether, but keep the Blade file. That will make it an anonymous component with no class definition needed at all, and you can still loop through the $bookings variable just as you do in the current Blade file.

uloncl's avatar

@MohamedTammam thats what it was before, i changed it back to that but i cant remember which error it gave me

Please or to participate in this conversation.