Laravel 5.2 version of whereDate - php

What is laravel 5.2 version of the below condition:
->whereDate('created_at', '=', $sel_date)
Please note that $sel_date is in the below format :
$sel_date = date('Y-m-d');

Laravel whereDate method generates the following query:
.... WHERE DATE('created_at') = 'Y-m-d date here'
You can get the same query by using a raw where:
->where(\DB::raw("DATE(created_at) = '".$sel_date."'"));
UPDATE
A good approach is to use bindings in raw queries so the right way to write the query would be this:
->where(\DB::raw("DATE(created_at) = '?'", [$sel_date]));
When the variable $sel_date is created from you in code is not a problem using first approach, but when it is a user input can cause a SQL Injection if you do not use bindings ore do not sanitize user input.

Related

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.

What is meant by whereRaw in PHP Laravel framework

I'm unsure what whereRaw is in PHP Laravel framework. Could you provide good and easily understandable example, please?
WhereRaw() is a function of Laravel query builder which puts your input as it is in the SQL query's where clause.
Think of it as the where() function whose input argument will not be processed before inserting into queries.
See the example below:
$Query = DB::table('some_table')->where('YEAR(date)', 'YEAR(CURRENT_DATE)');
In this Laravel will resolve your arguments to build a query. Which will result in the following query because your input will be treated as some field and its its value :
SELECT * FROM `some_table` WHERE `YEAR(date)` = `YEAR(CURRENT_DATE)`
Which is not desired.
And now if you use whereRaw like:
$Query = DB::table('some_table')->whereRaw('YEAR(date) = YEAR(CURRENT_DATE)');
Now Laravel put this where clause as it is in your query, like below:
SELECT * FROM `some_table` WHERE YEAR(date) = YEAR(CURRENT_DATE)
Hope it helped (:
WhereRaw: Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings.
If you are unable to generate the query you need via the fluent interface, feel free to use whereRaw()
Ex:
$users = User::whereRaw('age > ? and votes = 100', array(25))->get();
which is equals to:
"SELECT * FROM users WHERE age > 25 AND votes = 100";
Reference
In laraval we use query builder. But Sometime you need to execute raw sql query.
So you can inject it to whereRaw as this example :
whereRAW('YEAR(event_datetime) =?', [$year])

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();

Get date from timestamp in laravel

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.

syntax between inside syntax like in php(laravel)

i wanna make a query to get start date and end date using between,
and start_date & end_date is an input from user so i use syntax like '%'.
here my code, but not working
public function getData()
{
$inputname = Input::get('searchname');
$startdate = Input::get('tglmulai');
$enddate = Input::get('tglsiap');
$name = DB::table('tr_visit')
->join('tm_child','tr_visit.Child_ID','=','tm_child.Child_ID')
->select('tm_child.Child_Name','Bmi_Score')
->where('tm_child.Child_Name', 'LIKE', '%'.$inputname.'%')
->whereBetween('Visit_Date', 'LIKE', '%'.$startdate.'%' and 'LIKE', '%'.$enddate.'%')
->get();
return view('Laporan.Kehadiran.kehadiranview', compact('name'));
}
Please always check the documentation for code examples using the methods you wanted.
Where Between
Notably, the whereBetween http://laravel.com/docs/4.2/queries#advanced-wheres
I havent used that particular method myself, but it looked off so i found the doco and saw this:
$users = DB::table('users')
->whereBetween('votes', array(1, 100))->get();
I cant run your code to be certain, but it seems like this might work for you:
public function getData()
{
$inputname = Input::get('searchname');
$startdate = Input::get('tglmulai');
$enddate = Input::get('tglsiap');
$name = DB::table('tr_visit')
->join('tm_child','tr_visit.Child_ID','=','tm_child.Child_ID')
->select('tm_child.Child_Name','Bmi_Score')
->where('tm_child.Child_Name', 'LIKE', '%'.$inputname.'%')
->whereBetween('Visit_Date', array($startdate, $enddate))
->get();
return view('Laporan.Kehadiran.kehadiranview', compact('name'));
}
This is assuming the ->select() and other methods are work without issue (take out the whereBetween and confirm its working without it first ofcourse).
Wildcards Note
You do realise that '%' is just a wildcard character for LIKE.
It doesn't have anything to do with user input, its just to match any values containing the substring between the '%'.
SQL Wildcards: http://www.w3schools.com/sql/sql_wildcards.asp
DateTime strings in SQL Between
In this situation, you should really be using a datepicker or similar in your html, or be sure to cast the input (which you havent specified the format of) to a format for sql to pick up on.
The SQL Between documentation shows the following example:
SQL Between: http://www.w3schools.com/sql/sql_between.asp
SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;
So a plain string should work, just be certain its of the same format as the database.
There is some on this (using direct sql) here: SQL query to select dates between two dates
How you achieve this format in the html (or client-side js) is one of many many approaches, but there are many easy to use javascript datepickers out there, even if not using jquery or other library.

Categories