I'm trying like this:
$cost=DB::table('breakfast_orders')
->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
->where('breakfast_orders.user_id',1)
->WHERE(' MONTHNAME(date)='June'')
->sum('breakfast_costs.total_cost');
or the transformation of the following sql might help me:
SELECT * FROM `breakfast_orders` WHERE MONTHNAME(date)='June'
There are date helpers available in the query builder :
$q->whereDay('created_at', '=', date('d'));
$q->whereMonth('created_at', '=', date('m'));
$q->whereYear('created_at', '=', date('Y'));
So in your case :
$cost = DB::table('breakfast_orders')->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
->where('breakfast_orders.user_id',1)
->whereMonth('created_at', '=', $month)
->sum('breakfast_costs.total_cost');
$month = 'June';
$cost=DB::table('breakfast_orders')
->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
->where('breakfast_orders.user_id',1)
->whereRaw('MONTH(date) = '.$month)
->sum('breakfast_costs.total_cost');
Related
I am trying to get results from 2 table using join, where i want result between time range from those 2 tables,
here is my code
$final_trade = DB::table('finaltrade')
->join('exchanges', function($join)
{
$join->on('exchanges.id', '=', 'finaltrade.exchange_id')
->where('finaltrade.user_id', '=', Auth::user()->id)
->whereTime('finaltrade.created_at', '=', 'exchanges.start_time');
})
->get();
when i add hardcoded time value instead of 'exchanges.start_time' it gives correct result but i want to add values from column,
You have to tell Laravel that it isn't a value, but a column name:
->whereTime('finaltrade.created_at', '=', DB::raw('exchanges.start_time'));
The wrong code is ->whereTime('finaltrade.created_at', '=', 'exchanges.start_time'); change it to ->whereColumn('finaltrade.created_at', '=', 'exchanges.start_time');
And corrected code is
DB::table('finaltrade')
->join('exchanges', function($join) {
$join->on('exchanges.id', '=', 'finaltrade.exchange_id')
->where('finaltrade.user_id', '=', Auth::user()->id)
->whereColumn('finaltrade.created_at', '=', 'exchanges.start_time');
})->get();
matchthese=['name'=> Input::get('name'),];
Matchthose['place'=> Input::get('place')];
$Select_db = Db::table('actvities')
->where([[matchthese],[matchthose]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->get();
But the orwhere doesn't work correctly.I want the first one or the second one etc.How should i do this to work?
You can use skip and take with first:
For example for the first one you can use first directly:
$Select_db = Db::table('actvities')
->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->take(1)->first();
For the second :
$Select_db = Db::table('actvities')
->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->skip(1)->take(1)->first();
If you need to loop through the result you can simply use foreach:
$Select_db = Db::table('actvities')
->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->get();
foreach($Select_db as $record) {
//Do something with $record.
}
I hope this will help you.
How to write this query in Laravel. I am new in laravel.
Example:
SELECT * FROM ((respassanger join ((`reservation` join flightres
on flightres.res_id = reservation.id)) on respassanger.res_id = reservation.id)
join passanger on respassanger.pas_id = passanger.pas_id)
Have you read the documentation? Checkout Laravel's query builder. Using your table names as mentioned, work from the following example in the docs for your needs:
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
So something like:
DB::table('respassanger')
->join('reservation', function($join)
{
$join->on('flightres.res_id', '=', 'reservation.id')
})
->join('passanger', 'respassanger.pas_id', '=', 'passanger.pas_id')
->select( ... )
->get();
Not sure how correct the above is, but tweak it if it doesn't work.
Following query returning six values
SELECT tbl_start FROM timetable inner join route ON tbl_rte_id = id WHERE rte_origin = "UL" and rte_destination = "HW" ORDER BY(tbl_start) DESC;
And my laravel code is returning only one value
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'id')
->where('rte_origin', $origin, 'AND')
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get();
foreach ($tables as $table) {
$result[$table->id] = $table->tbl_start;
}
This laravel code is not similar or similar. Can anyone help me.
Change this part:
->where('rte_origin', $origin, 'AND')
// to:
->where('rte_origin', $origin)
It will know by default that it's AND operator
And if you want to provide this operator, then do this:
->where('rte_origin', '=', $origin, 'AND')
You may try something like this:
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'timetable.id')
->where('rte_origin', $origin)
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get()->lists('tbl_start', 'id');
The $tables will contain an array of id => tbl_start pairs.
Add a listener in your routes.php
Event::listen('illuminate.query', function($sql){
var_dump($sql);
});
Then execute both queries and check if you have the same result
Here is my query:
$comment = ORM::factory('comment')
->where('status', '=', 1)
->find_all();
field = user_id
How do I chose to only one of each comments in user_id?
The following:
$comment = ORM::factory('comment')
->distinct('user_id')
->where('status', '=', 1)
->find_all();
Displays everything in the database!
You code should be more like this..
$comment = ORM::factory('comment')
->where('status', '=', 1)
->group_by('user_id')
->find_all();
Check the documentation for more info.