Laravellocalization search in different language - php

I'm trying to search in different language.
How i read in documentation i must use ' join ' on translations but it don't working.
Every search works except translations.
Controller:
public function webSearch(Request $request)
{
$translations = \DB::table('articles')->join('translations')->where('value', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$articles = Article::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$episodes = Episode::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$quizzes = Quizze::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$shows = Show::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
return view('pages/search')
->with('articles', $articles)
->with('episodes', $episodes)
->with('quizzes', $quizzes)
->with('shows', $shows)
->with('translations', $translations);
}

Join should be used Like this:
The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join.
$translations = \DB::table('articles')
->join('translations','articles.id','=','translations.article_id')
->where('value', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();

Related

Joining two models in Laravel Eloquent

I have a model stored in a variable like:
$user = User::where('name', 'like', $test)
->orderBy('name');
I would like to build another query where I can join $user. Can't find the right syntax to do it.
What am trying to achieve:
$numbers= Number::join($user, $users.id, '=', 'number.user_id')
->where('name', 'like', "%" . $validated['text'] . "%")])
->get();
Assuming you have typical hasMany/belongsTo relationships set up between User and Number models, this should work:
User::where("name", "like", $test)
->whereHas("numbers", function($q) {
$q->where("name", "like", "%$validated[text]%");
})
->with("numbers", function($q) {
$q->where("name", "like", "%$validated[text]%");
})
->get();
The where() method, of course, matches users with the desired name. The whereHas() method further restricts based on the relationship, looking only for users having numbers with a matching name. Assuming you want to retrieve those matching numbers, you have to do the same filter again on the eager load.
Try this,
$numbers= Number::whereHas('<Your Eloquent Model>',function($query)use($validated){
$query->where('name', 'like', "%" . $validated['text'] . "%")]);
}
->get();
Join can be written in this way
$records = User::select('users.*')
->where('users.name', 'like', $test)
->join('numbers', function($join) {
$join->on('users.id', '=', 'numbers.id')
->where('numbers.name', 'like', "%" . $validated['text'] . "%");
})
->get();

I want to get all images which either have a word in their name, as one of their tags or as category

I'm trying to add search functionality to my website and have been tinkering with the eloquent queries I'd need to execute. Currently I've made 3 queries and each gets images meeting a certain criteria, however, I'm not sure how to combine it into 1 query that would spit all images that meet one, two or all criteria.
$images = Image::where('name', 'like', '%'.$query.'%')->get();
This query gets all images that have a name similar to the searched word.
$images = Image::whereHas('tags', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
This query gets all images that have a tag similar to the searched word.
$images = Image::whereHas('category', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
And finally, this query gets all images that have a category similar to the searched word.
public function search($query){
$images = Image::where('name', 'like', '%'.$query.'%')->get();
$images = Image::whereHas('tags', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
$images = Image::whereHas('category', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
return view('search', ['images' => $images]);
}
Is this the correct way to create a search functionality? Is there anything I could do to enhance it further? Is there some obvious problem that I personally might be overseeing? I'd appreciate any tips and tricks since I believe the search functionality is important for CRUD applications like mine.
Merge the constraint into a single query:
$images = Image::where('name', 'like', '%'.$query.'%')
->orWhereHas('tags', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orWhereHas('category', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->latest()
->get();
latest() is equivalent to orderBy('created_at', 'desc').

Laravel search with multiple `where`s

I'm trying to do a search using where in po website.
My code:
public function globalSearch($subject) {
$users = User::where('username', 'LIKE', '%'.$subject.'%')->get();
$users = User::where('steam_id', 'LIKE', '%'.$subject.'%')->get();
$results = array();
foreach ($users as $user) {
$results[$user->username] = url('player/'.$user->slug);
}
return $results;
}
But it's only searching for steam_id and if I put username it does not find anything. I think the problem is, that I need to use multiple wheres in 1 line. Sorry if you don't understand what I mean but how can I use 1 where with multiple fields?
Below two lines are overriding your search.So remove that two lines and write it as below.
$users = User::where('username', 'LIKE', '%'.$subject.'%')->get();
$users = User::where('steam_id', 'LIKE', '%'.$subject.'%')->get();
User below code.
$users = User::where('username', 'LIKE', '%'.$subject.'%')->orwhere('steam_id', 'LIKE', '%'.$subject.'%')->get();
You're overwriting $users with that second statement. If you want a result with both where clauses, you can use an array as options:
$users = User::where([
['username', 'LIKE', '%'.$subject.'%'],
['steam_id', 'LIKE', '%'.$subject.'%']
])->get();
More on Laravel where clauses.
Will this do the job?
public function globalSearch($subject)
{
return User::where('username', 'LIKE', '%'.$subject.'%')->orWhere('steam_id', 'LIKE', '%'.$subject.'%')->get();
}
Ideally group by the table index to not get unexpected results.
You can either use an array inside of the User::where() like #kerbholz answers states correctly. Or you can go for a chaining like so:
User::where('foo', 1)->where('bar', false)->get();
Which will result in the same result as the answer above but in my opinion is usually more readable.

How to return query from variable?

I try to search first_name, last_name and city but city is now always set whether the users want to set it with city or not. so I make query like this if request has city then filter it
$users = \App\User::join('profiles', 'users.id', '=', 'profiles.member_id')
->where(function($query) {
$query->where('first_name', 'like', '%z%')
->orWhere('last_name', 'like', '%z%');
})
->paginate(10);
if ($request->has('city')) {
$users->where('location', 'like', '%bandung%');
}
but where location like its like never used even request has city. i've try var_dump $users and count it and end up with no city query
Do little change in sequence of your lines of code:
$users = \App\User::join('profiles', 'users.id', '=', 'profiles.member_id')
->where(function($query) {
$query->where('first_name', 'like', '%z%')
->orWhere('last_name', 'like', '%z%');
});
if ($request->has('city')) {
$users->where('location', 'like', '%bandung%');
}
$users_data = $users->paginate(10);
The variable $users_data will be having proper result.

search function look for parameter's value in related table

I have 2 tables, merchants and transaction. merchants has (mid,name,age) and transaction has (id,mid,order_no,). I have displayed the data like (orderid,name) with eagerloading. now i have a search function on top where , i can search for all the orders by the merchant's name. how do i do that? im' trying something like this
public function search($search){
$query = Transaction::with('themerchant')
->join('merchants','transactions.mid', '=', 'merchants.mid' )
->select('merchants.name', '=' , '%'.$search.'%')->paginate(10);
return $query;
}
Oh you are using eagerLoading
Do this
public function search($search){
return Transaction::with(['themerchant' => function($query){
$query->where('name', 'like', '%'.$search.'%');
}])->paginate(10);;
}
If you want an exact merchant match change the where like this
...
->where('name', '=' , $search)
...
If you want to use Join
public function search($search){
return Transaction::join('merchants', 'merchants.mid', '=', 'transactions.mid')
->where('merchants.name', 'like', '%'.$search.'%')
->get();
}

Categories