yajra/laravel-datatables filterColumn doesnt work - laravel

Page loading correctly and every things work fine while bringing datas. Now I want to filter them but FilterColumn() method doesnt even work. When I tried filter() method it's working but then I won't get $keywords variable. I am missing something while querying leads ?
if (request()->ajax()) {
$leads = Lead::with('country','agent','detail')->orderBy('id','DESC');
$leads->where(function($q) use ($user){
if ($user->hasRole('agent') && !$user->hasRole('admin') && !$user->hasRole('lead')){ //agent isem
$q->where('agent_id',$user->id)
->orWhere('agent_id',null);
}
});
return DataTables::of($leads)->
addColumn('edit_button', function ($lead) {
$link = route('leads.edit',$lead->id);
return ' Edit ';
})->
filterColumn('edit_button', function ($query, $keyword) {
dd($keyword);
return $query->whereHas('patient', function ($query) use ($keyword) {
$query->whereRaw("CONCAT( name, ' ', surname ) like ?", ["%$keyword%"]);
});
})->rawColumns(['edit_button','phone','detail.conversion_notes'])->
toJson();
}
$tableColumn = [
['data' => 'edit_button', 'name' => 'edit_button', 'title' => 'Edit', 'searchable' => false],
['data' => 'full_name', 'name' => 'full_name', 'title' => 'Full Name'],
['data' => 'phone', 'name' => 'phone', 'title' => 'Phone'],
['data' => 'email', 'name' => 'email', 'title' => 'Email'],
['data' => 'country.name', 'name' => 'country.name', 'title' => 'Country'],
['data' => 'agent.name', 'name' => 'agent.name', 'title' => 'Agent'],
['data' => 'treatment', 'name' => 'treatment', 'title' => 'Treatment'],
['data' => 'find_us', 'name' => 'find_us', 'title' => 'Find Us'],
['data' => 'form_type', 'name' => 'form_type', 'title' => 'Form','visible' => false],
['data' => 'social_account', 'name' => 'social_account', 'title' => 'Sosyal Medya'],
['data' => 'created_at', 'name' => 'created_at', 'title' => 'Created At'],
['data' => 'detail.conversion_notes', 'name' => 'detail.conversion_notes', 'title' => 'Primary Notes'],
];
$html = $builder->columns($tableColumn)->parameters([
"pageLength" => 25,
"lengthMenu" => [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
'dom' => 'Bfrtip',
'columnDefs' => [
['width' => '2%', 'targets' => 0],
['width' => '7%', 'targets' => 1],
'buttons' => [
'pageLength',
[
'extend' => 'colvis',
'collectionLayout' => 'fixed two-column',
'columns' => ':not(.noVis)'
]
]
]);

Related

Call to a member function find() on array

"message": "Call to a member function find() on array",
"exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
How do I get the id parameter from the array below: When i pass the parameter 1 i want to fetch only first element when id is 2 I fetch item 2 of array
public function payments($id){
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
return $data->find($id);
}
Since we're using a multi-dimensional array, we can use array_search with array_column.
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
$key = array_search($id, array_column($data, 'id'));
echo var_dump($data[$key]);
If you want a "Laravel" alternative solution using collection
public function payments($id){
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
$data = collect($data);
return $data->where('id', $id)->first();
}
public function payments($id){
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
$data = collect($data);
return $data->where('id', $id)->all();
}

Laravel - How to add where clause in LaravelChart model

In my Laravel-5.8, I have this code:
$chart_settings = [
'chart_title' => 'Users By Months',
'chart_type' => 'line',
'report_type' => 'group_by_date',
'model' => 'App\\User',
'group_by_field' => 'last_login_at',
'group_by_period' => 'month',
'aggregate_function' => 'count',
'filter_field' => 'last_login_at',
'column_class' => 'col-md-12',
'entries_number' => '5',
];
$chart = new LaravelChart($chart_settings);
How do I add this where clause to the code above ( 'model' => 'App\User',)
where('hr_status', 0)->where('company_id', $userCompany)
Thanks
You can use where_raw to specify custom where condition
$chart_settings = [
'chart_title' => 'Users By Months',
'chart_type' => 'line',
'report_type' => 'group_by_date',
'model' => 'App\\User',
'group_by_field' => 'last_login_at',
'group_by_period' => 'month',
'aggregate_function' => 'count',
'filter_field' => 'last_login_at',
'column_class' => 'col-md-12',
'entries_number' => '5',
'where_raw' => 'hr_status = 0 AND company_id ='.$userCompany
];
$chart = new LaravelChart($chart_settings);

Search in Laravel Datatables

In my datatables, the search function works only on the name column. The name column ist different that the others. It means, the name column gives the value of the database back, while the others gives a value back with the function add column
In the column array there are the following values:
ยดยดยด
protected function getColumns()
{
return [
'status',
'name' => [
'title' => 'Name',
'orderable' => true,
'searchable' => true
],
'location' => [
'title' => 'Standort',
],
'department' => [
'title' => 'Abteilung',
'orderable' => true,
'searchable' => true
],
'division' => [
'title' => 'Bereich',
'orderable' => true,
'searchable' => true
],
'leader' => [
'title' => 'Verantwortlicher',
'orderable' => true,
'searchable' => true
],
'start_date' => [
'title' => 'Startdatum',
'searchable'=> true,
],
'end_date' => [
'title' => 'Enddatum',
'searchable'=> true
]
];
}
```
Why it doesn't search on all columns? What i have to do?
Try to search by using this code.
return Datatables::of($tasks)
->filter(function ($query) use ($request) {
$title = $request->title;
if (isset($title) && !empty($title)) {
$query->where('title', 'like', '%'.$title.'%');
}
})->make(true);
OR
return Datatables::of($tasks)
->filterColumn('title', function($query, $value) {
$query->whereRaw("title like ?", ["%{$value}%"]);
})->make(true);

Can't call model method in toSearchableArray array when created via factory

I am trying to get a model factory to work, the main model Venue the following:
Place which is a hasOne relation
Type which has a belongsTo relation
On top of that, place has a function called full_address, that returns a comma separated string.
The Venue has a toSearchableArray() to send custom data to Algolia, when I run the factory create method I get:
PHP Error: Call to a member function full_address() on null in app/Venue.php on line 57
This is the venue Model
class Venue extends Model
{
use Searchable;
public function toSearchableArray()
{
$type = $this->type()->first();
$place = $this->place()->first();
return [ 'name' => $this->name,
'type' => $type['name'],
'type_id' => $type['id'],
'venue_id' => $this->id,
'background_image' =>$type['background_file_name'],
'full_address' => $place->full_address(),
'slug' => $this->slug_field,
'_geoloc' =>
[ 'lat' => (float)$this->latitude,
'lng' => (float)$this->longitude
]
];
}
public function place()
{
return $this->hasOne('App\Place');
}
public function type()
{
return $this->belongsTo('App\Type');
}
}
And these are the model factories
$factory->define(App\Venue::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'phoneNumber' => $faker->word,
'latitude' => $faker->latitude,
'longitude' => $faker->longitude,
'LatLong' => $faker->word,
'website' => $faker->word,
'type' => $faker->word,
'place_id' => function () {
return factory(App\Place::class)->create()->id;
},
'status' => $faker->word,
'slug_field' => $faker->word,
'type_id' => function () {
return factory(App\Type::class)->create()->id;
},
];
});
$factory->define(App\Place::class, function (Faker\Generator $faker) {
return [
'place_id' => $faker->randomNumber(),
'administrative_area_level_2' => $faker->word,
'administrative_area_level_1' => $faker->word,
'country' => $faker->country,
'postal_town' => $faker->word,
'postal_code_prefix' => $faker->word,
'postal_code' => $faker->word,
'route' => $faker->word,
'locality' => $faker->word,
'premise' => $faker->word,
'street_number' => $faker->word,
'neighborhood' => $faker->word,
'point_of_interest' => $faker->word,
'natural_feature' => $faker->word,
'administrative_area_level_3' => $faker->word,
'postal_code_suffix' => $faker->word,
'sublocality_level_1' => $faker->word,
'subpremise' => $faker->word,
'sublocality_level_5' => $faker->word,
'sublocality_level_4' => $faker->word,
'sublocality_level_2' => $faker->word,
'sublocality_level_3' => $faker->word,
'administrative_area_level_4' => $faker->word,
'colloquial_area' => $faker->word,
'long' => '123',
'lat' => '123',
];
});
$factory->define(App\Type::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'marker_file_name' => $faker->word,
'background_file_name' => $faker->word,
'description' => $faker->word,
'ShortName' => $faker->word,
];
});
what do I need to do in order to get a reference to the place that is associated with the venue.
Thanks

Yii2 add related attribute to sort

I have normal ModelSearch with ActiveDataProvider, and I would like to add a virtual/related attribute to sorting in gridview. If I'm doing with setSort, and I'm adding this only attribute, then all other attributes are not sortable any more. Is there a built-in way to add an attribute to Sort? Thanks a lot!
public function search($params) {
$query = Za::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder' => ['aonr' => SORT_ASC]],
'pagination' => [
'pageSize' => 15,
],
]);
$dataProvider->setSort([
'attributes' => [
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
]
]);
$this->load($params);
...
}
You can set the sortable attributes with the setSort method, but in this case you need to set all the columns you want to sort, not just the column from the relation.
If you want to add one column you can try this (merging the currently existing sort attributes with the new one):
$dataProvider->setSort([
'attributes' => array_merge(
$dataProvider->getSort()->attributes,
[
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
]
),
]);
or you can add the missing attributes/columns by hand (which is a far better idea)
$dataProvider->setSort([
'attributes' =>
[
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
// Other attribute
'id' => [
'asc' => ['id' => SORT_ASC],
'desc' => ['id' => SORT_DESC],
],
...
],
]);
Another way:
$dataProvider->getSort()->attributes['lwnr'] = [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
];

Resources