/noquestionfound
Display DataTable with other data
I am going to display DataTable. My controller code for DataTable is like below,
$data = Student::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
But I have to show results of below data also in View file.
$product = Product::where('product_id',$id)->with('product_log')->first();
Thanks @laracoft . This is my controller function.
public function getProduct(Request $request,$id) {
$product = Product::where('product_id',$id)->with('product_log')->first();
$product_log_views = Product_log_view::where('sku',$product->sku)->paginate(5);
return view('welcome.product-details', ['product_log_views' => $product_log_views, 'product' => $product, 'active_menu' => 'products']);
}
I would like to display data of below portion using DataTables in blade file. (https://github.com/yajra/laravel-datatables). How can I do that ?
$product_log_views = Product_log_view::where('sku',$product->sku)->paginate(5);
Thanks.
Hello @laracoft . Are you there ?
@afoysal yes 1 moment
@afoysal does $product_log_views = Product_log_view::where('sku',$product->sku)->paginate(5); work already?
If you dd($product_log_views), is there correct data?
- Have you followed the quick starter here? https://yajrabox.com/docs/laravel-datatables/master/quick-starter
- You are not supposed to return a
view('welcome.product-details'...)(Unless you know how to write the entire HTML) - Is
$product_log_viewsa datatable class? - All the code is there in the link, but the important part is to start here
Route::get('/users', 'UsersController@index')->name('users.index');
...
class UsersController extends Controller
{
public function index(UsersDataTable $dataTable)
{
return $dataTable->render('users.index'); // not using view()
}
}
Thanks @laracoft for your reply. But may be you didn't get my issue. I searched a lot in Google but didn't get any solution regarding my issue. Actually I would like to send 2 types of data to blade file. One is DataTable and another is without DataTable. I am writing controller function again for you.
public function getProduct($id) {
$product = Product::where('product_id',$id)->with('product_log')->first(); //If I use DataTable how can I send this $product data
$data = Product_log_view::where('sku',$product->sku)->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
Hello @laracoft . Are you there ?
Thanks @laracoft for your reply. But may be you didn't get my issue. I searched a lot in Google but didn't get any solution regarding my issue. Actually I would like to send 2 types of data to blade file. One is DataTable and another is without DataTable. I am writing controller function again for you.
public function getProduct($id) {
$product = Product::where('product_id',$id)->with('product_log')->first(); //If I use DataTable how can I send this $product data
$data = Product_log_view::where('sku',$product->sku)->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
Hello @afoysal. You have 2 ways of doing what you want.
- Solution no 1: You wrap the entire Datatables return statement into a if statement checking if the request is ajax (see the code below)
public function getProduct($id) {
$product = Product::where('product_id',$id)->with('product_log')->first();
if (request()->ajax()) {
$data = Product_log_view::where('sku',$product->sku)->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
// I am making the view up, you should change it to what you need
return view('products.show', ['product' => $product]);
}
- Solution no 2: You separate everything into 2 methods, one for the the Datatables call and another for the view call
// This method is for the blade file
public function getProduct($id) {
$product = Product::where('product_id',$id)->with('product_log')->first();
// I am making the view up, you should change it to what you need
return view('products.show', ['product' => $product]);
}
// This method is for the Datatables call. The datatables should call this route with the $sku parameter
public function getProductLog($sku) {
$data = Product_log_view::where('sku', $sku)->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
Please or to participate in this conversation.