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

DADOMULTIMEDIA's avatar

Create a Eloquent query in a blade component

Hi everyone. I would like to know if it could be possible to make/create a query (either eloquent/DB) inside a blade component. For example:

public $categories = []; public $promosaleid;

public function __construct($promosaleid)
{
    $this->promosaleid = $promosaleid;

    $this->categories = Category::query()->with(array('products' => function($query) use($this->promosaleid){
        $query->where('promosale_id','<>',$this->promosaleid)->where('product_status','1')->where('stock_status','1');
    }))->select('id','name','slug')->take(5)->get();
}

public function render()
{

    return view('components.frontend.categories-product');
}

}

In the constructor, I´m getting the variable $promosaleid, then I´m creating a query with the variable $categories. Locally, works, but not for production... Using php 7.4 / Laravel 8 Thank you for any advice.

0 likes
6 replies
aschmelyun's avatar

@dadomultimedia Not sure I'm following 100% what you're trying to do, the first part of your question makes it seem like you want to include a DB call inside of a Blade component, in which case @tray2 's answer is correct.

But at the end of your question it looks like you're trying to access these two public properties you've created inside of your Blade component, but it's not working on production.

In that case, try to explicitly include them in the view() method like this:

return view('components.frontend.categories-product', [
    'promosaleid' => $this->promosaleid,
	'categories' => $this->categories
];

And see if that shows any difference!

DADOMULTIMEDIA's avatar

Thank you so much for read my post, I´m so sorry that I couldn´t explain well, (It´s my first post.) @tray2 thank your for your advise, seems to be that many procedures and details I could be forgetting.

I will explain better what I´m trying to do:

I have a blade page (Home) with the controller. in the home page, I placed a x-blade component called "categories-product" that shows products by category that are not part of those products with a special offer.

<!-- PRODUCTS BYCATEGORIES -->
    <x-frontend.categories-product :promosaleid="$promosale_id"/> 

This component is receiving just the variable $promosaleid. In the constructor of the x-blade component I´m receiving that variable.

class CategoriesProduct extends Component { public $categories = []; public $promosaleid;

public function __construct($promosaleid)
{
    $this->promosaleid = $promosaleid;

    $this->categories = Category::query()->with(array('products' => function($query) use($promosaleid){
        $query->where('promosale_id','<>',$promosaleid)->where('product_status','1')->where('stock_status','1');
    }))->select('id','name','slug')->take(5)->get();
}
public function render()
{
    return view('components.frontend.categories-product');
}

}

And then, I´m creating the query to get those products in the view having a foreach routine:

@foreach($categories as $category)
        <section class="text-center ">
            <!-- BAR TITLE CATEGORIES -->
            <div class="flex items-center justify-center p-3 text-primary-content bg-primary-focus">
                    <h1 class="text-xl tracking-wide uppercase uk-light font-quicksand">{{ $category->name }} </h1>

---- etc.etc.etc

In theory (for me, hehehe) must work, locally it´s working, but I realized that in the server it´s not the same. That´s why my question, if it could be possible to be done in this way. passing variable works, but to create the query in the logic, it´s not working...

Thank you guys for your feedback. @tray2 @aschmelyun Greetings.! :)

RayC's avatar

You could move the query:

public function __construct($promosaleid)
{
    $this->promosaleid = $promosaleid;
}
public function render()
{
 $this->categories = Category::query()->with(array('products' => function($query) use($promosaleid){
        $query->where('promosale_id','<>',$this->promosaleid)->where('product_status','1')->where('stock_status','1');
    }))->select('id','name','slug')->take(5)->get();

    return view('components.frontend.categories-product', [
            'categories' => $this->categories,
        ]);
}

Should send the categories to your view.

davidebo's avatar

i have tried like this

use App\Models\Post;

class aside extends Component { /** * Create a new component instance. * * @return void */ public $categories; public $tags; public $posts;

public function __construct($categories,  $tags, $posts)
{
    $this->categories = $categories;
    $this->tags = $tags;
    $this->query();
}

public function query(){
    $this->posts = Post::inRandomOrder()->get();
    return $this->posts;
}

public function render()
{
    return view('components.aside');
}

}

Please or to participate in this conversation.