Laravel Join returns empty array - laravel

$users = DB::table('users')
->join('course', 'users.id', '=', 'course.courseId')
->join('skills', 'users.id', '=', 'skills.id')
->join('subjects', 'users.id', '=', 'subjects.id')
->get();
dd($users);
This gives me an empty result like this- items: []. I would like to join the 4 tables - users, course, skills & subjects and fetch all the data in all the 4 tables. users table has the id column(PK) named as id, course table has the id column(PK) named as courseId, skills table has its id column(PK) named as id & subjects table also has the id column(PK) named as id. The PK of all 4 tables is of the same data type biginteger. How to solve this?

Using the relations to get all the data:
The User::class has three relation methods hasMany() to the other classes (Course::class, Skill::class and Subject::class)
$users = User::with(['courses', 'skills', 'subjects'])->get();

Related

Laravel Lumen eloquent left join returns joined table data rather than primary able data

I have two tables company and courses table
The table has the following fields
companies table
id, full_name, email, deleted_at
and courses table
courses table
id, company_id, course_name,deleted_at
Now i would like to retrieve all courses which company is not deleted. So in my controller i have added
public function index(Request $request){
$query = Courses::query();
$query = $query->leftJoin('companies','companies.id','=','courses.company_id');
$query->whereNull('companies.deleted_at');
if($request->get('filter_name')){
$query = $query->where('courses.name', 'like', '%' . $request->get('filter_name') . '%');
}
return response()->json($query->paginate($request->get("perPage")));
}
When i run the above it returns companies data rather than courses. Where am i going wrong or what am i missing out?
If you have use eager loading in both of your model, you can use this kind of approach.
$all_course = Courses::with(['company', function($query) {
return $query->whereNotNull('deleted_at');
}])->get();
As you can see, I query in the Courses model to return all the courses but I added some filter in the company relationship. where in I used whereNotNull to get only the company that's not deleted.

How to fetch data from multiple table according to id by using query builder in laravel

How to fetch data from multiple table according to id by using query builder in Laravel?
example:
I have three tables such as student,parent and teacher.Now I want to fetch data from this three tables(student,parent and teacher) according to id for editing.So how can I do that?
see bellow codes please
public function edit($id)
{
$values=DB::table("student")->find($id);
$values=DB::table("parent")->find($id);
$values=DB::table("teacher")->find($id);
return view("info", compact("values"));
}
You probably want to fetch data according to id that you have. Of course that other tables need to have foreign keys of that table>
Here is laravel documentation for joins:
https://laravel.com/docs/5.8/queries#joins
and here is example code from docs:
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

joining 3 tables in laravel using DB with similar column names

I have three 3 tables in my database user_projectswith columns
project_id,
user_id,
role
project_invitations with columns
project_id,
email,
role
and finally users
id,
email
I want to join the three tables using the laravel query builder so that I can get
userid, role, email, invited_user
null member test#gmail.com true
1 owner test123#gmail.com false
2 member test12#gmail.com true
So far I got the following data
[
{
"email":"test#sharklasers.com",
"id":2,
"role":"owner",
"invitedEmail":"test#gmail.com",
"invitedRole":"member"
},
{
"email":"test#sharklasers.com",
"id":2,
"role":"owner",
"invitedEmail":"safda#gmail.com",
"invitedRole":"member"
}
]
using the following query
DB::table('user_projects')
->join('users', 'user_projects.user_id', '=', 'users.id')
->join('project_invitations', 'project_invitations.project_id','=','user_projects.project_id')
->where('user_projects.project_id', '=', $projectId)
->select('users.email', 'users.id', 'user_projects.role', 'project_invitations.email as invitedEmail','project_invitations.role as invitedRole')
->get();
You need to use outer joins to get all records even if there is not a join for the record.
DB::table('user_projects')
->join('users', 'user_projects.user_id', '=', 'users.id', 'outer')
->join('project_invitations', 'project_invitations.project_id','=','user_projects.project_id', 'outer')
->where('user_projects.project_id', '=', $projectId)
->select('users.email', 'users.id', 'user_projects.role', 'project_invitations.email as invitedEmail','project_invitations.role as invitedRole')
->get();

Laravel elequent pivot table using where

How can I use Laravel to do something like this:
Tables:
projects:
id,name, active
pivot table: project_users
id, project_id, user_id
If I do this, I get good results, including all users of the project
Project::with('users')->where('active', '=', 1)->get();
But how can I filter on users and on active ? something like ?
Project::with('users')->where('active', '=', 1)->where('users',=,3)->get();
or ??
Project::with('users')->where('active', '=', 1)->where('users.user_id',=,3)->get();
If you want to restrict rows in the table you are eager loading then you can add constraints to the eager load where you can perform your usual QueryBuilder operations on the $query. So in your example we can do the following:
Project::with(['users' => function($query)
{
$query->where('user_id', '=', 3);
}])->where('active', '=', 1)->get();
This is assuming active is accessible to your initial table before the eager load.

Laravel 4/5, order by a foreign column

In Laravel 4/5 how can order a table results based in a field that are connected to this table by a relationship?
My case:
I have the users that only store the e-mail and password fields. But I have an another table called details that store the name, birthday, etc...
How can I get the users table results ordering by the details.name?
P.S.: Since users is a central table that have many others relations and have many items, I can't just make a inverse search like Details::...
I would recommend using join. (Models should be named in the singular form; User; Detail)
$users = User::join('details', 'users.id', '=', 'details.user_id') //'details' and 'users' is the table name; not the model name
->orderBy('details.name', 'asc')
->get();
If you use this query many times, you could save it in a scope in the Model.
class User extends \Eloquent {
public function scopeUserDetails($query) {
return $query->join('details', 'users.id', '=', 'details.user_id')
}
}
Then call the query from your controller.
$users = User::userDetails()->orderBy('details.name', 'asc')->get();

Resources