I am not able to reproduce this query in eloquent.
How could I have made a query without eloquent?
select * from points where operator = 2 and (month(date) = '1' and day(date) >= 25 or month(date) = '2' and day(date) <= 24)
You may use DB facade for this purpose..
$query = "select * from points where operator = :operator and (month(date) = :monthDate and day(date) >= :dayDate or month(date) = :monthDate2 and day(date) <= :dayDate2)"
$points = DB::select($query,[
"operator" => 2,
"monthDate" => 1,
"dayDate" => 25,
"monthDate2" => 2,
"dayDate2" => 24
])
Considering you have Point model for points table, below eloquent query should work :
<?php
$points = Point::where('operator', 2)->where(function($q){
return $q->whereMonth('date', '=', 1)->whereDay('date', '>=', 25);
})->orWhere(function($q){
return $q->whereMonth('date', '=', 2)->whereDay('date', '<=', 24);
})->get();
Related
I have a complicated query that I would like to translate either in Eloquent ORM or with the Query Builder for a Laravel site, but I can't do it, can someone help me?
Here is my SQL query
SELECT opp_id, risk_study.rst_id, risk_study.rst_status
FROM opportunity
LEFT JOIN risk_study ON risk_study.rst_id =
(SELECT risk_study_quote_vehicle.rst_id
FROM risk_study_quote_vehicle
INNER JOIN quote_vehicle ON quote_vehicle.quv_id = risk_study_quote_vehicle.quv_id
INNER JOIN quote ON quote.quo_id = quote_vehicle.quo_id
WHERE quote.opp_id = opportunity.opp_id
ORDER BY risk_study_quote_vehicle.rst_id DESC
LIMIT 0,1)
WHERE 1 = 1
AND opportunity.per_id_process = '5'
AND opportunity.opp_locked = '1'
Here is my solution.
But this query does not retrieve values from the risk_study table.
$opportunities = Opportunity::select('opp_id', 'risk_study.rst_id', 'risk_study.rst_status')
->leftJoin('risk_study', function ($join) {
$join->on('risk_study.rst_id', '=', DB::raw('"SELECT risk_study_quote_vehicle.rst_id FROM risk_study_quote_vehicle INNER JOIN quote_vehicle ON quote_vehicle.quv_id = risk_study_quote_vehicle.quv_id INNER JOIN quote ON quote.quo_id = quote_vehicle.quo_id WHERE quote.opp_id = opportunity.opp_id ORDER BY risk_study_quote_vehicle.rst_id DESC LIMIT 0,1"'));
})
->where([
['per_id_process', 5],
['opp_locked', 1],
])
->get();
Finally, I wrote the query and it works ! Thanks !
$opportunities = Opportunity::query()
->select(
'opp_id',
'risk_study.rst_id',
'risk_study.rst_status'
)
->from('opportunity')
->leftJoin('risk_study', function ($join) {
$join->where('risk_study.rst_id', function ($sub) {
$sub->select('risk_study_quote_vehicle.rst_id')
->from('risk_study_quote_vehicle')
->join('quote_vehicle', 'quote_vehicle.quv_id', 'risk_study_quote_vehicle.quv_id')
->join('quote', 'quote.quo_id', 'quote_vehicle.quo_id')
->whereColumn('quote.opp_id', 'opportunity.opp_id')
->orderByDesc('risk_study_quote_vehicle.rst_id')
->limit(1);
});
})
->where('opportunity.per_id_process', '5')
->where('opportunity.opp_locked', '1')
->get();
You should be able to use a Closure as the second parameter of where() inside your join Closure.
I don't think Laravel's query builder supports the LIMIT 0,1 statement.
I don't know why you need WHERE 1 = 1, but you should be able to use whereRaw for that.
When you're done, the result looks very alike a formatted SQL query.
$opportunities = Opportunity::query()
->select(
'opp_id',
'risk_study.rst_id',
'risk_study.rst_status'
)
->from('opportunity')
->leftJoin('risk_study', function ($join) {
$join->where('risk_study_quote_vehicle.rst_id', function ($sub) {
$sub->select('risk_study_quote_vehicle.rst_id')
->from('risk_study_quote_vehicle')
->join('quote_vehicle', 'quote_vehicle.quv.id', 'risk_study_quote_vehicle.quv_id')
->join('quote', 'quote.quo_id', 'quote_vehicle.quo_id')
->where('quote_opp_id', 'opportunity.opp_id')
->orderByDesc('risk_study_quote_vehicle.rst_id')
->limit(1);
});
})
->whereRaw('1 = 1')
->where('opportunity.per_id_process', '5')
->where('opportunity.opp_locked', '1')
->get();
I have four different queries I want to combine as on queries, with each representing a column. Each stand for just only one row, because of the count(), sum() functions
I have written the queries
$lastsevendayssubscribers = DB::table('users')
->select(DB::raw('count(*) as total ') )
->where(DB::raw("DATE(created_at)"), '>=', DB::raw('DATE(NOW()) - INTERVAL 7 DAY'))
->count();
$activesubscriptions = DB::table('users')
->where('plan_status', 1)
->count();
$expiredsubscriptions = DB::table('users')
->where('plan_status', 1)
->count();
$currentDay = date('m');
$currentdaybilling = DB::table("users")
->select(DB::raw("SUM(amount) as total"))
->whereRaw('(created_at) = ?',$currentDay)
->get();
I want the final result to look like this:
|$lastsevendayssubscribers|$activesubscriptions|$expiredsubscriptions|$currentdaybilling|
$semester01 = '1';
$session01 = '5';
$level01 = '200';
$results = DB::table('results')
->join('departments', 'results.DepartmentID', '=', 'departments.DepartmantID')
->join('subjects', 'results.SubjectID', '=', 'subjects.SubjectID')
->groupBy('matricno')
->selectRaw('results.matricno as matricno, sum(tnu) as sum, sum(subjects.SubjectValue) as sum2')
->where('results.Level', $level01)
->where('results.Semester', $semester01)
->where('results.SessionID', $session01)
->where('departments.DepartmantID', '17')
->get();
//return $arrayed;
$results2 = DB::table('results')
->join('departments', 'results.DepartmentID', '=', 'departments.DepartmantID')
->join('subjects', 'results.SubjectID', '=', 'subjects.SubjectID')
->groupBy('matricno')
->selectRaw('results.matricno as matricno, sum(tnu) as sumr2, sum(subjects.SubjectValue) as sum2r2')
->where('results.Level', $level01)
->where('results.Semester', $semester01)
->where('results.SessionID', $session01)
->where('departments.DepartmantID', '7')
->get();
The above code is supposed to return a group of students with distinct results alongside the various departments.
Result1 and result2 are supposed to be joined together, with matricno being the distinct field. I want to display the result in this form. Merging both results into one tabular form.
matricno | sum | sum2 | sumr2 | sum2r2
You can use merge() method for merge those collections like;
$merged_result = $results->merge($results2)
You can return a view with some data as follows
return view('viewname', ['results' => $results, 'results2' => $results2]);
return view('yourview')->with(['results' => $results, 'results2' => $results2]);
$tadaydate = ddate('m/d/Y');
$value1 = value_table::where('value_1', $tadaydate)->get();
$value2 = value_table::where('value_2', $tadaydate)->get();
$value3 = value_table::where('value_3', $tadaydate)->get();
$value4 = value_table::where('value_4', $tadaydate)->get();
$value5 = value_table::where('value_5', $tadaydate)->get();
Is there any chance to check with single query?
You can use the "->whereIn()" function I believe.
$value_array = ['value1',...,'value5'];
$value_table->whereIn($tadaydate, $value_array)->get;
You can combine then:
$query = value_table::where('value_1',$tadaydate)
->orWhere('value_2',$tadaydate)
->orWhere('value_3',$tadaydate)
->orWhere('value_4',$tadaydate)
->orWhere('value_5',$tadaydate)
->get()
You should use where instead of orWhere to connect the wheres with an AND instead of an OR
To simplify it in a loop:
$columns = 7;
$query = value_table::query();
for ($i = 1;$i <= $columns;$i++) {
$query = $query->orWhere('value_'.$i,$tadaydate);
}
$values=$query->get();
However you will have much more success if you normalise your table:
table
-----------
id | more columns
value_table
------------
id | table_id (fK) | value
And two models Table and ValueTable . That way (assuming proper eloquent setup) you can just do:
$tableRow = Table::whereHas("value", function ($q) use ($tadaydate) {
return $q->where("value",$tadaydate);
})->get();
You can use whereIn for this. In whereIn you can pass your array like this:
$values_list = [$val1,$val2,$val3];
$data = YourModel::whereIn($tadaydate, $values_list)->get();
You should try this:
$tadaydate=ddate('m/d/Y');
$result = value_table::where('value_1',$tadaydate)
->orWhere('value_2',$tadaydate)
->orWhere('value_3',$tadaydate)
->orWhere('value_4',$tadaydate)
->orWhere('value_5',$tadaydate)
->get();
Updated Answer
$user_list = value_table::where(function($query) use ($tadaydate){
$query->where('value_1', =, $tadaydate);
$query->where('value_2', =, $tadaydate);
$query->where('value_3', =, $tadaydate);
$query->where('value_4', =, $tadaydate);
$query->where('value_5', =, $tadaydate);
})
->get();
Hello I am seaching for a solution in Laravel 5 (eloquent) to select records form a table where updated_at + 15 minutes <= today (actual datetime).
I try this solution but it does not work :
$albums = Album::with(array('pictures' => function($q){
$q->whereDate('updated_at','<=', date("Y-m-d H:i:s", time() - (15*60)));
}))->get();
The result look always empty
dd($albums->pictures)
$currentTime = \Carbon\Carbon::now()->subMinutes(15);
above will give you equivalent to this mess 'date("Y-m-d H:i:s", time() - (15*60))'
I still don't understand why you need these 15 minute operation? But any way try this and let us know outcome.
$albums = Album::with(array('pictures' => function($q){
$currentTime = \Carbon\Carbon::now()->subMinutes(15);
$q->whereDate('updated_at','<=', $currentTime));
}))->get();