i am using laravel 5.5 and am trying to show into a view the current data :
$data = DB::table('demandes')
->join('articles', 'articles.id', '=', 'demandes.article_id')
->join('users', 'users.id', '=', 'demandes.client_id')
->select('demandes.*', 'articles.nom_article', 'users.prenom')
->get();
i am trying to find how to make a foreach statement even thought its not the same table i am working with ,
here is the demande table :
table demandes in phpmyadmin
Few changes in query
$data = DB::table('demandes as D')
->select('D.*', 'A.nom_article', 'U.prenom')
->join('articles as A', 'A.id', '=', 'D.article_id')
->join('users as U', 'U.id', '=', 'D.client_id')
->get();
Foreach loop on view:
#foreach($data as $value)
{{ $value->nom_article }}
{{ $value->prenom }}
#endforeach
See if the same you are looking for.
your foreach is same , there is noeffect of table structure because your above query only add two column in single row
Related
I am getting data from join in laravel but i cant get rides id using get or pluck method in query.
$datas = \App\Models\Order::find($id);
$new = explode(',',$id);
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->get();
here is the data
enter image description here
i want to get data of rides.id from query
whenever i use get or pluck method i got error
`
$datas = \App\Models\Order::find($id);
$new = explode(',',$id);
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->get('id');
`
enter image description here
In the second image you posted error show that 'id' field is ambiguous which means there are multiple columns named 'id' and it is true.
There are three 'id' columns coming from orders table, jobs table and rides table.
So you need to tell that which 'id' column you want to extract.
For this purpose you can try following code.
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->get('rides.id')
OR
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->select('rides.id')
->get()
these both should work and return you the collection of rides ids.
I need to get specific fields from the database and then paginate the results but I am not sure where to place ->paginate().
My code is as below:
Upload::where('sponsor_id', Request::segment(2))
//->paginate(20)
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->get([
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
])
I'm getting the correct results without paginating but I would like to paginate the results.
First, you need to use select then pass in pagination.
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select('uploads.filename','users.first_name','users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at')
->pagiate(10);
You can use select method for filtering columns. And then put paginate method end of your query.
Finaly your query will be like this:
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select(
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
)
->paginate(20);
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();
im trying to fetch multiple Id's from one array for then using it on a query relationship.
This is what I tried:
$following = $user->following;
$followingId = $following->id;
I get this error:
'Undefined property: Illuminate\Database\Eloquent\Collection::$id'
If I log $following, I get the following array, as you can see I'm following 4 different users and I need to get those id's:
local.INFO: [{"id":32,"email":"test#mail.com","username":"test#mail.com","photo":"97dfebf4098c0f5c16bca61e2b76c373","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":1,"pivot":{"follower_id":1,"followed_id":32}},{"id":69,"email":"fhamada#paginar.net","username":"fhamada#paginar.net","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":69}},{"id":79,"email":"sandrastun#gmail.com","username":"sandrastun#gmail.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":79}},{"id":101,"email":"cristobalalfonzo#outlook.com","username":"cristobalalfonzo#outlook.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":101}}]
Query relationship:
$activities = DB::table('notifications')
->join('users', 'users.id', '=', 'notifications.sender_id')
->join('followers', 'followed_id', '=', 'users.id')
->join('user_details', 'user_details.id', '=', 'notifications.user_id')
->join('winwins', 'winwins.id', '=', 'object_id')
->leftJoin('groups', 'notifications.object_id', '=', 'groups.id')
->where('notifications.sender_id', '=', $user->id)
->orWhere('winwins.id', '=', 'notifications.object_id')
->orWhere('sender_id', '=', $user->id)
->orWhere('followers.followed_id', '=', $followingId)
->orderBy('sent_at', 'desc')
->skip($page * $amount)
->take($amount)
To fetch multiple columns from a collection (e.g. multiple IDs from your following collection), use this:
$followingId = $following->pluck('id')->toArray();
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...
}