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

afoysal's avatar

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();
0 likes
11 replies
afoysal's avatar

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.

laracoft's avatar

@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?

1 like
laracoft's avatar

@afoysal

  1. Have you followed the quick starter here? https://yajrabox.com/docs/laravel-datatables/master/quick-starter
  2. You are not supposed to return a view('welcome.product-details'...) (Unless you know how to write the entire HTML)
  3. Is $product_log_views a datatable class?
  4. 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()
    }
}
1 like
afoysal's avatar

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);
    }
afoysal's avatar

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);
    }
viorel's avatar

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);
}


2 likes

Please or to participate in this conversation.