How to query from two columns with one string in Laravel - laravel

I have two columns in the users table: first_name & last_name. I want to query the user search john doe. I attempted it like the following.
$users = User::where('username', $request->text)
->orWhere('first_name', 'LIKE', '%'.$request->text.'%')
->paginate(5);
It searches only through john and I don't understand how to make it work

Try the following...
$users = User::where('username', $request->text)
->orWhereRaw("CONCAT(first_name, ' ', last_name)
LIKE ?", ['%' . $request->text . '%'])
->paginate(5);
The whereRaw and orWhereRaw methods can be used to inject a raw where clause into your query.

Related

Eloquent: Order by sum of two columns

My working SQL query is as follows:
SELECT post_id
FROM news_tags
ORDER BY (link_clicks+views) DESC
What I've tried so far in eloquent is like this:
$newsTagSaved = NewsTag::
orderBy(DB::raw("`views` + `link_clicks`"), 'desc')
->paginate(12)
->pluck('post_id');
Newstag table has many columns and views and link_clicks are two of them. Now, I'm trying to retrieve the post_id order by desc of sum of views and link_click.
How can I do it in Laravel eloquent?
Thank you!
Do something like this:
$newsTagSaved = NewsTag::select(DB::raw('views + link_clicks as score'))
->orderBy('score', 'desc')
->get();
dd($newsTagSaved);
This will return just score value. If you want to return other fields, simply add them to select. As an example:
$newsTagSaved = NewsTag::select(
DB::raw('views + link_clicks as score'),
'title',
'created_at'
)
->orderBy('score', 'desc')
->get();
dd($newsTagSaved);

How to merge count and sum queries in one query in Eloquent

I have problem with queries in my Controller. I want to count rows and sum by column in one query.
Relations view
So far, I created two queries:
Query to count rows
$paymentsCount = Payments::where('pay_status', 'like', "%{$payStatus}%")->count();
Query to sum
$paymentsValue = Payments::where('pay_status', 'like', "%{$payStatus}%")->sum('brutto');
I have no Idea, how to make on query instead this two queries.
I will be grateful for any help.
The query builder allows you to customize your SELECT clause with the select() method.
Use it to add COUNT() and SUM() in addition of the other columns:
$payments = Payments::select('*', 'COUNT(*) AS count', 'SUM(brutto) AS sum')
->where('pay_status', 'LIKE', "%{$payStatus}%")
->get();

Using CONCAT with Eloquent in Laravel

I want to use concat with eloquent , I have two tables (users) and (cars)
From users table I want to use first_name , last_name fields, and from cars table I want to use field called (vin).
$cars = Car::where(DB::raw('concat(first_name," ",vin," ",make)') , 'LIKE' , '%$search%')->paginate();
first_name field from Users table and the rest from Cars table
You can use join() for this as:
$cars = Car::join('users', 'cars.user_id', '=', 'users.id')
->where(DB::raw('concat(users.first_name," ",cars.vin," ",cars.make)') , 'LIKE' , "%$search%")
->select('cars.*')
->paginate();

get both by ID and Name in laravel?

In MySQL Database Table there are 2 columns
Order TypeID
Order Type
In Order to find a record with ID, I am using below code.
$OrderType = \App\Models\OrderType_Model::find($request->input('OrderTypeID'));
Is there any way to get both by ID and Name ?
You can use a orWhere clause
$orderType = OrderType_Model
->where('OrderTypeId',$request->input('OrderTypeId))
->orWhere('OrderType', $request->input('OrderType))
->first();
$OrderType = \App\Models\OrderType_Model
::where("OrderTypeID", "!=", $request->input('OrderTypeID'))
->where("OrderType", '=', $request->input('OrderType'))->first();

subquery with distinct clause in query builder/laravel

SELECT * FROM `movie_list`
WHERE `movie_id` IN
(SELECT DISTINCT movie_id FROM `movie_genre` where genre_id in (12,18,53))
AND rated IN
('Not Rated','N/A')
How can i convert the above to a query builder syntax:
$movies = DB::table('movie_list')
->whereIn('movie_id',function($query){
$query->select.....
})->get();
I have the inner one: it goes like this:
DB::table('movie_genre')
->whereIn('genre_id', array(12,18,53))
->distinct()
->get(array('movie_id'));
How do i use this result with the rest of my query?
You could do this alot smoother with Eloquent Models, but assuming you don't have your models setup, this should do the trick (untested)
$ids = DB::table('movie_genre')
->whereIn('genre_id', [12,18,33])
->distinct()
->get(['movie_id'])
->toArray();
$movies = DB::table('movie_list')
->whereIn('movie_id', array_values($ids))
->get();

Resources