Laravel Eloquent query OR WHERE - laravel

How to write a Laravel Eloquent Query for a Model/table named Country/countries where If I search from any variable and get result according to and the query will search from 3 different column as similar in SQL in given Image.
How to write a similar query in Laravel in Laravel 5.6 /5.7

What you are looking for is where() and orWhere() functions in Query Builder in laravel
$countries = Country::where('name', 'like', '%af%')
->orWhere('alpha-2', 'like', '%af%)
->orWhere('alpha-3', 'like', '%af%')
->get();
and if you want to dynamically pass values to where() functions.
$value = 'af';
$countries = Country::where('name', 'like', "%$value%")
->orWhere('alpha-2', 'like', "%$value%")
->orWhere('alpha-3', 'like', "%$value%")
->get();
Note 'Double Quotation' in variable passing.

use your model query like this
Model::where(function($query)
{
$query->where('column1', 'like', 'keyword');
$query->orWhere('column2', 'like', 'keyword');
$query->orWhere('column3', 'like', 'keyword');
})->get();

Related

Laravel - whereHas query not returning correct records

I am currently writing a search query but I'm having issues joining a where with a whereHas
if I wanna search for categories only..
The below query works if I just search for categories solo
$goals = $myGoals
->whereHas('categories', function ($q) use ($search) {
$q->where('name', 'like', "%$search%");
})->paginate(10);
if I wanna search for the title only..
Same as above if I wanna search for the title of goals solo
$goals = $myGoals->where('title', 'like', "%$search%" )->paginate(10);
both of the queries work if I want to search for them individually.
Query attempt
Below is the query I've written to allow a user to search for the title of a goal and the name of a category.
$myGoals = $team->goals();
$goals = $myGoals
->whereHas('categories', function ($q) use ($search) {
$q->where('name', 'like', "%$search%");
})->where('title', 'like', "%$search%" )->paginate(10);
I have tried the above query but the search results return empty when I enter an input. I've also tried swapping the where() to orWhere() which then returns a goal that doesn't belong to the $team model instance but it's a goal record linking to a different Team model id within the pivot table.
Can anyone spot where I am going wrong? thanks
You need to use it all inside where.
$goals = $myGoals->where(function ($query) use ($search){
return $query->whereHas('categories', function ($q) use ($search) {
$q->where('name', 'like', "%$search%");
})->orWhere('title', 'like', "%$search%" );
})->paginate(10);

Where query in both relationships Laravel 7

How can I get all users which names starts with the given string, and also where their city from profile relationship is equal to London?
$users = User::where('name', 'LIKE', "{$searchingName}%")->with('profile')->get();
You may find whereHas very useful:
use Illuminate\Database\Eloquent\Builder;
// Retrieve profiles where the city is like 'London'.
$users = User::where('name', 'LIKE', "{$searchingName}%")
->whereHas('profile', function (Builder $query) {
$query->where('city', 'like', 'London');
})->get();

Sort a result of search in Laravel Eloquent model with relations

I did a search for a Model (Startup), next step is to order results of the search by DESC or ASC (it doesn't matter now)
My current code is:
$startups = Startup::whereHas('category', function ($query) use ($search, $sort_type) {
$query
->where('name', 'like', "%$search%")
->orderBy('name', $sort_type);
})
->orWhere('description', 'LIKE', "%$search%")
->orWhere('url', 'LIKE', "%$search%")
->get();
return StartupResource::collection($startups);
Explanation of the code:
as you see at the beginning I'm using "whereHas" to search also for coincidences in related model - "Category".
then inside of "whereHas" I'm trying to apply 'orderBy('name', $sort_type)' but it doesn't work properly (it doesn't sort by categories)
I know that we can create method in Startup model and sort it inside of the method, but the problem is that I have to pass variables to the method ( $sort_type, $search) and I don't know how to do this
So how to sort Startups model including related Category model by ASC or DESC in my case ?
Thank you guys a lot for any ideas and help!
That's not possible with whereHas(). You can use a JOIN:
$startups = Startup::select('startups.*')
->join('category', 'category.id', '=', 'startups.category_id')
->where('category.name', 'LIKE', "%$search%")
->orWhere('startups.description', 'LIKE', "%$search%")
->orWhere('startups.url', 'LIKE', "%$search%")
->orderBy('category.name', $sort_type)
->get();

How do I query a relation database in Laravel?

I have this search query for Post model in Laravel:
return $q ->where('title', 'like', '%'.$keyword.'%')
Which works totally fine. In my Post model I also have a relation database categories. This is available as an object in my returned post object.
Is there a way I can query the category -> name?
I have tried:
return $q ->where('category->name', 'like', '%'.$keyword.'%')
I believe what you are looking for can be found here: https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
For your use case you need to make sure you have the relationship established in your Post model. If that is set your query should look similar to the following:
return $q->where('title', 'like', '%'.$keyword.'%')->whereHas('category',
function ($query) use ($keyword) {
$query->where('name', 'like', '%'.$keyword.'%');
})->get();
Hope this helps!
If you're db is well structured - then will want to use Laravel's ORM for this stuff.
https://laravel.com/docs/5.6/eloquent#introduction
I assume that you have Models:
Post
Category
Also that you have category_id as a foreign key in Post model.
This is basic structure.
I assume that you want to search Posts and Categories same time so you can do something like this:
return Post::leftJoin('categories', 'posts.category_id', '=', 'categories.id)
->where('categories.name', 'like', '%' . $keyword . '%')
->orWhere('posts.title', 'like', '%' . $keyword . '%')
->get();

How to force a where clause on a collection in laravel 5

I have a table that I am running a search on. Foo. I want to check if the search term is like a bunch of different fields in the table. But I want the results to be forced to be "active".
I am using Laravel 5.0
$foos = DB::table('foo')
->where('name', 'LIKE', '%' . $query . '%')
->orWhere('description', 'LIKE', '%' . $query . '%')
->orWhere('address_city', 'LIKE', '%' . $query . '%')
->orWhere('short_description', 'LIKE', '%' . $query . '%')
->orWhere('interview', 'LIKE', '%' . $query . '%')
->get();
$foos = $foos->where('active', true)->get();
Collection has a where method similar to query builder.
$activeFoos = $foos->where('active', true);
https://laravel.com/docs/5.4/collections#method-where
Edit :
I understand what you need from the comments now. Try the code below, and also note that i changed your original $query to $find. So keep that in mind.
$foos = DB::table('foo')
->where('active', true)
->where(function ($query) use ($find) {
$query->where('name', 'LIKE', "%{$find}%")
->orWhere('description', 'LIKE', "%{$find}%")
->orWhere('address_city', 'LIKE', "%{$find}%")
->orWhere('short_description', 'LIKE', "%{$find}%")
->orWhere('interview', 'LIKE', "%{$find}%");
})
->get();
Is the value of active actually true or just truthy (such as an integer of 1)?
For loose comparisons then you'll need to use whereLoose('active', true), otherwise Laravel's method where() on Collections will perform strict type comparisons.
Like Sandeesh said, you'll need to remove the get() since get() is a query builder method, Collections where() methods return a new collection not a builder instance.

Resources