i want to sum up a field in array result in laravel - sql

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

Related

Convert SQL with nested queries to Laravel Eloquent

I need to convert this query that I got from Access to an Laravel eloquent for my remake database project, and this is what i did but i haven't the same result with the origin sql thanks.
SELECT T_Sites.Code_Region, T_Sites.Nom_Site, T_CLM.N_CLM, T_Port.N_Port,
T_Vlan.IP_Vlan, T_Ref_Vlan.Id_Referentiel, T_Ref_Vlan.Nom_Vlan,
T_Vlan.Dt_Creat, T_Vlan.Id_Vlan, T_Vlan.IP_HSRP, T_Vlan.IP_SousReseaux,
T_CLM.Id_CLM, T_Vlan.Masque
FROM (T_CLM
INNER JOIN (
(T_Port INNER JOIN T_Vlan ON T_Port.Id_Port = T_Vlan.Id_Port)
LEFT JOIN T_Ref_Vlan ON T_Vlan.Id_Referentiel = T_Ref_Vlan.Id_Referentiel
)
ON T_CLM.Id_CLM = T_Port.Id_CLM)
INNER JOIN T_Sites ON T_CLM.Id_Site = T_Sites.Id_Site
ORDER BY T_CLM.Id_CLM DESC
WITH OWNERACCESS OPTION;
// what i did
$SuiviVlan = DB::table('T_CLM')
->join('T_Sites', 'T_CLM.Id_Site', '=', 'T_Sites.Id_Site')
->join('T_Port', 'T_CLM.Id_CLM' , '=', 'T_Port.Id_CLM')
->join('T_Vlan', 'T_Vlan.Id_Port', '=', 'T_Port.Id_Port')
->leftjoin('T_Ref_Vlan', 'T_Ref_Vlan.Id_Referentiel', '=', 'T_Vlan.Id_Referentiel')
->select('T_Sites.Code_Region','T_Sites.Nom_Site','T_CLM.N_CLM','T_Port.N_Port','T_Vlan.IP_Vlan',
'T_Ref_Vlan.Id_Referentiel', 'T_Ref_Vlan.Nom_Vlan', 'T_Vlan.Dt_Creat', 'T_Vlan.Id_Vlan', 'T_Vlan.IP_HSRP',
'T_Vlan.IP_SousReseaux', 'T_CLM.Id_CLM', 'T_Vlan.Masque')
->orderBy('T_CLM.Id_CLM', 'desc')
->get();
// It was the correct laravel eloquent , thanks btw for help.
$SuiviVlan = DB::table('T_CLM')
->join('T_Sites', 'T_CLM.Id_Site', '=', 'T_Sites.Id_Site')
->join('T_Port', 'T_CLM.Id_CLM' , '=', 'T_Port.Id_CLM')
->join('T_Vlan', 'T_Vlan.Id_Port', '=', 'T_Port.Id_Port')
->leftjoin('T_Ref_Vlan', 'T_Ref_Vlan.Id_Referentiel', '=', 'T_Vlan.Id_Referentiel')
->select('T_Sites.Code_Region','T_Sites.Nom_Site','T_CLM.N_CLM','T_Port.N_Port','T_Vlan.IP_Vlan',
'T_Ref_Vlan.Id_Referentiel', 'T_Ref_Vlan.Nom_Vlan', 'T_Vlan.Dt_Creat', 'T_Vlan.Id_Vlan', 'T_Vlan.IP_HSRP',
'T_Vlan.IP_SousReseaux', 'T_CLM.Id_CLM', 'T_Vlan.Masque')
->orderBy('T_CLM.Id_CLM', 'desc')
->get();

Error in Laravel join

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

Building where query based on input

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.

Laravel 5 join table

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

Eloquent advanced where within an advanced join

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