How to set relation in laravel with two ids? - php

I have these two tables:
product
- id
- name
favorites
- id
- product_id
- user_id
So, a user can add product to favorites only once. How can I set up this relation something like the following?
public function favorites() {
return $this->hasOne(Favorite::class, 'user_id', 'product_id')
}
So, I want to use both product_id & user_id such that the query would return proper result as per the following:
Get me the wishlist of user with id 1 and product with id 13!

you can do something like that:
in favourite Model class:
public function product(){
return $this->belongsTo('App\Product');
}
public function user(){
return $this->belongsTo('App\User');
}
In Product Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
In User Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}

The user may have many favorites, so in Class User
public function favorites()
{
return $this->hasMany('App\Favorite');
}
Class Favorite
public function product()
{
return $this->belongsTo('App\Product');
}
public function user()
{
return $this->belongsTo('App\User');
}
If you have user you can
$userFavProducts = $user->favorites;
$product2 = $user->favorites()->where('product_id', 2)->get();

You should try this:
Favorites Model
public function product(){
return $this->belongsTo('App\Product','product_id');
}
public function user(){
return $this->belongsTo('App\User','user_id');
}

Related

Laravel 9 laravel associate polymorphic relations bookmarks

I have this Bookmark model
public function bookmarkable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
and in my User model
public function bookmarks()
{
return $this->hasMany(Bookmark::class);
}
public function posts()
{
return $this->hasMany(Post::class);
}
and the Post model
public function bookmarks()
{
return $this->morphMany(Bookmark::class, 'bookmarkable');
}
public function user()
{
return $this->belongsTo(User::class);
}
I want to create a new $bookmark and associate a $post with that bookmark
the bookmarks table has the columns bookmarkable_type, bookmarkable_id and user_id
Cause morphTo extends belongsTo
U can use like this
$post = Post::first()// the post u want add to bookmark
$newBookmark = new Bookmark([]);
$newBookmark->name="lorem ipsum";
$newBookmark->bookmarkable()->associate($post);
$newBookmark->user()->associate($post->user);
$newBookmark->save();
See the class docs:
https://laravel.com/api/9.x/Illuminate/Database/Eloquent/Relations/MorphTo.html

Relationship between laravel models does not work

I have 2 models: one for users and one for clients. A user is a customer
User has a 'codigocli' field and client has a 'codigo' field
The relationships between my models are like this:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigo', 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'codigo');
}
My database is fine (I think) client has the 'codigo' field and users has the 'codigocli' field. So what am I doing wrong? When I want to query my home.blade.php with dd(auth()->user()-cliente()) I don't get anything, although it shows me the parent object fine.
H
You have an OneToOne relationship here so try this if you don't change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli');
}
if you change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli','local_id_name' );
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'local_id_name');
}

How to get records from 3 belongsToMany

I have a table structured like this.
user_company
user_id
company_id
user_dispensary
user_id
dispensary_id
user_group
user_id
group_id
// User.php
public function groups()
{
return $this->belongsToMany(Group::class,'user_group','user_id');
}
public function dispensary()
{
return $this->belongsToMany(Dispensary::class, 'user_dispensary', 'user_id');
}
public function company()
{
return $this->belongsToMany(Company::class, 'user_company', 'user_id');
}
// Company.php
public function dispensary()
{
return $this->hasMany(Dispensary::class);
}
public function users()
{
return $this->belongsToMany(User::class, 'user_company', 'company_id');
}
// Dispensary.php
public function company()
{
return $this->belongsTo(Company::class);
}
public function users()
{
return $this->belongsToMany(User::class, 'user_dispensary', 'dispensary_id');
}
I have tried this to get the users of the company where the user is.
\Auth::user()->company()->first()->users
But what I want is to get the records of all the users in a company where the current loggedin user is along with their groups and the users of the dispensaries where those dispensaries belongs to the company where the current user is.

Query for relational model in laravel 5.3

I have 3 models named "User","Order","Orders_products". The relations are following:
User has many Order
Order has many Orders_products
Here are the model's code
User Model
public function order(){
return $this->hasMany('App\Order');
}
Order Model
public function order_product(){
return $this-> hasMany('App\Orders_product');
}
public function user(){
return $this-> belongsTo('App\User');
}
Orders_products Model
public function order(){
return $this->belongsTo('App\Order');
}
An user could have multiple orders and all orders could have multiple ordered products. How can i get a particular users all orders along with the ordered products.
User model
public function order()
{
return $this->hasMany(Order::class);
}
Order Model
public function user()
{
return $this->hasOne(User::class);
}
public function orders_products()
{
return $this->belongsToMany(Orders_products::class);
}
Orders_products
public function order()
{
return $this->belongsToMany(Order::class);
}
This should work, $userid is how ever you specify the user id in your app.
User::with('order_product.order')->where('id', $usersid)->get();
$Order = Order::with('order_product')->where(['user_id'=>$id])->get();
where $id is the user_id

Laravel selection from pivot table

I have three tables: users, items and user_items. A user has many items and a item belongs to many users.
**Users**
id
username
password
**Items**
id
name
**User_items**
id
user_id
item_id
Models:
class User extends Eloquent {
public function items()
{
return $this->belongsToMany('Item', 'user_items', 'item_id', 'user_id');
}
}
class Item extends Eloquent {
public function users()
{
return $this->belongsToMany('User', 'user_items', 'user_id', 'item_id');
}
}
I need to select all items table, print it and highlight rows which belongs to specific user id=1.
Selection highlighted output:
What is the right way to do it (in laravel style)?
You can use it like this
public function user_items()
{
return $this->belongsToMany('User', 'user_items', 'user_id', 'item_id')->withPivot('id');
}
Like this you can access values of third table.
Some useful links-
http://www.developed.be/2013/08/30/laravel-4-pivot-table-example-attach-and-detach/
http://vegibit.com/many-to-many-relationships-in-laravel/
http://laravel.com/docs/4.2/eloquent
You can do it like this way...
class User extends Eloquent {
public function items()
{
return $this->belongsToMany('Item', 'user_items', 'item_id', 'user_id')->withPivot('id');
}
}
class Item extends Eloquent {
public function users()
{
return $this->belongsToMany('User', 'user_items', 'user_id', 'item_id')->withPivot('id');
}
}
From controller..
$user_id = 2;
Item::with(['users'=>function($q) use ($user_id){$q->where('user_id',$user_id);}])->get();
In view at the time of listing a row you can highlight the row just use a condition as each item->users is blank or not.

Categories