Attaching extra model to BelongstoMany relations - php

I have two tables (users and drinks) with a pivot table, the user has a hasOne relation with profiles table, is there a way to attach the profile table to the pivot table and get all data.
Table user
id | name | email
Table profile
id | user_id | picture
Table drinks
id | name | price
Pivot Table user_drinks
id | user_id | drink_id | quantity | price | status
Drink Model
public function users()
{
return $this->belongsToMany('App\User', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}
User Model
public function drinks()
{
return $this->belongsToMany('App\Drink', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}
public function profile() {
return $this->hasOne('App\Profile');
}
PS: I don't wanna write this with raw sql query, it's driving me nuts.

I wouldn't change the tables relation.
I'd use the ->with() function to get the profile information within the existing relations.
So $user->drinks()->where('drink_id', $drink_id)->with('profile')->get(); would be my guess.

Related

Get data from pivot table

I have two tables with a pivot table
Table user
id | name | email
Table drinks
id | name | price
Pivot Table user_drinks
id | user_id | drink_id | quantity | price | status
Drink Model
public function users()
{
return $this->belongsToMany('App\User', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}
User Model
public function drinks()
{
return $this->belongsToMany('App\Drink', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}
I want to get the latest users that have bought drinks and the price and quantity from pivot table e.g
User 1 with name John has bought 2 cups of coffee at $50 dollars
User 2 with name Jane has bought 3 cups of coffee at $75 dollars
use this relation to the User model
public function drinks()
{
return $this->belongsToMany(Drink::class, 'user_drinks')->withPivot(['quantity', 'price','status'])->withTimestamps();
}
to get it:
$user = User::with(['drinks' => function ($q) {
$q->orderBy('created_at', 'desc');
}])->orderByDesc('drinks.created_at')->get();

Laravel user model with multiple one to one relationship

I want to set the User model to have a one-to-one relationship with the Student model as well as the Lecture model also.
I'm login in using the user table and from then it will map to the student or lecture table depending on the user type.
The problem is when I run app\user::find('a123')->lecture it will return the b345(Mr Steven) data.
And when I run app\user::find('b345')->student, it returns the a123(david) data.
Both result should have produce NULL because it doesnt matches the id.
New in Laravel thanks.
User table
id | password | type
a123 | 1234 | student
b345 | 1234 | lecture
Lecture table
lecture_id | id | name
b345 | b345 | Mr Steve
Student table
student_id | id | name
a123 | a123 | david
User model
public function student(){
return $this->hasOne(student::class,'student_id');
}
public function lecture(){
return $this->hasOne(lecture::class,'lecture_id');
}
Lecture model
protected $primaryKey = 'lecture_id';
public function User()
{
return $this->belongsTo(User::class,'id');
}
Student model
protected $primaryKey = 'student_id';
public function User()
{
return $this->belongsTo(User::class,'id');
}

Laravel eloquent has hasmany of hasmany

How to show all company who has photo attachment?
company:
id | name
user:
id | name | company_id
post:
id | user_id | text
post_attachment:
id | post_id | path | type
company model:
public function users() {
return $this->hasMany('App\User')->orderBy('id', 'ASC');
}
user model:
public function posts() {
return $this->hasMany('App\Post');
}
posts model:
public function images(){
return $this->hasMany('App\PostsAttachment')->where("type", "image");
}
I want to get all company who have user(s) that have post(s) with minimal 2 images attachment. Anyone can help me?
I tried Company::has('users.posts.images', '<', 2)->get(); but it also give companies who has user image below 2.
Using eloquent you could write your logic as
$companies = Company::has('users.posts.images')->get();
See Querying Relationship Existence

Lumen/Laravel many-to-many (fetch data in the middle)

I am testing out Lumen here and have a question regarding many-to-many relationship.
I've read the documentation, but I've either overlooked the answer or I might be dumber then I think. Also have to add that I am pretty new to the MVC pattern.
So I have an example here where we can have many permissions, many users and each user can have many permissions.
I have 3 database tables:
-------------------------------------
| users | id | firstname | lastname |
-------------------------------------
----------------------------------------------------------------
| permissions_users | id | permission_id | user_id | from | to |
----------------------------------------------------------------
----------------------------------
| permissions | id | name | desc |
----------------------------------
And for now I've created 2 models:
User:
public function permissions(){
return $this->belongsToMany('App\Permission', 'permissions_users', 'user_id', 'permission_id');
}
Permission:
public function users(){
return $this->belongsToMany('App\User', 'permissions_users', 'permission_id', 'user_id');
}
Now my question is, what if I want to fetch the "from" and "to" dates in the permissions_users table, what do I do?
Do I create a model called PermissionUser "between" that act as a middleman between User and Permission or is there another way?
Thanks
You can define these attributes also in select on relation. I think this will work for you.
public function permissions()
{
return $this->belongsToMany('App\Permission', 'permissions_users', 'user_id', 'permission_id')->select(['from', 'to']);
}
You might need to add permission columns too in select else it might show only from and to in your relation object.
public function permissions()
{
return $this
->belongsToMany('App\Permission', 'permissions_users', 'user_id', 'permission_id')
->withPivot('from', 'to');
}
foreach($user->permissions as $permission){
$permission->pivot->from;
$permission->pivot->to;
}

Laravel: use Eloquent belongsto , get data from both tables?

In Controller, I would like to pass the only one variable with specifies column from parent in it. Now,I'm using
View::make('store.product')->with('products', Product::find($id)
->join('design','product.design_id','=','design.id')
->join('user','design.user_id','=','user.id')
->select('user.name','design.title','product.price')
->get();
My question is
1.Is there a better way to do this by using Belongsto?
2.If can do, Is it work the same with Hasmany?
This is my table structure.
User
id | name
1 | 'John'
Design
id | user_id | title
1 | 1 | 'Chill'
2 | 1 | 'Mad'
Product
id | design_id | price
1 | 1 | 20
2 | 1 | 30
And Model be like this
Product belongsto Design ,
Design belongsto User
Add a method to your Users like so for your designs that the user has;
public function designs(){
$this->hasMany('Design');
}
For the designs model add the following methods;
public function user(){
$this->belongsTo('User');
}
public function products(){
$this->hasMany('Product');
}
For your products model
public function design(){
$this->belongsTo('Design');
}
These will set up the relationship allowing you to eager load the data on your models.
This can be done like so;
$variable = Product::with('designs')->find(1); //This will load the product with the related designs
If you want all the designs and the users that belong to the designs do the following;
$variable = Product::with('designs', 'design.user')->find(1); //This will load the designs that relate to the Product and include the user that that design belongs to on the Design model.
To access the properties use the following;
$variable->designs //This will return a collection of all the designs.
$variable->designs->first()->user //This will return the user model that the design belongs to.
An example of displaying the information;
#foreach ($variable->designs as $design)
{{ $design->user->username }}
#endforeach
Please note: i have not tested this code.

Categories