Joining tables laravel and keeping first table ids - laravel

I noticed that when you join tables in laravel using lefJoin() or Join the ids that it returns are the one of the last table. So I was wondering if I can join two tables in laravel and keepig the ids of the first table?
Here's my query
$comments = DB::table('posts')
->where('posts.parent_id',$id)
->leftJoin('profiles', 'profiles.user_id', '=', 'posts.user_id')
->leftJoin('users', 'users.id', '=', 'posts.user_id')
->get();

you should show ur code .
but try like this
$query = Users::where("users.user_id","=",$users->user_id)
if ($request->exists('product_name')) {
$query->join('products', 'users.user_id', 'products.user_id');
$query->where('products.product_name', 'like', '%' . $request->input('product_name')
. '%');
}
$test= $query->get();

Better use aliases in this case.For Example
DB::table('table_1')->select('table1.column_name as alias_name', 'table_2.column_name as alias_name2')->leftjoin('table_2', 'table_2.userId', '=', 'table_1.id')
->get();

You can select all from first table and nothing from second
DB::table('table_1')->join('table_2', 'table_1.id', '=', 'table_2.table_1_id')
->selectRaw('table_1.*')
->get();
Example DB::table('guests')->join('answers', 'guests.id', '=', 'answers.guest_id')
->selectRaw('guests.*')->get()
You also can select here anything from another table
use: ->addSelect('table_2.column')

Related

Laravel how to Order by relation in scope?

I have the following scope in User model
$query->with('country')
->when($filters['search'] ?? null, function ($query, $search) {
return $query->where('name', 'LIKE', '%' . $search . '%')
->orWhereHas('country', function ($query) use ($search) {
$query->where('name', 'LIKE', '%' . $search . '%');
});
});
$query->orderBy('name', 'asc');
}
return $query;
}
I am pretty new to Laravel - I currently the above query is sorting by user name but I would like to sort by country name. I can do this with country_id as there is a relation but not sure how to sort by country name.
Thanks
there are two approaches we can use to order these users by their company. The first is using a join:
$users = User::select('users.*')
->join('countries', 'countries.id', '=', 'users.country_id')
->orderBy('companies.name')
->get();
Here is the generated SQL for this query:
select users.*
from users
inner join countries on countries.id = users.country_id
order by countries.name asc
The second way is using a subquery:
$users = User::orderBy(Country::select('name')
->whereColumn('countries.id', 'users.country_id')
)->get();
And you can see the reference here: ordering database queries by relationship columns in laravel

Laravel 8 - How to write statement with paginate instead with get

I use Alias name of column because I have two columns in different tables with the same name, when retrieve them one column will override another column
DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->get(['*','trips.name As tirp_name','trip_user.id As reservation_id'])
How can I write it with paginate?
Try it this way:
$result = DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->paginate(100, ['*','trips.name As tirp_name','trip_user.id As reservation_id']);

how laravel join three tables with advance join clause using variable

Larvel have a question about inner joins with multiple tables and on values. I did build my code like this in laravel.
$clients = clients::leftjoin('clients_payment_type','clients_topup', function($join) {
$join->on('clients_payment_type.user_id', '=', 'clients.id') AND
$join->on('clients_topup.user_id', '=', 'clients.id');
})->where('clients.reference_id','=',$reference_id)->get();
Try this :
$clients = clients::leftjoin('clients_payment_type','clients_topup')
->join('clients','clients_payment_type.user_id', '=', 'clients.id')
->join('client_toup','clients.id', '=', 'clients_topup.user_id')
->where('clients.reference_id','=',$reference_id)
->get();
Regards
Working Answer
$clients1 = DB::table('clients')
->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
->where('clients.reference_id','=',$reference_id)
->get();

Laravel distinct on join results not working in query builder

I have a posts table that has join query with 4 other tables. 3 of them are one to one relations but the 4th is one to many. I want the query to return only 1 row for each post. What i am trying so far is like this-
$query = DB::table('posts')
->select('posts.*',
'subcategories.subcategory_title_en',
'subcategories.subcategory_title_bn',
'categories.category_title_en',
'categories.category_title_bn',
'users.*',
'postimages.postimage_thumbnail'
)
->join('subcategories', 'subcategories.subcategory_id', '=', 'posts.subcategory_id')
->join('categories', 'categories.category_id', '=', 'subcategories.parent_category_id')
->join('users', 'users.id', '=', 'posts.user_id')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id');
$query->groupBy('posts.post_id');
echo $query->count();
exit();
I have currently 50 posts in database, but the query returns all rows for each postimages, more than 1 row for each post that is. I thought distinct would only show each post_id once? What am i missing here?
I would prefer if someone tells me how to do this with query builder. As this will have a lot of searching on different columns and i want it to be as fast as possible.
This is a simpler version -
$query = DB::table('posts')
->select('posts.*','postimages.postimage_thumbnail')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id')
->groupBy('posts.post_id');
echo $query->count();
exit();
The strange thing is the SQL query that is shown by laravel " select posts.*, postimages.postimage_thumbnail from posts inner join postimages on postimages.post_id = posts.post_id group by posts.post_id" works fine in mysql
You should use groupby
Try this code
$query = DB::table('posts')
->select('posts.*',
'subcategories.subcategory_title_en',
'subcategories.subcategory_title_bn',
'categories.category_title_en',
'categories.category_title_bn',
'users.*',
'postimages.postimage_thumbnail'
)
->join('subcategories', 'subcategories.subcategory_id', '=', 'posts.subcategory_id')
->join('categories', 'categories.category_id', '=', 'subcategories.parent_category_id')
->join('users', 'users.id', '=', 'posts.user_id')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id')->groupBy('posts.post_id');
In config/database.php at "mysql" change : 'strict' => true, to false

Sql error in laravel 5.4

Here I joined 2 tables addprojects and users. But It show me error. How can I fix it?
$test= addproject::select('addprojects.id','addprojects.proj_name',DB::raw("DATEDIFF(addprojects.proj_starts,addprojects.proj_end)AS Days"))
->join('user', 'users.emp_id', '=', 'addprojects.emp_id')
->where('id', 1)->get();
what the error appeared to you ?
check this example below, i had done it to join tables :
$nextvisits = patients::join('visits', 'visits.patient_id', '=', 'patients.id')
->join('users', 'users.id', '=', 'visits.user_id')
->where('visits.revisit_date','>=',$now_str)
->where('patients.clinic_id', $user->clinic_id)
->where('visits.clinic_id', $user->clinic_id)
->where('visits.active','=','1')
->get();

Resources