Laravel 5 join table - sql

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

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

QueryException SQLSTATE[42S22]: Column not found: 1054

i have query exception
SQLSTATE[42S22]: Column not found: 1054 Champ '3' inconnu dans on clause (SQL: select content, name, to_id from message inner join users on users.id = 3 where to_id = 1)
here is my controller :
public function show(User $user)
{
$users = DB::table('users')
->select('name','id')
->where('id','!=',auth()->user()->id)
->get();
$messages = DB::table('message')
->join('users','users.id','=',$user->id)
->select('content','name','to_id')
->where('to_id','=',auth()->user()->id)
->get();
return view('conversations/show',compact('users','user','messages'));
}
You need to join the tables based on a foreign key. Like:
->join('users', 'users.id', '=', 'message.user_id')
and you need to specify the table in "select" and "where" after joining. Like:
->select('message.content','users.name','messages.to_id')
->where('message.to_id', auth()->user()->id)
and instead of:
->join('users', 'users.id','=', $user->id)
do this after joining:
->where('users.id', $user->id)
You need to correct your join clause in messages query.
$messages = DB::table('message')
->join('users', function($join) use($user) {
$join->('users.id', '=', 'messages.user_id')
->where('users.id', '=', $user->id)
})
->select('content','name','to_id')
->where('to_id','=',auth()->user()->id)
->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();

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

Select sql code in Laravel

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