Using Conditional Statement in updateOrCreate - laravel

before updating i need to check if a particular value is empty. if its empty avoid updating that column
DrmCustomer::updateOrCreate([
'email'=> isset($row['email']) ? $row['email'] : "",
'user_id'=> $user_id,
],[
(isset($row['fieldMobilePhone']['value'])) ?: 'phone'=> $row['fieldMobilePhone']['value'],
(isset($row['fieldWebsite']['value'])) ?: 'website'=> $row['fieldWebsite']['value'],
(isset($row['fieldStreet1']['value'])) ?: 'address'=> $row['fieldStreet1']['value'],
(isset($row['fieldCity']['value'])) ?: 'city'=> $row['fieldCity']['value'],
(isset($row['fieldState']['value'])) ?: 'state'=> $row['fieldState']['value'],
(isset($row['fieldZip']['value'])) ?: 'zip_code'=> $row['fieldZip']['value'],
(isset($row['fieldCountry']['value'])) ?: 'country'=> $row['fieldCountry']['value'],
'default_language'=> 'DE',
'currency'=> 'EUR',
'insert_type'=> 'API'
]);

You can use Laravel Collection to filter your data by using following technique
<?php
$mapFields = [
'fieldMobilePhone' => 'phone',
'fieldWebsite' => 'website',
'fieldStreet1' => 'address',
'fieldCity' => 'city',
'fieldState' => 'state',
'fieldZip' => 'zip_code',
'fieldCountry' => 'country',
];
$validKeys = array_keys($mapFields);
$collection = collect($row);
$data = $collection->filter(function($value, $key) use($validKeys){
return Arr::get($value, 'value' false) && in_array($key, $validKeys);
})->flatMap(function($value, $key) use($mapFields){
return [
$mapFields[$key] => $value['value']
];
})->merge([
'default_language'=> 'DE',
'currency'=> 'EUR',
'insert_type'=> 'API'
])->all()->toArray();
DrmCustomer::updateOrCreate([
'email'=> Arr::get($row, 'email', ''),
'user_id'=> $user_id,
], $data);

I was able to do that like this:
DrmCustomer::updateOrCreate(['email' => isset($row['email']) ? $row['email'] : '', 'user_id' => $user_id], [
'default_language'=> 'DE',
'currency'=> 'EUR',
'insert_type'=> 'API'
] + (isset($row['fieldMobilePhone']['value']) ? [
'phone' => $row['fieldMobilePhone']['value']
] : []) + (isset($row['fieldWebsite']['value'])) ? [
'website' => $row['fieldWebsite']['value']
] : []) + (isset($row['fieldStreet1']['value']) ? [
'address' => $row['fieldStreet1']['value']
] : []) + (isset($row['fieldCity']['value']) ? [
'city' => $row['fieldCity']['value']
] : []) + (isset($row['fieldState']['value']) ? [
'state' => $row['fieldState']['value']
] : []) + (isset($row['fieldZip']['value']) ? [
'zip_code' => $row['fieldZip']['value']
] : []) + (isset($row['fieldCountry']['value']) ? [
'zip_code' => $row['fieldCountry']['value']
] : []));

Related

yajra/laravel-datatables filterColumn doesnt work

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)'
]
]
]);

Tweaking Lingo parameters with carrot2 (with PHP)

I'm trying to tweak the call to the Carrot2 REST API :
$client = new Client();
try {
$params = [
'multipart'=> [
['name'=> 'dcs.c2stream', 'contents' => $xml],
['name' => 'dcs.algorithm', 'contents' => 'lingo'],
['name' => 'dcs.output.format', 'contents' => 'JSON'],
['name' => 'dcs.clusters.only', 'contents' => 'true'],
['name' => 'MultilingualClustering.defaultLanguage', 'contents' => 'FRENCH'],
['name' => 'preprocessing.labelFilters.minLengthLabelFilter.minLength', 'contents' => 5],
['name' => 'preprocessing.documentAssigner.minClusterSize', 'contents' => 4]
],
'debug' => false
];
$response = $client->request('POST', 'http://devbox:8080/dcs/rest', $params);
The lingo parameters 'preprocessing.labelFilters.minLengthLabelFilter.minLength' and 'preprocessing.documentAssigner.minClusterSize' have no impact in the request.
I've found them in the documentation of the lingo algorithm.
Thanks for help !
With the good docker image, everything is fine (docker pull touane/carrot2) :
$c2Payload = [
'algorithm' => 'Lingo',
'language' => 'French',
'parameters' => [
'preprocessing' => [
'documentAssigner' => [
'minClusterSize' => 4
],
'labelFilters' => [
'minLengthLabelFilter' => [
'minLength' => 8
],
'completeLabelFilter' => [
'labelOverrideThreshold' => 0.35
]
]
],
'scoreWeight' => 1, // Tri par score
'clusterBuilder' => [
'phraseLabelBoost' => 2.5
],
'dictionaries' => [
'wordFilters' => [
['exact' => $this->getParameter('carrot2')['stop_words']]
]
],
'matrixBuilder' => [
'termWeighting' => [
'#type' => 'LinearTfIdfTermWeighting'
],
'boostFields' => ['title']
]
],
'documents' => []
];
$client = new Client();
$params = [
'body' => json_encode($c2Payload ),
'debug' => false
];
$response = $client->request('POST', $this->getParameter('carrot2')['url'], $params);

If condition with array in Laravel

I'm trying to create an array to convert JSON. My data is queried from database.
My problem is I have to check condition for array. If $item->verified == 1, my 'isVerified'
will be true, my email will be in verified and opposite.
Here is what I did, I check condition and create 2 array for it. Can I just use 1 array
for condition:
if( ($item->verified) == 1)
{
$data[] = [
'name' => $item->fullname,
'address' => $item->address,
'isVerified' => true,
'email' => [
'verified' => $item->email,
'unverified' => []
]
];
}
else
{
$data[] = [
'name' => $item->fullname,
'address' => $item->address,
'isVerified' => false,
'email' => [
'verified' => [],
'unverified' => $item->email
]
];
}
You can use ternary operator.
$data[] = [
'name' => $item->fullname,
'address' => $item->address,
'isVerified' => $item->verified == 1,
'email' => [
'verified' => $item->verified == 1 ? $item->email : [],
'unverified' => $item->verified == 0 ? $item->email : [],
]
];

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

How to Combine Aggregate + Lookup and Search in Laravel

I Have the following Aggregate + Lookup Query
Aggregate + lookup Query
$collection = Dapil_Kotakab::raw(function ($collection) use ($page, $perPage) {
return $collection->aggregate([
[
'$lookup' => [
'as' => 'KecDetails',
'from' => 'src_kecamatan',
'foreignField' => 'id',
'localField' => 'idKecamatan'
]
],
[
'$lookup' => [
'as' => 'KotDetails',
'from' => 'src_kota_kabupaten',
'foreignField' => 'code',
'localField' => 'idKota'
]
],
[
'$lookup' => [
'as' => 'ProvDetails',
'from' => 'src_provinsi',
'foreignField' => 'idProv',
'localField' => 'idProvinsi'
]
],
['$skip' => ($page - 1) * $perPage],
['$limit' => $perPage],
]);
});
And I have the following $Search code.
$collection = Dapil_Kotakab::whereRaw(array('$text'=>array('$search'=> "\"".$word."\"" )))->skip(($page - 1)*$perPage)->limit($perPage)->get();
I have tried several times, But I keep getting error.
Any suggestions in this regard will be appreciated.

Resources