I'm a noob in Laravel. can anyone help me write this query in eloquent
SELECT
*
FROM
table
WHERE
(
STR_TO_DATE(`date`, '%m/%d/%Y') BETWEEN '2014-08-05'
AND '2014-08-05'
)
ORDER BY
id
If you want to use your query as is, just use DB::raw
http://laravel.com/docs/queries#raw-expressions
DB::raw(SELECT * FROM table WHERE ( STR_TO_DATE(date, '%m/%d/%Y') BETWEEN '2014-08-05' AND '2014-08-05' ) ORDER BY id);
Well making the assumption that your model is called Table, if your field is of type DATE, you can do this:
Table::where('date', '>=', '2014-08-05')
->where('date', '=<', '2014-08-05')
->get();
Alternatively you can do:
Table::select('table.*', DB::raw("STR_TO_DATE(date, '%m/%d/%Y') as date_format"))
->where('date_format', '>=', '2014-08-05')
->where('date_format', '=<', '2014-08-05')
->get();
You may try this:
$from = '...';
$to = '...';
DB::table('table')->whereBetween('date', array($from, $to))->get();
Or using Eloquent:
ModelName::whereBetween('date', array($from, $to))->get();
Related
My eloquent query:
$data = (new Model())->whereDate('start_date', '<=', Carbon::today())
->whereDate('end_date', '>=', Carbon::today())
->count();
The raw query I have tried:
$data = (new Model())->select(DB::raw("COUNT(id) as countData where date(start_date) <= NOW() and date(end_date) >= NOW()"))->get();
How can I write my eloquent query as raw query. The below raw query gives me syntax violation error;
This is because of how you are constructing your query. If you inspect the generated SQL statement you'll see that the SQL FROM clause is actually on the end of your statement and not at all where it should be.
You'll want to split your WHERE clauses out and use either where or whereRaw. For example:
$data = (new Model)->select(DB::raw('COUNT(id) as countData'))
->whereRaw('date("start_date") <= NOW() AND date("end_date") >= NOW()')
->get();
Your question was a little unclear but if you want to do what you did with eloquent in raw query, be noted that the select function of DB in raw by itself, so simply write your query inside select function:
$tableName = (new Model)->getTable();
$data = DB::select("
SELECT COUNT(`id`) as `countData` FROM `$tableName`
WHERE DATE(`start_date`) <= NOW() AND DATE(`end_date`) >= NOW()
")->first();
Alternatively you may use whereRaw function of your Model:
$data = Model::whereRaw(
"DATE(`start_date`) <= NOW() AND DATE(end_date) >= NOW()"
)->count();
I have a structure table like this : Start_date End_date
Now with today's date, I should go and get all the data including from Start_Date and End_Date, what can I do?
I tried with
SELECT *
FROM text
WHERE Start_date BETWEEN '2021-10-10' AND '2021-10-08'
OR End_date BETWEEN '2021-10-08' AND '2021-10-10
but to no avail ...
The whereBetween method in Laravel will determine if an items value is between the given range.
$start = '2021-10-08';
$end = '2021-10-10';
Text::whereBetween('start_date', [$start, $end])
->whereBetween('end_date', [$start, $end])
->get();
if you just want to get data from start to end date you can use this eloquent Code
$start_date = "2020-10-20";
$end_date = "2021-10-20";
Text::where(function ($wh) use ($start_date, $end_date) {
$wh->whereBetween('Start_date', [$start_date, $end_date])->orwhereBetween('End_date', [$start_date, $end_date]);
})->get();
here is a raw query from the above eloquent code
select * from `users` where (`Start_date` between '2020-10-20' and '2021-10-20' or `End_date` between '2020-10-20' and '2021-10-20')
if you want to find data from start to end date including today's date you can you this eloquent Code
$start_date = "2020-10-20";
$end_date = "2021-10-20";
Text::where(function ($wh) use ($start_date, $end_date) {
$today_date = date('Y-m-d');
$wh->where(function ($or_where) use ($start_date, $end_date) {
$or_where->whereBetween('Start_date', [$start_date, $end_date])
->orwhereBetween('End_date', [$start_date, $end_date]);
})->orwhereRAW("('{$today_date}' between `Start_date` and `End_date`)");
})->get();
here is a raw query from the above eloquent code
select * from `text` where ((`Start_date` between '2020-10-20' and '2021-10-20' or `End_date` between '2020-10-20' and '2021-10-20') or ('2021-10-09' between `Start_date` and `End_date`))
I want to increment +1 the value i get with the query.
The query is this:
$project->order = DB::table('projects')
->where('order', DB::raw( "( select max(`order`) from projects )" ))
->get();
I think i should do something with increment, or just put +1 in the query, but not.
What's the better way to do it?
Thanks !
Try this :
$project->order = DB::table('projects')
->where('order', DB::raw("(select max(`order`) from projects)"))
->first()
->order + 1;
I have this query that I am having trouble to write query in laravel eloquent ORM.
Appreciate if someone can help.
Here is SQL Expression:
SELECT DISTINCT cust, cust_no FROM delivery_sap
WHERE cust NOT IN ( SELECT cust_name FROM customer)
AND cust_no NOT IN ( SELECT cust_code FROM customer)
Instead of executing 3 different queries you can use like shown below,
DB::table('delivery_sap')
->whereNotIn('cust', function ($query) {
$query->select('cust_name')->from('customer');
})
->whereNotIn('cust_no', function ($query) {
$query->select('cust_code')->from('customer');
})
->select('cust', 'cust_no')
->distinct('cust')
->get();
This code will give the exact same query which is asked in the question,
to check the query, use following code
DB::table('delivery_sap')
->whereNotIn('cust', function ($query) {
$query->select('cust_name')->from('customer');
})
->whereNotIn('cust_no', function ($query) {
$query->select('cust_code')->from('customer');
})
->select('cust', 'cust_no')
->distinct('cust')
->toSql();
Output will be,
select distinct `cust`, `cust_no` from `delivery_sap`
where `cust` not in (select `cust_name` from `customer`)
and `cust_no` not in (select `cust_code` from `customer`)
Try Something like this:
DB::table('delivery_sap')
->whereNotIn('cust', DB::table('customer')->pluck('cust'))
->whereNotIn('cust_no', DB::table('customer')->pluck('cust_no'))
->select('cust', 'cust_no')
->groupBy('cust', 'cust_no')
->get();
I corrected the code below pluck('cust') to pluck('cust_name') and
pluck('cust_no') to pluck('cust_code') and it works
DB::table('delivery_sap')
->whereNotIn('cust', DB::table('customer')->pluck('cust_name'))
->whereNotIn('cust_no', DB::table('customer')->pluck('cust_code'))
->select('cust', 'cust_no')
->groupBy('cust', 'cust_no')
->get();
You could use exists or left join for better performance instead of sub queries on same table like in existing solution, there is no need for these 2 extra sub queries
SELECT DISTINCT cust, cust_no
FROM delivery_sap d
WHERE EXISTS (
SELECT 1
FROM delivery_sap
WHERE cust_name = d.cust OR cust_code = d.cust
)
OR
SELECT DISTINCT d.cust, d.cust_no
FROM delivery_sap d
LEFT JOIN delivery_sap d1 ON d.cust = d1.cust_name OR d.cust = d1.cust_code
WHERE d1.cust IS NULL
DB::table('delivery_sap as d')
->leftJoin('delivery_sap as d1', function ($join) {
$join->on('d.cust','=','d1.cust_name')
->orWhere('d.cust', '=', 'd1.cust_code');
})
->whereNull('d1.cust')
->select('cust', 'cust_no')
->distinct()
->get();
I am trying to convert a MYSQL query to codeigniter and going no wheres real fast. I am trying to convert this query
$conn->prepare("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc");
This is in my model:
//$where = '(SELECT MAX(mix_number)from podcasts)';
$this->db->select('id,song,artist,album,track,mix_name,date, link');
//$this->db->where('mix_number', '(SELECT MAX(mix_number)from podcasts)');
$this->db->order_by('track', 'asc');
$query = $this->db->get('podcasts');
return $query->result();
My problem area is in the where statement. When I comment out the where statement I get the data. Obviously not in the manner I want it.
I am doing it this way becuase my next query(s) will be
("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = **(SELECT MAX(mix_number) FROM podcasts) - 1** order by track asc")
And on down to (SELECT MAX(mix_number) FROM podcasts) - 3
Any thoughts on the proper way of writing the where statement? Thank you for yout time.
Set the third argument of where() to false to prevent CI from altering the string you pass in to the second argument, then you can do the subquery:
return $this->db
->select('id,song,artist,album,track,mix_name,date, link')
->where('mix_number', '(SELECT MAX(mix_number) from podcasts)', false)
->order_by('track', 'asc')
->get('podcasts')
->result();
https://www.codeigniter.com/userguide2/database/active_record.html$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
For me this produces the following query:
SELECT `id`, `song`, `artist`, `album`, `track`, `mix_name`, `date`, `link`
FROM (`podcasts`)
WHERE mix_number = (SELECT MAX(mix_number) from podcasts) ORDER BY `track` asc
If you are not too particular about using CodeIgniter's Active Record syntax, you can simply use your query as is:
$sql = "SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc";
$this->db->query($sql);
and then use $query->result() to get your results.