Leoh
Leoh

Reputation: 670

Can I send some variable to the blade view laravel in an addColumn () function of datatable

Is it possible to send a variable to the blade view through the addColumn() function of datatable jquery?

I want to pass another variable to compare values ​​of the result of the query. this is the code:

 if ($request->ajax()) {

           
           
            $products = Product::with('categories', 'featureds')->orderBy('name', 'ASC');
            

            $selecteds = [];
            $products_selecteds = Product::all();
            
            foreach($products_selecteds as $products_selected){
                foreach($products_selected->featureds as $featured){
                    array_push($selecteds, $products_selected->id);
                }
                
            } 
            
            

            return Datatables::of($products)->addColumn('format_price', function($product) {
                return $product->format_price;
            })->addColumn('checkbox', function ($product, $selecteds) {
           
                return view('pages.products.components.table.checkbox', ['product' => $product, 'selecteds' => $selecteds]);
                
            });
}

the array $selecteds its i want to pass to the view checkbox:

<td>
    <label>
        
        <input type="checkbox" class="checkbox-item" {{ in_array($product->id, $selecteds) ? 'checked' : "" }} value=""/> 
        <span></span>
    </label>
</td>

Upvotes: 0

Views: 200

Answers (1)

mdzk
mdzk

Reputation: 66

Yes, it's possible to render view on additional columns. But to achieve that, you have to make the column as a Raw Column like this:

Datatable::of($products)
->addColumn('selected', function($product) use($selecteds) {
    return view('product.selected', compact('product', 'selecteds'));
})
->rawColumns(['selected'])

note: you also need to declare use keyword since $selecteds is outside the closure

Upvotes: 2

Related Questions