Jan 9, 2017
0
Level 2
yajra datatable global filter does not use LIKE operator instead in eager loaded models
I have a Conversation model like this :
class Conversation extends Model
{
protected $primaryKey = 'conversation_id';
public function latestQuestion ()
{
return $this->hasOne('App\Question','conversation_id','conversation_id')
->orderBy('created_at', 'desc');
}
}
And a Question model like this :
class Question extends Model
{
protected $primaryKey = 'question_id';
public function conversation ()
{
return $this->belongsTo('App\Conversation', 'conversation_id', 'conversation_id');
}
}
Now I want to list all Conversations with their latest questions. for that I used yajra laravel datatables package like this :
$('#allQuestionsTable').DataTable({
processing: true,
serverSide: true,
"bSort": false,
"responsive": true,
ajax: {
url: '{!! route('admin.conversation.conversationsDatatable ') !!}'
},
columns: [
{
data: 'conversation_id',
name: 'conversation_id'
},
{
data: 'text',
name: 'latestQuestion.text'
},
{
data: 'created_at',
'searchable': false,
'orderable': false
},
{
data: 'answer',
name: 'latestQuestion.answer'
}
});
As you can see I used latestQuestion relation to fetch text and answer fields from Question model.
Now I want user can input a word in global search filter textbox and datatable show all conversations with questions that have a text or answer field contain that word.
On the backend I wrote this :
$conversations =
Conversation::
with('latestQuestion');
$datatable = app('datatables')->of($conversations)
->addColumn('checkbox', '<input type="checkbox" name="item_id[]" value="{{$conversation_id}}">')
->editColumn('text', function ($conversation) {
$res = '<span data-toggle="tooltip" data-placement="top" title="' . str_limit(strip_tags($conversation->latestQuestion->text), 200) . '">' . str_limit(strip_tags($conversation->latestQuestion->text), 40) . '</span>';
return $res;
})
->editColumn('answer', function ($conversation) {
$res = '<span data-toggle="tooltip" data-placement="top" title="' . str_limit(strip_tags($conversation->latestQuestion->answer), 200) . '">' . str_limit(strip_tags($conversation->latestQuestion->answer), 40) . '</span>';
return $res;
})
//other columns here
;
return $datatable->make(true);
But when I tried to input a word on global textbox, datatable return all questions that have exactly that word in answer or text field means uses = operator while I want to use LIKE operator.
What do I do?
Please or to participate in this conversation.