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();
Related
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.
For a search query I have the following:
DB::whereRaw('column = ?', 'foo')->orWhereRaw('column IS NULL')->get();
Adding the orWhereRaw statement gives me less results than only the whereRaw. Somehow it seems to ignore the first when adding the other. It is included in the SQL statement. Is there another way to compare for a string and null value?
I have also tried the following, as suggested below:
return self::select('id')
->where('current_state', 'unavailable')
->orWhereNull('current_state')
->get();
If I change the order (the whereNull first and the where second) this also gives me different results. It appears as if the inclusive query doesn't function correctly in correspondence with the where clause. If I use to regular where clauses I don't experience any issues.
Running SELECT * FROM events WHERE current_state='unavailable' OR current_state IS NULL; does produce the correct result for me.
Don't use whereRaw to check for null. You can use this instead:
->orWhereNull('column')
The proper way to do the first where, unless you're doing something extra such as a mysql function, is just to pass the column along like this:
where('column', '=', 'foo')
You can actually eliminate the equals, since it defaults to that. So your query would be:
DB::table('table')->where('column', 'foo')->orWhereNull('column')->get();
How can I get date from "2015-07-31 06:03:21" using Laravel query builder?
In normal query using DATE() we can get date easily, but I don't know how to use DATE() in Laravel query builder. Please check my code sample given below and correct me?
Normal Query
SELECT DATE('2015-07-31 06:03:21'), customer_id FROM customer_histories
Laravel Query Builder
$customerhistory = Customerhistory::where('customer_id', 1)
->select('freetext', 'DATE(created_at)')
->get();
You can do this with DB::raw(), though if you are only selecting it, why not just take created_at complete? By default, Eloquent will convert the created_at and updated_at columns to instances of Carbon so you can handle it like this in your code:
$customerhistory = Customerhistory::where('customer_id', 1)
->select('freetext', 'created_at')
->get();
dd($customerhistory[0]->created_at->toDateString());
You can use DB::raw('something') for that. In this case your raw input will be treated as part of actual SQL.
This is something you might wanna give a try:
$customerhistory = Customerhistory::where('customer_id', 1)
->select('freetext', DB::raw('DATE(`created_at`)'))
->get();
More details and yet another example cal be found here.
Also it looks like you're using eloquent. You might wanna check the mutators section.
$customerhistory = Customerhistory::where('customer_id', '=', '1')
->select('freetext', DB::raw('DATE(`created_at`)'))
->get();
Thanks.
In Laravel
->whereBetween('usage.created_at', array('subscriptions.created_date', 'subscriptions.end_date'))
It gives query as
`usage`.`created_at` between 'subscriptions.created_date' and 'subscriptions.end_date'
But I am expecting the query to treat the subscriptions.created_date and subscriptions.end_date as column itself instead of string value
`usage`.`created_at` between subscriptions.created_date and subscriptions.end_date
Removed the quotes from subscriptions.created_date and subscriptions.end_date.
From their docs I got this but not suitable for my situation as I want column names instead of 1 and 100
$users = DB::table('users')
->whereBetween('votes', array(1, 100))->get();
How can I achieve this. I am New to Laravel.
Alternatively, you can do this with two whereRaw():
->whereRaw('usage.created_at >= subscriptions.created_date')
->whereRaw('usage.created_at <= subscriptions.end_date');
But if you really want to use whereBetween, I don't know :/
I believe you would have to use DB::raw
->whereBetween('usage.created_at', array(DB::raw('subscriptions.created_date'), DB::raw('subscriptions.end_date')))
I heed two variables storing the maximum id from a table, and the minimum id from the same table.
the first id is easy to be taken ,using find() and a query like
$first = Model::factory('product')->sale($sale_id)->find();
but how can i retrieve the last id? is there a sorting option in the Kohana 3 ORM?
thanks!
Yes, you can sort resulting rows in ORM with order_by($column, $order). For example, ->order_by('id', 'ASC').
Use QBuilder to get a specific values:
public function get_minmax()
{
return DB::select(array('MAX("id")', 'max_id'),array('MIN("id")', 'min_id'))
->from($this->_table_name)
->execute($this->_db);
}
The problem could actually be that you are setting order_by after find_all. You should put it before. People do tend to put it last.
This way it works.
$smthn = ORM::factory('smthn')
->where('something', '=', something)
->order_by('id', 'desc')
->find_all();
Doing like this, I suppose you'll be :
selecting all lines of your table that correspond to your condition
fetching all those lines from MySQL to PHP
to, finally, only work with one of those lines
Ideally, you should be doing an SQL query that uses the MAX() or the MIN() function -- a bit like this :
select max(your_column) as max_value
from your_table
where ...
Not sure how to do that with Kohana, but this topic on its forum looks interesting.