Yes it is possible but not recommended. Try to keep your queries in the controllers or in your models.
I suggest you take a look at this free series to learn the basics of Laravel
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
Please or to participate in this conversation.