search function with Where clause not work - laravel

hello everyone i try to run a search query with a condition for my column 'structure_id' but when i run the query , it display me also results who are not from 'structure' = 4
here my query :
$search = $request->get('q');
return User::where(function($q) use($search) {
$q->where('name', 'like', '%' . $search .'%')
->orWhere('email', 'like', '%'.$search.'%')
->where('structure_id' , '=' , '4');
})->get();
someone have an idea to resolve this? thanks a lot in advance

change it to
$search = $request->get('q');
return User::where(function($q) use($search) {
$q->where('name', 'like', '%' . $search .'%')
->orWhere('email', 'like', '%'.$search.'%');
})
->where('structure_id' , '=' , '4')
->get();

Related

Eager Loading With Where Condition faulty

public function getListStaff(string $searchString = '')
{
return Staff::with(['citizen_identification' => function ($query) {
$query->where('full_name', 'like', '%' . $searchString . '%');
}])
->where('phone', 'like', '%' . $searchString . '%')
->orWhere('email', 'like', '%' . $searchString . '%')
->paginate(2);
}
I use Repository in project livewire of me, i newbie use livewire, when i code method getListStaff is faulty, i try fix, but not success, please help me fix... Thanhks bros
Because I wrong syntax or visual code error or so ???
It seems like you have missed use statement for searchstring.
In any case, based from you question its not clear what is not working.
return Staff::with([
'citizen_identification' => function ($query) use ($searchString) {
$query->where('full_name', 'like', '%'.$searchString.'%');
},
])
->where('phone', 'like', '%'.$searchString.'%')
->orWhere('email', 'like', '%'.$searchString.'%')
->paginate(2);

Where clauses and search

I'm new in Laravel and Livewire. I have problem with my code, I'm trying to use where clauses and search in one query. I have code like this.
This is in my controller
public function read()
{
return Groom::query()
->search($this->search)
->orderBy($this->sortBy, $this->sortDirection)
->paginate($this->perPage);
}
and this one in my model
public function scopeSearch($query, $val)
{
return $query
->where('status', '<>', 'selesai')
->leftJoin('pets', 'pets.id', '=', 'grooms.pet_id')
->where('name', 'like', '%' .$val. '%')
->where(function ($query) use ($val) {
$query
->Orwhere('service', 'like', '%'. $val. '%')
->Orwhere('address', 'like', '%' .$val. '%')
->Orwhere('status', 'like', '%' .$val. '%');
});
}
There's no error there, but my search isn't working
Your generated SQL query is
select * from grooms
left join "pets" on "pets"."id" = "grooms"."pet_id"
where "status" <> 'selesai'
and "name" like ? and (
"service" like ?
or "address" like ?
or "status" like ?
)
order by ...
Perhaps you want the name filter to be inside that grouped where? Try
public function scopeSearch($query, $val)
{
return $query
->where('status', '<>', 'selesai')
->leftJoin('pets', 'pets.id', '=', 'grooms.pet_id')
->where(function ($query) use ($val) {
$query
->orWhere('name', 'like', '%' .$val. '%')
->orwhere('service', 'like', '%'. $val. '%')
->orwhere('address', 'like', '%' .$val. '%')
->orwhere('status', 'like', '%' .$val. '%');
});
}

How To Get Search Query From Multiple Columns in Database

I have search form to get information from table named books.
Right now i'm using this controller
public function search(Request $request)
{
$keyword = $request->input('keyword');
$query = Book::where('judul', 'LIKE', '%' . $keyword . '%');
$book_list = $query->paginate(5);
$pagination = $book_list->appends($request->except('page'));
$total_book = $book_list->total();
return view('dashboards.index', compact('book_list', 'keyword', 'pagination', 'total_book'));
}
The problem is the data that i get from the request only available for judul. it just show empty result if the input keyword search addressed to search writter or publisher
I want the search form able to get data from other columns named writters and publisher
Is there any method to get data from multiple column?
You can use orwhere to fullfill this, like this
Book::where(function ($query) use($keyword) {
$query->where('judul', 'like', '%' . $keyword . '%')
->orWhere('writters', 'like', '%' . $keyword . '%');
})
->get();
I hope it helps you.
You can execute conditional queries in many ways.
1. You can use when():
Book::when($keyword, function ($q) use ($keyword) {
return $q->where('judul', 'LIKE', '%' . $keyword . '%');;
})
->get();
2. Use the where closure:
Book::where(function($q) use ($keyword, $request) {
if ($request) {
$q->where('judul', 'LIKE', '%' . $keyword . '%');
}
})
->get();
3. Do this:
$books = Book::query();
if ($request) {
$books = $books->where('judul', 'LIKE', '%' . $keyword . '%');
}
$books = $books->get();

Laravel eloquent search on fields of related model

I have an eloquent models as,
User : users(id, username, password, email, status)
Profile : profiles(id, user_id, first_name, last_name, gender, dob)
In the controller logic, I am eagerly loading the Profile model.
I can do this,
$user = User::with('Profile')->get();
or
$user = User::with('Profile')->where('status', '1')->get();
but how to do something like,
$user = User::with('Profile')->where('status', '1')->where('gender', 'Male')->get();
That's where whereHas comes in handy:
$user = User::with('Profile')->where('status', 1)->whereHas('Profile', function($q){
$q->where('gender', 'Male');
})->get();
Basically it adds the condition that the user needs to have a profile with gender = Male
If you want to search multiple columns in relation model.
$searchText = 'test text';
Product::with('owner')->where(function($query) use ($searchText)
{
$query->where('product_name', 'LIKE', '%' . $searchText . '%');
$columns = ['product_code', 'place_location', 'remark'];
foreach ($columns as $column ) {
$query->orWhere($column, 'LIKE', '%' . $searchText . '%');
}
$query->orWhereHas('owner', function($q) use ($searchText) {
$q->where(function($q) use ($searchText) {
$q->where('name', 'LIKE', '%' . $searchText . '%');
$q->orWhere('company_name', 'LIKE', '%' . $searchText . '%');
});
});
});
Let's say you've multiple relations
and you want to search records based on multiple relational columns value
User::with('associate')
->where('name', 'like', '%' . $input . '%')
->orWhere(function ($query) use ($input) {
$query->whereHas('associate', function ($q) use ($input) {
$q->where('first_name', 'like', '%' . $input . '%');
});
})
->orWhere(function ($query) use ($input) {
$query->with('roles')->whereHas('roles', function ($q) use ($input) {
$q->where('display_name', 'like', '%' . $input . '%');
});
})
->get();
Suppose, Your search input field name is q.
function name(Request $request){
$query = User::select();
if($request->q && $request->q !=''){
// if you search
$keyword = $request->q;
$query = User::with('Profile')->whereHas('Profile', function($q) use
($keyword){
$q->where('gender', 'like', "%{$keyword}%" );
});
}
$query->latest('id')->paginate();
}

Laravel 4: A where and whereIn inside a whereHas using Eloquent

Laravel 4 - advanced Where
I'm trying to retrieve Posts that have a $keyword like a certain Companion and besides that I want to retrieve the Posts the have a linking title or content as the $keyword.
But when I try to use a where or whereIn inside a whereHas the query doesn't take these into account. When the state is 0 (not visible) or not inside the category 1 the Post item should not get selected.
$companion_id = Companion::where('name', 'LIKE', '%' . $keyword . '%' )->lists('id');
The code block below has to do two things:
Search for Post items with a title or content like the $keyword
and search for Post items that have Companions like the $keyword
code:
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', 1)
->whereIn('category_id', array(1));
})
->whereIn('category_id', array(1))
->orwhere('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' )
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
The code above retrieves data succesfully except for the where and whereIn parts inside the whereHas.
Who can help me out?
wrap your orWhere clauses in (..)
you don't need where and whereIn in the whereHas closure, since it queries companions table
you don't need whereIn for category_id, unless you want to pass multiple ids there
.
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id);
})
->whereIn('category_id', array(1)) // why not where(..) ?
->where(function ($q) use ($keyword) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
Thanks to Jarek Tkaczyk.
His answer was almost correct. All I had to do was wrap the where inside a orWhere. Now I get the Posts that has a Companion like the $keyword and I get the Posts that has the $keyword inside the content or title.
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', '1');
})
->orWhere( function($q) use ( $keyword ) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->whereIn('category_id', array(1, 3))
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();

Resources