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();
Related
i have a query joining multiple tables together but i would love to sum u the amount fields in the collection together and push it to a view
$rec =DB::table('opos_itemdetails')
->join('opos_receiptproduct','opos_receiptproduct.id','=','opos_itemdetails.receiptproduct_id')
->join('product', 'product.id', '=', 'opos_receiptproduct.product_id')
->join('opos_receipt', 'opos_receipt.id', '=', 'opos_receiptproduct.receipt_id')
->join('opos_terminal', 'opos_terminal.id', '=', 'opos_receipt.terminal_id')
->join('location', 'location.id', '=', 'opos_terminal.id')
->get();
$rec =DB::table('opos_itemdetails')
->join('opos_receiptproduct','opos_receiptproduct.id','=','opos_itemdetails.receiptproduct_id')
->join('product', 'product.id', '=', 'opos_receiptproduct.product_id')
->join('opos_receipt', 'opos_receipt.id', '=', 'opos_receiptproduct.receipt_id')
->join('opos_terminal', 'opos_terminal.id', '=', 'opos_receipt.terminal_id')
->join('location', 'location.id', '=', 'opos_terminal.id')
->sum('table_name.column_name');
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.
Is any possible to join table users only on column where my user id != user id from db.
$games = Games::where('status', '=', 2)->where('owner_user_id', Auth::user()->id)->orWhere('join_user_id', Auth::user()->id)->orderBy('games.updated_at', 'desc')->paginate(10);
Question is:
How join user table, where owner_user_id and join_user_id != 30.
So I only want join user table for 1304 and 49. Its only example.
Games::where('status', '=', 2)
->join('users', function($join){
$join->on('users.id', '=', 'games.owner_user_id')
->where('user.id', '!=', Auth::user()->id);
})
->orderBy('games.updated_at', 'desc')
->paginate(10);
Something like this?
You need to use a use function to pass data to join:
$id = 1; // whatever id of other user
Games::where('status', '=', 2)
->join('users', function($join)use($id){ // Here you need to pass user Id
$join->on('users.id', '=', 'games.owner_user_id')
->where('user.id', '!=', $id);
})
->orderBy('games.updated_at', 'desc')
->paginate(10);
I'm trying to do the following join:
Schedules::select()
->leftJoin('histories', function ($j)
{
$j->on('histories.schedule_id', '=', 'schedules.id')
->where('histories.test', '=', DB::raw('schedules.test'))
->where(function ($q)
{
$q->where('histories.order_id', '=', 'schedules.order_id')
->orWhere('histories.customer_id', '=', DB::raw('schedules.customer_id'));
});
});
When I do, Laravel reports the error:
Missing argument 2 for Illuminate\Database\Query\JoinClause::where()
How do I structure this query in Eloquent syntax so that I get the equivalent of the following for the join statement?
left join histories
on histories.schedule_id = schedules.id
and histories.test = schedules.test
and (
histories.order_id = schedules.order_id
or histories.customer_id = schedules.customer_id
)
Unfortunately JoinClauses where doesn't work like ordinary where clause, and you can't pass closure for nested wheres, so you just need to repeat part of your conditions.
Also, you don't need where and DB::raw for your additional join clauses, instead use on once again.
So basically you need this:
$j->on('histories.schedule_id', '=', 'schedules.id')
->on('histories.test', '=', 'schedules.test')
->on('histories.order_id', '=', 'schedules.order_id')
->orOn('histories.schedule_id', '=', 'schedules.id')
->on('histories.test', '=', 'schedules.test')
->on('histories.customer_id', '=', 'schedules.customer_id');
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.