Laravel push in order - laravel

Hi I am trying to combine two collections in a way that, the difference from the second collection is inserted one by one at the end of the first collection. However, when I try to use diff to find the difference between two collections and push each different set, the order seems to be messed up. Can someone help me with this please?
$serviceids = Service::where('service_name', 'like', '% ' . $chip_service . ' %')
->orWhere('service_name', 'like', '% ' . $chip_service)
->orWhere('service_name', 'like', $chip_service . ' %')
->orWhere('service_name', $chip_service)
->pluck('service_recordid');
$service_description_all = Service::where('service_description', 'like', '% ' . $chip_service . ' %')
->orWhere('service_description', 'like', '% ' . $chip_service)
->orWhere('service_description', 'like', $chip_service . ' %')
->orWhere('service_description', $chip_service)
->pluck('service_recordid');
$service_description = $service_description_all->diff($serviceids)->all();
foreach ($service_description as $key => $ser){
$serviceids->push($ser);
}

push function acts like stack, so the first element will be at the end the last in the stack.
you can use directly:
$serviceids = $service_description + $serviceids

Related

How can I escase string in whereRaw?

In laravel 9 I use whereRaw in code like :
$query->whereRaw( 'item.day ' . $sign . "'".$filter_day."' " );
But I need to escape it.
Function mysql_real_escape_string is absolute now...
What can I use here ? I work with mysql8 now, but I prefer decision for different db on laravel side.
Thanks in advance!
I suggest reading the Laravel documentation thoroughly: https://laravel.com/docs/9.x/queries#raw-methods
You can escape the string using ? as the link I given above.
$query->whereRaw( 'item.day ' . $sign . ' ?', [$filter_day]);
You must NOT escape the operator $sign because its an MYSQL operator.
And also you must make sure that you properly check/sanitize $sign to avoid SQL injection. E.g.
if (in_array($sign, ['=', '<', '>', '<=', '>=', '!=', '<>'])) {
$query->whereRaw( 'item.day ' . $sign . ' ?', [$filter_day]);
}

A way to determine if "where like" matches 100% with a post in Laravel?

Is there a way to determine if this query:
$news_result = Post::with('image')
->has('image')
->with('tags')
->where('title', 'LIKE', '%' . $search . '%')
->get();
Has a 100% match with some title? If a user searches for "Some random news title" and in the database there is a post with such title to return some kind of marker?
The LIKE operator is used to partially match something.
You should not use it, and do ->where('title', $search) instead.
Remove '%' in your code
$news_result = Post::with('image')->has('image')->with('tags')->where('title', $search)->get();

LARAVEL column Eloquent query with OrderByRaw

I need to calculate distance with latitude,longitude and order it by nearest,
but in my OrderBy clause latitude is missing, I don't know why
->with(['foglio' => function ($query) use ($latitude, $longitude) {
$query->select([
'id',
'indirizzo',
'piano',
'categoria',
'consistenza',
'latitude',
'longitude',
]);
$query->WhereNotNull(['indirizzo', 'latitude', 'longitude']);
}])
->limit($limit)
->orderBy(DB::raw('ABS(latitude - ' . $latitude . ') + ABS(longitude - ' . $longitude . ')'), 'ASC')
->get();
You could use:
->orderByRaw('ABS(latitude - ' . $latitude . ') + ABS(longitude - ' . $longitude . ') ASC')
You should use left join, not eager loading.
Eager loading will produce two seperate queries and that's why you can't access latitude
column in order by clause.

How to match only the beginning of a field using query filtering in Laravel

I have a query param from the URL q, for this example let's say it's foo. This needs to be used to filter either the name, surname, or email of a User record.
But partial matches should only be returned if they match the beginning of at least one of those fields.
e.g a User with name oofoo should not be returned. I think this is a regex issue.
I'm using Eloquent orWhere():
return $this->model->where("id", $blahblahId)
->firstOrFail()
->where('name', 'like', '%' . $queryParam . '%')
->orWhere('surname', 'like', '%' . $queryParam . '%')
->orWhere('email', 'like', '%' . $queryParam . '%')
->get();
You should have % only at end. Is is not Laravel issue, its just SQL
%xxx% - column has somewhere "xxx" inside (can be also beginning, end and exact "xxx")
%xxx - column ends with "xxx"
xxx% - column starts with "xxx"
So in your case it will be:
return $this->model->where("id", $blahblahId)
->firstOrFail()
->where('name', 'like', $queryParam . '%')
->orWhere('surname', 'like', $queryParam . '%')
->orWhere('email', 'like', $queryParam . '%')
->get();
Read more https://www.w3schools.com/sql/sql_like.asp

Laravel Eloquent Where Query with multiple words

I have the following query (cut for brevity):
$employees = Employee::where('first_name', 'LIKE', "%$query%")
->orWhere('last_name', 'LIKE', "%$query%")
Now this works when the user inputs a single name like 'John' or 'Smith', but when they input 'John Smith' it doesn't find anything. Am I missing an extra orWhere?
Try this :
Employee::where(DB::raw('CONCAT(first_name," ",lastname)'), 'LIKE', "%' . $query . '%"))
You would have to add a 3rd orWhere. For our search function we use something like this:
Employee::whereraw("COALESCE(last_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(first_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(last_name + ', ' + first_name, '') LIKE '%$query%'")
Adding Coalesce seemed to help with some issues we had when we first implemented it, not sure if it is necessary in your case though.
You can do :
$fullName = trim($query);
$employees = Employee::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%".$fullName."%")->get();
You are concatenating the values in database for first_name + ' ' + last_name and then using like to find the matching records.

Resources