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

patrick1981's avatar

ServerSide Processing - Datatables with some items not having a relationship (NULL)

I have a large collection with videos (Vod::) that are sorted into seperate categories (VodCat::) identified by an category_id and a database with categorie names identified by their category_id. So they have a relationship HasMany.

However when im adding videos to the database i don't always specify the category_id so its NULL, and when im serverside processing the datatables it's producing the error that the category_name is not found in the DataTables when sorting or searching through the datatable (hence the relationship is not found, other videos with a category_id set is working to find the category_name).

When ServerSide Processing is off its not producing any errors and the category_name is just blank in the DataTables but due the amount of videos i have to use ServerSide Processing which takes literally one second instead of 15secs.

Relevant stuff, ServerSide Processing this:

$videos = Vod::with('category');
	if (request()->ajax()) {
			return Datatables::of($videos)
			->editColumn('category_name', function($model) {
				 if (isset($model->category[0]->category_name)) {
					  return $model->category[0]->category_name;
				  }
				 else { return '-';}	
			})
			->editColumn('added', function($model) {
				if (isset($model->added)) {
					return '<span class="text-nowrap">' . Carbon::parse($model->added)->format('Y-m-d') . '</span>';
				}
				else { return '';}
			})
			->rawColumns(['added'])
			->make(true);

When im using $videos = Vod::with('category')->get(); it works but every request is taking 30secs to load in the Datatables, whether im sorting/searching/changing limits/etc, it just takes 30secs to load the Datatables. Not sure why thats why i used Vod::with('category');

I think i need to change the Model returning at least something when the category_id is NULL but i don't know how.

Already tried:

  1. ->withDefault in model is not working
  2. adding defaultcontent: '-' to DataTable, i think its not working either due to serverside processing

-edit- Issue is similiar to https://github.com/yajra/laravel-datatables/issues/803, i can use addcolumn then it works but then its not sortable and searchable. I think its very simple but i cant understand, using datatable serverside processing with a editcolumn that does not exist for some specific videos produces an error that the column can not be found (hence there is no relationship).

0 likes
1 reply

Please or to participate in this conversation.