laravellaravel-10

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','category','publish_status','image','image_two','image_three'];

    public function category()
    {
        return $this->belongsTo(Category::class,'category','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

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'));
        }