I have a search line where a user enters query divided by commas. I need to find at least 1 matches in the SQL-table. But I need to mark the matches in each found object too. How can I do this?
Working search (Laravel Eloquent (PostgreSQL) without marking matches):
public function searchOfAuthor(Request $request)
{
$search = array_map('trim', explode(',', $request->get('search')));
$columns = [
'city',
'phone',
'email',
'skype',
'icq',
'vk'
];
$authors = AuthorMask::where(function ($query) use ($columns, $search) {
foreach ($search as $searchKey) {
if (!empty($searchKey)) {
$query->orWhere('name', 'ILIKE', '%'.$searchKey.'%');
foreach ($columns as $column) {
$query->orWhere($column, 'ILIKE', $searchKey);
}
}
}
})
->with('author')
->orderByRaw('company_id = ? desc', Auth::user()->company_id)
->paginate(5);
if (empty($authors->items())) {
return response()->json([
'data' => null,
'error' => 'Authors Have Not Been Found'
], 404);
}
return response()->json([
'data' => [
'authors' => $authors
],
'error' => null
], 200);
}
Sorry for my English.
There are nothing condition like ILIKE in laravel or Mysql. There should be LIKE. There are two lines of codes have ILIKE.
$query->orWhere('name', 'ILIKE', '%'.$searchKey.'%');
$query->orWhere($column, 'ILIKE', $searchKey);
Remove I from ILIKE of above two lines. After removing I from ILIKE it should look like
$query->orWhere('name', 'LIKE', '%'.$searchKey.'%');
$query->orWhere($column, 'LIKE', $searchKey);
Did it. Just created new array for mark of matches. Another question: who know how to append array to the object in pagination?
$matches = [];
foreach ($authors->items() as $author) {
$matches[$author->id]['name'] = 0;
foreach ($columns as $column) {
$matches[$author->id][$column] = 0;
}
foreach ($search as $searchKey) {
if (!empty($searchKey)) {
foreach ($author->toArray() as $key => $attribute) {
if (!strcasecmp($searchKey, $attribute)) {
$matches[$author->id][$key] = 1;
}
}
}
}
}
return response()->json([
'data' => [
'authors' => $authors,
'matches' => $matches
],
'error' => null
], 200);
Related
I have multidimensional array and I want to sum all the same value with a certain condition.
Here's what my array looks like
$input_taxes = [
[
"tax_account_id" => 65,
"checkbox" => false,
"amount" => "13950.89",
],
[
"tax_account_id" => 64,
"checkbox" => 0,
"amount" => "1920.70",
]
];
What I've researched, they used array_reduce. Below is my code:
$result = array_reduce($input_taxes, function ($carry, $item) {
if (! isset($carry[$item['tax_account_id']])) {
$chart_of_account = ChartOfAccount::where('id', $item['tax_account_id'])
->first()
->name;
$carry[$item['tax_account_id']] = [
'tax_account_id' => $item['tax_account_id'],
'amount_to_apply' => $item['amount_to_apply'],
'chart_of_account' => $chart_of_account
];
} else {
$carry[$item['tax_account_id']]['amount_to_apply'] += $item['amount_to_apply'];
}
return $carry;
});
Currently, it will sum all the same values even though it is not checked. Now I want to put a condition where it should be check first before it will sum all the same values.
NOTE: I already tried to put an if statement inside of both if and else statement but unfortunately it didn't work.
Question: How can I add a certain condition within array_reduce?
The check must be inserted at the beginning of your function.
$result = array_reduce($input_taxes, function ($carry, $item) {
if (!$item['checkbox']) {
return $carry;
}
if (! isset($carry[$item['tax_account_id']])) {
$chart_of_account = ChartOfAccount::where('id', $item['tax_account_id'])
->first()
->name;
$carry[$item['tax_account_id']] = [
'tax_account_id' => $item['tax_account_id'],
'amount_to_apply' => $item['amount_to_apply'],
'chart_of_account' => $chart_of_account
];
} else {
$carry[$item['tax_account_id']]['amount_to_apply'] += $item['amount_to_apply'];
}
return $carry;
});
I can also offer a small optimization to reduce the number of requests to the database
$chartOfAccountNames = UserProfile::query()
->whereIn('id', array_column($input_taxes, 'tax_account_id'))
->pluck('name', 'id')
->toArray();
$result = array_reduce($input_taxes, static function ($carry, $item) use ($chartOfAccountNames) {
if (!$item['checkbox']) {
return $carry;
}
if (isset($carry[$item['tax_account_id']])) {
$carry[$item['tax_account_id']]['amount_to_apply'] += $item['amount'];
return $carry;
}
$carry[$item['tax_account_id']] = [
'tax_account_id' => $item['tax_account_id'],
'amount_to_apply' => $item['amount'],
'chart_of_account' => $chartOfAccountNames[$item['tax_account_id']],
];
return $carry;
});
I have the code if there is the same name in 2nd foreach, then the same name is not displayed, but I don't know how to keep the same name not displayed?.
$arr = [];
foreach ($respon_tel['item'] as $item) {
$proyek = [
'nama_proyek' => $item['judul_kontrak'],
'from' => 'Another API'
];
foreach($model as $m){
if(trim(strtolower($item['judul_kontrak'])) == trim(strtolower($m['nama_proyek']))){
// ????
}
}
$arr[] = $proyek;
}
return $arr;
You can get all the nama_proyek's from the $model then you can check if the current $item's judul_kontrak is in that set:
$models = collect($model)
->map(fn ($i) => trim(strtolower($i['nama_proyek'])));
foreach ($respon_tel['item'] as $item) {
if (! $models->contains(trim(strtolower($item['judul_kontrak'])))) {
$arr[] = [
'nama_proyek' => $item['judul_kontrak'],
'from' => 'Another API'
];
}
}
Or you could get creative with the Collection methods:
collect($respon_tel['item'])
->pluck('judul_kontrak', 'judul_kontrak')
->map($f = fn ($item) => trim(strtolower($item)))
->diff(
collect($model)->pluck('nama_proyek')->map($f)
)->map(function ($item, $key) {
return [
'nama_proyek' => $key,
'from' => 'Another API',
];
})->values();
I tried to code a request with search system. Here the code:
$search = request()->get('search');
if(Auth::user()->hasRole('admin') || true)
{
list($orderBy, $orderDirection) = explode('.', request()->get('sort_by'));
$prestations = Prestation::with(
'service:id,name',
'facility:id,name'
)
->orWhere('service:name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
}
The problem is that I don't know how to do something like I tried with the orWhere -> get all service name (from the relationship "with") which are equal to my $search.
Thank you.
Try this query.
$prestations = Prestation::with(
[
'service' => function($service) use($search){
$service->select(['id','name'])->where('name', $search);
},
'facility' => function($facility) {
$facility->select(['id','name'])
}
]
);
I have this query:
Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->update(array(
'status' => 'stopped',
));
The problem is that the amount of records it has to go through to do the update causes it to take around 15 minutes or so to finish.
I would like to split it up so the select and update queries are separate entities. Something sort of like this:
$pending = Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->get();
$pending->update(array(
'status' => 'stopped',
));
How would I go about doing this? Or is there an easier way?
Thanks!
I wasn't thinking, I figured out the answer. I had to run the second part in a foreach like so:
$records = Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->get();
foreach ($records as $record) {
DB::table('sendqueue')
->where('ID', $record->ID)
->update(['status' => 'stopped']);
}
protected $table="user";
public function updateUser($id,$username)
{
$resultData = array();
$updateArray = array('user_name'=>$username);
$update=DB::table('user')
->where('user_id', $id)
->update($updateArray);
return $resultData['status'] = true;
}
$my_id = preg_replace ('#[^0-9]#', '', $request->id);
if (! empty ($my_id)) {
$this->c->where ('id', $my_id )->update ( [
'first_name' => $request->get ( 'first_name' ),
'last_name' => $request->get ( 'last_name' ) ,
'phone' => $request->get ( 'phone' )
] );`enter code here`
\Session::flash ('message', 'Update Successful');
return redirect ('customer');
}
$this->edit ();
http://developer.e-power.com.kh/update-query-in-laravel-5-2/
I searched in Google and consulted the PHP documentation, but couldn't figure out how the following code works:
$some='name=Licensing Module;nextduedate=2013-04-10;status=Active|name=Test Addon;nextduedate=2013-04-11;status=Active';
function getActiveAddons($somet) {
$addons = array( );
foreach ($somet as $addon) {
if ($addon['status'] == 'Active') {
$addons[] = $addon['name'];
continue;
}
}
return $addons;
}
echo (count( getActiveAddons( $some ) ) ? implode( '<br />', getActiveAddons( $some ) ) : 'None');
The code always echo's None.
Please help me in this.
I don't know where you got this code from but you've initialized $some the wrong way. It is expected as an array like this:
$some = array(
array(
'name' => 'Licensing Module',
'nextduedate' => '2013-04-10',
'status' => 'Active'
),
array(
'name' => 'Test Addon'
'nextduedate' => '2013-04-11',
'status' => 'Active'
)
);
I guess the article you've read is expecting you to parse the original string into this format.
You can achieve this like this:
$string = 'name=Licensing Module;nextduedate=2013-04-10;status=Active|name=Test Addon;nextduedate=2013-04-11;status=Active';
$result = array();
foreach(explode('|', $string) as $record) {
$item = array();
foreach(explode(';', $record) as $column) {
$keyval = explode('=', $column);
$item[$keyval[0]] = $keyval[1];
}
$result[]= $item;
}
// now call your function
getActiveAddons($result);
$some is not an array so foreach will not operate on it. You need to do something like
$some = array(
array(
'name' => 'Licensing Module',
'nextduedate' => '2013-04-10',
'status' => 'Active'
),
array(
'name' => 'Test Addon',
'nextduedate' => '2013-04-11',
'status'=> 'Active'
)
);
This will create a multidimensional array that you can loop through.
function getActiveAddons($somet) {
$addons = array( );
foreach ($somet as $addon) {
foreach($addon as $key => $value) {
if ($key == 'status' && $value == 'Active') {
$addons[] = $addon['name'];
continue;
}
}
}
return $addons;
}
First, your $some variable is just a string. You could parse the string into an array using explode(), but it's easier to just start as an array:
$some = array(
array(
"name" => "Licensing Module",
"nextduedate" => "2013-04-10",
"status" => "Active",
),
array(
"name" => "Test Addon",
"nextduedate" => "2013-04-11",
"status" => "Active",
)
);
Now, for your function, you are on the right track, but I'll just clean it up:
function getActiveAddons($somet) {
if (!is_array($somet)) {
return false;
}
$addons = array();
foreach ($somet as $addon) {
if ($addon['status'] == 'Active') {
$addons[] = $addon['name'];
}
}
if (count($addons) > 0) {
return $addons;
}
return false;
}
And finally your output (you were calling the function twice):
$result = getActiveAddons($some);
if ($result === false) {
echo "No active addons!";
}
else {
echo implode("<br />", $result);
}