whereraw Vs DB::raw in Laravel - php

I am confused and really don't know how and where should I choose to use one from both ?
I read docs for both
https://laravel.com/docs/5.4/queries#where-clauses
And
https://laravel.com/docs/5.4/queries#raw-expressions
If I use query something like this its not working
DB::table('table_name')
->where('parent_id', $parent_id)
->whereRaw("date",">",$date)
->get();
But it works
DB::table('table_name')
->where('parent_id', $parent_id)
->where(DB::raw("date",">",$date))
->get();

DB::raw() lets you write raw statements as a part of the query. Eg:
->where(DB::raw('DATE(date_column)'), '>', '2017-01-01')
But if you need to write a complete "raw where", you should use whereRaw (for your convenience). Eg:
->whereRaw('DATE(date_column) > DATE(another_date_column)')
Also, whereRaw() accepts a complete where clause.
So, in your first example it's not working, because you should do it:
->whereRaw("date > ".$date)
And your second example could be simplified by using just whereRaw() like the above statement in my answer.
Also DB::raw() can be used in ->select(),groupBy() and others.

Related

I want to use orderBy ()and take() after where(), but it is querying just oppsite to my desired answer

Let's assume an Event table, I want to get the first latest event which will be held after current_date and I ordered it by event_start_date.
But it is querying the first orderBy and take then running condition over it.
EVENT::where(Event::START_DATE, '>', 'CURDATE()')->orderBy(Event::START_DATE)->take($nThEvent)->get()->toArray();
Does this work for you
EVENT::where(Event::START_DATE, '>', 'CURDATE()')->latest('START_DATE')->first();
You should use whereRaw() if you have to call sql functions, e.g.:
EVENT::whereRaw(Event::START_DATE.' > CURDATE()')
->latest(Event::START_DATE)
->take($nThEvent)
->get()->toArray();
BTW you can always debug your query with toSql(), it returns a string with the generated SQL:
EVENT::whereRaw(Event::START_DATE.' > CURDATE()')
->latest(Event::START_DATE)
->take($nThEvent)
->toSql();
Please note that latest(Event::START_DATE) is equal to write orderBy(Event::START_DATE, 'desc').
Update: if you want to get only one record you should use first() not get() that returns instead a collection, e.g.:
EVENT::whereRaw(Event::START_DATE.' > CURDATE()')
->latest(Event::START_DATE)
->first()

i want to use both query in the following code when is used orWhereNull i am getiing error how i can make my code to run

I want to get the null expiry date result. My whereRaw is working fine but when I used orWhereNull, I get an error. Here is my code:
$offer_details = #\App\Offer::where('store_id',$store_id)->whereRaw('expiry_date > now()')->orWhereNull('expiry_date ')->get();
Following query should work for you as, I don't thing 'orWhereNull()' available in laravel :
$offer_details = #\App\Offer::where('store_id',$store_id)->
->whereNull('expiry_date')
->orWhereRaw('expiry_date > now()')
->get();
Unlike that the "or" variant of 'whereRaw()' is availble as 'orWhereRaw()'.
More details of : whereRaw / orWhereRaw
The whereRaw and orWhereRaw methods can be used to inject a raw where clause into your query. WhereRaw() is a function of Laravel query builder which puts your input as it is in the SQL query's where clause.

Laravel: WhereNull, OrWhere, is ignoring WhereRaw

In my controller I am using Laravels WhereNull, OrWhere and WhereRaw in DB Query. It is pulling in all the results but the problem is it looks like it is pulling in everything and ignoring the last Where clause. I have used this in a different method on other controllers and it works fine. Is there a specific order or something I am missing?
Doesn't Work (Ignores WhereRaw and shows all results)
$lists = DB::table('statuses')
->whereNull('company_id')
->orWhere('company_id', '=', Auth::user()->company_id)
->whereRaw("FIND_IN_SET('Task',assigned_to)")
->get();
This works in other controllers used as a different method without the whereRaw:
return Status::whereNull('company_id')->orWhere('company_id', '=', Auth::user()->company_id)
->orderBy('created_at', 'asc')
->get();
Sample DB
Source link: https://laravel.com/docs/5.3/queries#where-clauses
Use DB::raw instead of whereRaw
->where(DB::raw("FIND_IN_SET('Task',assigned_to)"))

Laravel 4 unable to join on multiple conditions using ->on( ) more than once

In a nutshell I am trying to do a join with more than one condition. We are using legacy Laravel 4, and the actual class I've tracked it to is Illuminate\Database\Query\Builder. Here is what I am adding:
->leftJoin('node_fields AS visible_for_categories', function($join){
$join->on('nv2.id', '=', 'visible_for_categories.node_version_id');
$join->on('visible_for_categories.name', '=', 'visible_for_categories');
})
It works fine with the first $join->on( ) call, but the page fails if the second on is called. why is this and what is the proper way to do this in Laravel 4?
The way that worked for me on the above query is as follows:
->leftJoin('node_fields AS visible_for_categories', function($join){
$join->on('nv2.id', '=', 'visible_for_categories.node_version_id');
$join->on('visible_for_categories.name', '=', DB::raw("'visible_for_categories'"));
})
The query builder will assume all three values in the on() function are fields, not strings, and will parse out the . period as well.
The general assumption is that JOINS will have the relational field joins to create structure, and the WHERE conditionals will provide the desired filter. However anyone who's worked esp. with LEFT or RIGHT joins knows this is not always possible.
Be careful for SQL injection using DB::raw but in this case, an EAV table, we're dealing with a fixed string, not a variable.
Try
->leftJoin('node_fields AS visible_for_categories', function($join){
$join->on('nv2.id', '=', 'visible_for_categories.node_version_id')
->on('visible_for_categories.name', '=', 'visible_for_categories');
})

Comparing two columns in Laravel 5

I am using Laravel
Let's say, I have two date fields in my table. If I want to compare them, i can do whereRaw clause.
$query->whereRaw('date1 > date2')->get()
Is there a way to make a modification to date2 inside this query, so it is actually date2-1day?
Something like:
$query->whereRaw('date1 > (date2-1day)')->get()
You are free to call any SQL code in the "raw" part of your query, so you could do sth like below:
$query->whereRaw('date1 > DATE_SUB(date2, INTERVAL 1 DAY)')->get();
Keep in mind that executing SQL code this way will make your queries work only in databases that support such functions.
Another way would be using whereColumn like
$users = DB::table('users')
->whereColumn('updated_at', '>', 'created_at')
->get();
OR
UserTable::whereRaw('column1 != column2')->get();

Categories