Multiple like in a laravel mongo db package not working - php

I have a project which is creating in laravel and mongodb. I am using a package for connecting to mongodb "https://github.com/jenssegers/laravel-mongodb". I have a table listing. In this table listing there is a text box for search data. For that I used like feature. I have followed the same in documentation but not working. When I search invoice_number data is getting empty. But when I search beds data is getting perfectly. Please correct me.
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%")
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();

The WHERE statements in your query translate to this:
WHERE [cond_1] AND [cond_2] OR [cond_3]
I doubt this is the condition you need. I'd say you want this:
WHERE [cond_1] AND ([cond_2] OR [cond_3]) -- notice the brackets.
To achieve this, you need a closure in your Builder query. Try this:
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where(function($query) use ($search) { /* That's the closure */
$query->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%");
})
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();
And an example from the docs.

Related

Laravel where clause is not returning data after multiple orWhere clauses?

I am using laravel 6.10 version in that I am implementing search,
I have 3 tables
1) course_category
2) course_sub_category
3) course [course_id_category(foreign key),course_sub_category_id(foreign key)]
below is my code
$course = $request->searchItem;
if ($course=="") {
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->orderBy('course.title','asc')
->paginate(15);
}
else{
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where('course.title', 'LIKE', '%'.$course.'%')
->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
->get();
}
when i am retunring the values i am gatting 0 arrays but when i am removing one orwhere from existing my query is working and its returnig we all values with match
means when i am using multiple orWhere in laravel my where is not working, please share the solution on same.
->orWhere doesnt work like typical SQL. You should use it like that:
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where(function($query) use ($course){
$query->where('course.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
})
->get();

How to get first entry from the record in Laravel relation?

I got this query, it fetch results from my database, based on relationships.
The query works fine, but it grabs the first entry from the database/table and not the first value of the array.
How can I fix that?
$motor = Motor::query()
->where([
['effect_id', '=', $request->effect_id],
['poles', '=', $request->poles],
['voltage_id', '=', $request->voltage_id],
['active', '=', 1],
])
->whereHas('MotorSize', function (Builder $query) {
$query->whereIn('mounting', ['B5', 'B14', 'B34', 'B35']);
})
->firstOrFail();
I have tried different things, such as:
Adding the orderBy to the Motor model and created a "priority" field in my database, giving each a number between 1 and 100:
(i actually thought this would work, but it still grabs the first entry from the table)
public function MotorSize()
{
return $this->belongsTo('App\MotorSize')->orderBy('priority', 'ASC');
}
I also tried something like this, but it's a mess and never got it to work:
$query->where(function ($query) {
$query->where('mounting', 'like', 'B5');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B14');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B34');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B35');
});
Any suggestions will be welcome :)

Laravel 5.4 Query Builder Implementing orderBy with groupBy

I wanted to manipulate how data will be shown with cost layering methods such as LIFO and FIFO.
To get things started, here's my query:
$items = Inventory::whereActive('1')
->where('quantity', '>', 0)
->where(function($query) use($request) {
$query->where('name', 'LIKE', '%'.$request->search.'%')
->orwhere('ref_code', 'LIKE', '%'.$request->search.'%');
})->orderBy('created_at', 'dsc')
->groupBy('ref_code')
->get();
I wanted to use groupBy with ref_code so it will only show one data with the same ref_code, and I used orderBy with created_at and dsc for descending so that one data that will be shown will be the last one that was added. (Well, that's what I think that would happen).
But unfortunately, when I used groupBy, it only shows the first added data. How can I select the last data? I thought orderBy was gonna do the job but it didn't.
You can use an alternative way for this. If you have auto incrementing primary key as id then you can do it like,
$ids = Inventory::whereActive('1')
->where('quantity', '>', 0)
->where(function($query) use($request) {
$query->where('name', 'LIKE', '%'.$request->search.'%')
->orwhere('ref_code', 'LIKE', '%'.$request->search.'%');
})
->select('ref_code',\DB::raw('MAX(id) as ID'))
->groupBy('ref_code')
->pluck('ID');
Now, Get Items as
$items = Inventory::whereIn('id',$ids)->get();
Hence you will get your desired result.

How can we write multiple column search with the help of laravel and mongo?

In my laravel project, I have search field in listing page to search email, firstname and lastname but when I type firstname and lastname sometimes result is not getting. Database I am using is mongo. I am using a package to connect mongo "https://github.com/jenssegers/laravel-mongodb". I think for better result we need to use raw method right? If so how can I add mongo query with raw(). Please help me I am a beginner in mongo.
$users = Userlist::select('_id', 'usrEmail', 'usrFirstname', 'usrLastname')
->where(function($query) use ($search) {
$query->where('usrEmail', 'like', "%{$search}%")
->orWhere('usrFirstname', 'like', "%{$search}%")
->orWhere('usrLastname', 'like', "%{$search}%");
})
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();

Laravel Eloquent Search Multiple Related Tables

I am struggling with figuring out how to run a like query against multiple related tables.
I have a submissions table that has related users, mcd_forms, and submission_statuses tables.
Here is my code for running a LIKE statement with the given $terms_like.
$submission = new Submission;
$terms_like = '%'.$search_terms.'%';
$data['submissions'] = $submission
->join('users as users', 'users.id', '=', 'submissions.user_id')
->join('mcd_forms as forms', 'forms.submission_id', '=', 'submissions.id')
->join('submission_statuses as statuses', 'statuses.id', '=', 'submissions.submission_status_id')
->where(function($q) use ($terms_like) {
$q->where('users.user_group_id', '=', Auth::user()->user_group_id)
->orWhere('forms.name', 'like', $terms_like)
->orWhere('forms.custom_id', 'like', $terms_like)
->orWhere('forms.start_date', 'like', $terms_like)
->orWhere('forms.end_date', 'like', $terms_like)
->orWhere('forms.soft_sell_date', 'like', $terms_like)
->orWhere('forms.region', 'like', $terms_like)
->orWhere('statuses.status_formatted', 'like', $terms_like);
});
No matter what I try it returns incorrect results. What am I doing wrong?
In your query above, since you are not using the "%" symbol, your like clause is working as an "=" since it's trying to match the whole word.
Replace all the "where" clause like this:
->orWhere('forms.name', 'like', "%".$terms_like."%")
This will try to match the word anywhere in the text.
You can try 'like' operator following this:
$users = DB::table('users')
->where('forms.name','LIKE', '%'.$terms_like.'%')
->get();

Categories