Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call to undefined relationship [product] on model [App\Models\Product] …I try to solve it but I failed

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $fillable = ['title', 'location','price','discount_price','quantity','catagory','publish_status','image','image_two','image_three'];

    public function category()
    {
        return $this->belongsTo(Catagory::class,'catagory','id');
    }

    public function product_two()
    {
        return $this->hasOne(Product_two::class,'product_id','id');
    }

    public function product_plan(){
        return $this->hasOne(Product_plan::class, 'other_details','id');
    }

}

//my controllers function


public function show_product(){
$product=Product::with(['product.product_plan','product.product_two'])->get();
        return view('admin.show_product',compact('product'));
    }

product_plan model

class Product_plan extends Model
{
    use HasFactory;

    protected $fillable = ['day_one','day_two','day_three','day_four','day_five','day_six','day_seven','day_eight','day_nine','day_ten','other_details'];
    

    public function product(){

        return $this->belongsTo(Product::class, 'other_details','id');
    }

}

//product_twos model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Product_two extends Model
{
    use HasFactory;
    protected $fillable = ['duration','description','start_date','end_date','journey_date','expiry_date','provide','not_provide','extra_service','child_policy','seller_id','product_id'];

    public function product()
    {
        return $this->belongsTo(Product::class,'product_id','id');
    }

}

>Solution :

Error means what relationship are you calling does not exist in that model.

Controller.

public function show_product(){
$product=Product::with(['product_plan','product_two'])->get();
        return view('admin.show_product',compact('product'));
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading