I try these code in MySQl:
SELECT
A.*,
B.name,
C.name
FROM
eventlog_tbl as A
LEFT JOIN users B ON A.byuser=B.email
LEFT JOIN users C ON A.affectiveuser=C.email;
I try these in Laravel
return DB::table('eventlog_tbl')
->leftjoin('users', 'users.email', '=', 'eventlog_tbl.byuser')
->leftjoin('users', 'users.email', '=', 'eventlog_tbl.affectiveuser')
->select('eventlog_tbl.*','users.name','users.name')
->get();
How can i convert it to Laravel?
Try below code:
$res = DB::table('eventlog_tbl')
->leftjoin('users AS A', 'A.email', '=', 'eventlog_tbl.byuser')
->leftjoin('users AS B', 'B.email', '=', 'eventlog_tbl.affectiveuser')
->select('eventlog_tbl.*','A.name as byuser_name','B.name as affectiveuser_name')
->get();
print_r($res);
This is your query written using the Laravel query builder.
$events = DB::table('eventlog_tbl')
->select('eventlog_tbl.*', 'users_1.name', 'users_2.name')
->leftJoin('users AS users_1', 'users_1.email', '=', 'eventlog_tbl.byuser')
->leftJoin('users AS users_2', 'users_2.email', '=', 'eventlog_tbl.affectiveuser')
->get();
Edit:
$events = DB::table('eventlog_tbl')
->select('eventlog_tbl.*', 'users_1.name AS user_1', 'users_2.name AS user_2')
->leftJoin('users AS users_1', 'users_1.email', '=', 'eventlog_tbl.byuser')
->leftJoin('users AS users_2', 'users_2.email', '=', 'eventlog_tbl.affectiveuser')
->get();
The problem is that both name columns are called the same thing. As per the accepted answer, these will need to be aliased differently too.
Why don't you convert this to use relationships? I have probably got the relationships wrong, something like this:
class EventLog extends Model
{
public function byUser()
{
return $this->hasOne(User::class, 'byuser', 'id');
}
public function affectiveUser()
{
return $this->hasOne(User::class, 'affectiveuser', 'id');
}
}
And then
$event_log = EventLog::with(['byUser', 'affectiveUser')->all();
foreach ($event_log as $item) {
echo $item->byUser->email();
}
Related
I have a collection of user skills but i need is to get only skills as an array i tried this
$builder->where('job_id', $value)
->join('users', 'job_applicants.user_id', '=', 'users.id')
->join('user_skills', 'users.id', '=', 'user_skills.user_id', function ($join){
$join->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
})
->groupBy('job_applicants.job_id')
->get();
The exception message : Object of class Closure could not be converted to string
The join closure should be the first and only argument after the table name.
->join('user_skills', function ($join) {
$join->on('users.id', '=', 'user_skills.user_id')
->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
});
However, a selectRaw inside the join closure doesn't make a whole lot of sense. Logically, this isn't where you select data. You've joined the table, your select statement belongs in the main query, it doesn't belong contained within the join, like so:
$builder->selectRaw("job_applicants.*, GROUP_CONCAT(user_skills.skill, ', ')")
->where('job_id', $value)
->join('users', 'job_applicants.user_id', '=', 'users.id')
->join('user_skills', 'users.id', '=', 'user_skills.user_id')
->groupBy('job_applicants.job_id')
->get();
Good day all, I am trying to count all records in a table but only if the table does not contain data in a specific column (deleted_at). It is a join table the table names are companies and employees. I am currently counting the records with a DB::raw but it should only count it if the deleted_at column is null. Please understand that i am a beginner.
public function index()
{
$user = Auth::user()->id;
$companies = DB::table('companies AS c')
->select([
'c.id',
'c.logo',
'c.company_name',
'c.created_at',
'c.sector',
'c.deleted_at',
DB::raw('COUNT(e.id) AS employee_count')
])
->leftJoin('employees AS e', 'e.company_id', '=', 'c.id' )
->leftJoin('company_user AS cu', 'cu.company_id', '=', 'c.id')
->where('cu.user_id', '=', $user)
->where('c.deleted_at', '=', null)
->groupBy('c.id')
->get();
return view('account.companies.index')
->with('companies', $companies);
}
If you are using Mysql then you could use conditional aggregation
$companies = DB::table('companies AS c')
->select([
'c.id',
'c.logo',
'c.company_name',
'c.created_at',
'c.sector',
'c.deleted_at',
DB::raw('SUM(c.deleted_at IS NULL) AS employee_count')
])
->leftJoin('employees AS e', 'e.company_id', '=', 'c.id' )
->leftJoin('company_user AS cu', 'cu.company_id', '=', 'c.id')
->where('cu.user_id', '=', $user)
->groupBy('c.id')
->get();
In mysql when an expression is used inside sum(a= b) it will result as a boolean 0/1 so you can get your conditional count using above
Or you could use whereNull() method in your query
->whereNull('c.deleted_at')
Use this code:
$employeeCount = DB::table('employees')
->select('companies.name', DB::raw('count(employees.id) as employee_count'))
->join('companies', 'employees.company','=','companies.id')
->groupBy('companies.id')
->get();
I have 2 tables 'users' and 'instantUsers'. I want to join them on users.id = instantUsers.user_id and want to add 2 where clauses on the resulting. I'm not getting how to do both. The query I'm using is -
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join->on('users.id', '=', 'instantUsers.user_id');
})
->where('instantUsers.instantMode', '=', '1')
->where (function($query) use ($userId) {
$query->where('instantUsers.user_id', '!=', $userId);
})
->get();
You can try this one maybe this will help you:
DB::table('users as table1')->join('instantUsers as table2','table1.id','=','table2.fkId') ->where('table2.instantMode','=','1')->where('table2.user_id','!=',$userId)->get();
Your 'instantUsers.instantMode','=','1' expression can be done in a join, resulting in a better performance.
I would write it like this
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join
->on('users.id', '=', 'instantUsers.user_id')
->on('instantUsers.instantMode', '=', 1);
})
->where('users.id', '!=', $userId)
->get();
i have the sql below:
$result = DB::table('tblA')
->join('tblB','tblB.id', '=', 'tblA.photoid')
->join('tblB','tblB.id', '=', 'tblA.linkedphotoid')
...
I have tblA with photoid and linkedphotoid and both link to one same table(tblB) and same column.
How to write it in laravel sql?
You can add OR condition in your join
$result = DB::table('tblA')
->join('tblB', function($join) {
$join->on('tblB.id', '=', 'tblA.photoid')->orOn('tblB.id', '=', 'tblA.linkedphotoid');
})
->get();
I am building a Twitter-like app. There is a Feed in which I want to only show posts of Users who I follow.
I tried everything with joins, but nothing seems to work.
I have 3 tables: Users, Followers, Shares
The Tables look like this:
Users: id
Followers: user_id, follower_id
Shares: user_id
What I need to get is "ALL Shares WHERE share.user_id = followers.follower_id"
"ANDWHERE followers.user_id = users.id"
Assume, the users.id is 3, I tried this:
$shares = DB::table('shares')
->leftjoin('followers', 'shares.user_id', '=', 'followers.follower_id')
->leftjoin('users', 'followers.user_id', '=', 'users.id')
->where('users.id', 3)
->where('shares.user_id', 'followers.follower_id')
->get();
But it doesnt work.
Any help is appreciated :)
I believe your join is wrong:
$shares = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('followers', 'followers.user_id', '=', 'users.id')
->where('followers.follower_id', '=', 3)
->get();
I also suggest you to name your table as follows instead, it feels a bit more natural to say user has many followers through follows and user has many followees through follows.
Example
$shares = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('follows', 'follows.user_id', '=', 'users.id')
->where('follows.follower_id', '=', 3)
->get();
Model approach
I didn't realize you were using DB:: queries and not models. So I'm fixing the answer and providing a lot more clarity. I suggest you use models, it's a lot easier for those beginning with the framework and specially SQL.
Example of models:
class User extends Model {
public function shares() {
return $this->hasMany('Share');
}
public function followers() {
return $this->belongsToMany('User', 'follows', 'user_id', 'follower_id');
}
public function followees() {
return $this->belongsToMany('User', 'follows', 'follower_id', 'user_id');
}
}
class Share extends Model {
public function user() {
return $this->belongsTo('User');
}
}
Example of Model usage:
$my = User::find('my_id');
// Retrieves all shares by users that I follow
// eager loading the "owner" of the share
$shares = Share::with('user')
->join('follows', 'follows.user_id', '=', 'shares.user_id')
->where('follows.follower_id', '=', $my->id)
->get('shares.*'); // Notice the shares.* here
// prints the username of the person who shared something
foreach ($shares as $share) {
echo $share->user->username;
}
// Retrieves all users I'm following
$my->followees;
// Retrieves all users that follows me
$my->followers;
In terms of general MySQL syntax, this is best written:
SELECT * FROM USER a JOIN FOLLOWERS b ON (a.id = b.user_id) JOIN SHARES c on (b.follower_id = c.user_id) WHERE a.id = 3
will return a data set of all followers and their respective shares.
I believe you would want the following in Laravel
DB::table('USER')
->join('FOLLOWERS', 'USER.id', '=', 'FOLLOWERS.user_id')
->join('SHARES', 'FOLLOWERS.follower_id', '=', 'SHARES.user_id')
->where('USER.id', 3)
->get();
Instead of
->where('shares.user_id', 'followers.follower_id')
It should be
->whereRaw('shares.user_id=followers.follower_id')
because on the original example the 'followers.follower_id' is interpreted as a string.
$data[shares] = DB::table('shares')
->leftjoin('followers', 'shares.user_id', '=', 'followers.follower_id')
->leftjoin('users', 'users.id', '=', 'users.id')
->where('users.id','=', 3)
->get();
to see results.
print_r($data[shares]);die;
for other query Simply give discription of your table
First of all, go through users table and join it with followers table, and then with share table to get corresponding shares data of only followers. Here is the code
$shares = DB::table('users')
->leftjoin('followers', 'users.user_id', '=', 'followers.follower_id')
->leftjoin('shares', 'shares.user_id', '=', 'users.id')
->where('users.id', 3)
->get();