I'm new to Laravel and I'm still working on my Project using this Framework. In my project, I use an SQL JOIN but I don't know how to retrieve/fetch data on the joined table.
Can anybody help me on this one?
I really appreciate your help. Thanks!
Here is a simple example!
$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();
$users = \DB::table('users')
->join('contacts', 'contacts.user_id', '=', 'users.id')
->join('orders', 'orders.user_id', '=', 'users.id')
->select('users.id as id', 'contacts.phone as phone', 'orders.price as price')
//Where condition here if exist
->get();
$users will now have the array of values.Loop it in an array if necessary.
Eg:
forearch($users as $user)
{ $user->id;
$user->phone;
}
Like that.
Related
I have 4 tables clinics , locations , services & location_services in which relations are clinicid is present in locations and both serviceid and location id is present in location_services table.
My requirement is that i want to retrive all the cinics and their coreesponding locations and services .
But when i tried it is retrving the results of only one clinic id , i dont know how can i retrive complete list
following is my code
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid','locations.locationName as locname')
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->toSql();
dd($locationservices);
die();
Please help me to solve this
$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();
What I am trying to achieve is to allow teachers to import a student into different classes.
Note: A student can be multiple classes.
The problem is that when I show the list of students in a select dropdown it should return all students except for students that are not in this class (the class being the page that I am on, app.com/classes/5 for example).
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'asc')
->get();
This works and shows all students that are not in this specific class BUT if a student that's in this class and another class their name appears in the list and as duplicate names.
What can I do?
When MySQL's only_full_group_by mode is turned on, it means that strict ANSI SQL rules will apply when using GROUP BY
You should try to select fields from schema on which you can apply group by instead of select *.
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->select('users.id', 'other fields you used')
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
Not IN is also useful in your case
User::select('fields you used')
->role('student')
->whereNotIn('id', DB::table('group_user')->where('group_id', $id)->pluck('user_id')) // $id = 5
->orderBy('name', 'ASC')
->get();
Modify your query to use distinct() like so;
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->distinct()
->get();
You could also groupBy('users.id')
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
I have two tables, one with users, one with the name of their document. The first table consists of two columns: id and username. The second one consists of three columns: id, userid and document_name.
Now, I'm trying to create a query in the controller. What should happen, ideally, is that if someone visits website.com/{documentname}, it displays the username of the owner. Also, this should only happen if the current logged in user is the owner of the document. However, this is proving more difficult than I imagined. As in, I can't see what I'm doing wrong.
Here's the query:
$user = DB::table('documents')
->join('users', function($join)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
**Try this query :**
$user = DB::table('documents')
->leftJoin('users', 'users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
->get();
$document_name isn't in scope for the join function: you need to pass it through to the closure via use
$user = DB::table('documents')
->join('users', function($join) use ($document_name)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
EDIT
Because the WHERE conditions apply to the base table, and not to the JOIN:
$user = DB::table('documents')
->join('users', function($join) {
$join->on('users.id', '=', 'documents.userid')
})
->where('userid', '=', Auth::id())
->where('document_name', '=', $document_name);
->get();
TABLES X(ID,A_ID)A(ID,B_ID),B(ID,C_ID),C(ID,D_ID)D(id,VALUE)
I want to retrieve the value of D table using laravel syntax on basis of X table Id and perform a JOIN with other tables.
Please post answers only in laravel syntax. In other format I can do. I am new to it so.
X::select('value')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
But it is not working. please provide me proper solution. Right now I am using PHP logic to get the value, rather than optimise the query.
There are many ways, but this is the basic:
$rows = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('follows', 'follows.user_id', '=', 'users.id')
->where('follows.follower_id', '=', 3)
->get();
doc
Try this:
DB::table('X')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->select('D.value')
->where('x.id', '=', $val)
->get();
$result = X::select('a.value', 'd.*')
->join('a', 'x.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
foreach ($result as $row) {
# code...
}
Any way of defining an AS for a query??
I have tried the following:
$data = News::order_by('news.id', 'desc')
->join('categories', 'news.category_id', '=', 'categories.id')
->left_join('users', 'news.user_id', '=', 'users.id') // ['created_by']
->left_join('users', 'news.modified_by', '=', 'users.id') // ['modified_by']
->paginate(30, array('news.title', 'categories.name as categories', 'users.name as username'));
The problem is that ['name'] from categories will be replaces with the one from users. Any way of having them with different names?
Having the aliases above... how can I create an alias where both joins return users.name ?
paginate() method's second parameter accepts array of table columns to select in the query. So this part:
paginate(30, array('news.title, category.name'));
must be like this:
paginate(30, array('news.title', 'category.name'));
UPDATE (after you changed the question)
Try this:
->paginate(30, array('news.title', 'categories.name as category_name', 'users.name as user_name'));
UPDATE 2 (after you changed the question, again)
You can use alias on tables, too:
$data = News::order_by('news.id', 'desc')
->join('categories', 'news.category_id', '=', 'categories.id')
->join('users as u1', 'news.user_id', '=', 'u1.id') // ['created_by']
->join('users as u2', 'news.modified_by', '=', 'u2.id') // ['modified_by']
->paginate(30, array('news.title', 'categories.name as categories', 'u1.name as creater_username', 'u2.name as modifier_username'));
More simple and plain answer to this question is and what I was looking for that Eloquent supports aliases directly with table names or columns, for example:
$users = DB::table('really_long_table_name AS t')
->select('t.id AS uid')
->get();