Kirit Pendyala
Kirit Pendyala

Reputation: 50

How to join two model in laravel php

I'm working on a laravel project. I have to join two models Products and User. Where I need to get the results of a product as well as the details of the user who added it. I wrote the code below which isn't working. I'm wondering what'd be the solution.

Can anyone help me in fixing the issue?

Products

class Products extends Model implements AuditableContract
{    
    use SoftDeletes;
    use Auditable;
    protected $table = 'products';
    protected $fillable = ['product', 'active_ingredient', 'brand', 'similar_to1', 'similar_to2', 'similar_to3', 'similar_to4', 'category_id', 'unit', 'jug_size', 'pallet_qty', 'tote_size', 'class', 'pallet_length', 'pallet_width', 'pallet_height', 'tote_length', 'tote_width', 'tote_height', 'pallet_avg_wt', 'restricted', 'hazmat', 'hazmat_notes', 'market_avg', 'quickbooks_id', 'trending', 'added_by', 'removed_by', 'deleted_at'];

    
    public function productUser()
    {
        return $this->hasOne('App\User', 'id');
    }
}

User

<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable implements AuditableContract
{
    use HasApiTokens, Notifiable;
    use SoftDeletes;
    use Auditable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    protected $fillable = ['first_name', 'last_name','email','phone','device_token','password','approved','added_by','removed','removed_by','deleted_at'];


    public function userProduct(){
        return $this->belongsTo('App\Products', 'added_by');
    }
}

enter image description here

Upvotes: 0

Views: 853

Answers (2)

Avinash kumawat
Avinash kumawat

Reputation: 75

Product belong to a user and user will have products.

in products

function productUser(){
  return $this->belongsTo(User::class, "added_by", "id");
}

in users for one to many relation

function products(){
 return $this->hasMany(Product::class, "added_by", "id");
}

if the relation is one on one then it should be like

function product(){
   return $this->hasOne(Product::class, "added_by", "id");
}

To get the products with their respective users

$products = Products::whereDate('created_at', Carbon::today())->with('productUser')->get();

Upvotes: 0

pr1nc3
pr1nc3

Reputation: 8338

You need the relation of the Products model to be linked to the user so you create this inside the products model.

You have to modify this:

public function productUser()
{
    return $this->hasOne(User::class , 'id', 'added_by');
}

Where user_id is the foreign key in the Products table of the user and id is the primary key in the users table

Then to use that you simple do:

Products::with('productUser')->get();

That will retrieve all products with the user who added each product.

Since you want to retrieve the Products with the user info the function should be written inside the Products model.

The one you have in your user model is irrelevant for what you are trying to achieve so you can remove it.

Upvotes: 1

Related Questions