Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.5k views
in Technique[技术] by (71.8m points)

php - Call to undefined relationship [products] on model [AppCategory] in Laravel 7

I am creating a simple inventory system using larvel 7. I ran in to the problem with

Call to undefined relationship [products] on model [AppCategory].

I am creating a two tables category and products I want have relationship with them.

Category table

id categoryname
1   drink
2   biscuits
3   toy

product table

id productname category
 1   fanta        1
 2   apple juice  1
 3    buildblocks 3 

I need to category table and product table look like this this and get the data and passing to the table. I need this below output:

id productname categoryname
1   fanta          drink 

While I am running the program I got this error:

Call to undefined relationship [products] on model [AppCategory].

I don't know why it has happing. What I tried so far I attached below.

Model

Category

    class Category extends Model
{
    
    protected $fillable = [
        'categoryname',
    ];


}

Product

class Product extends Model
{
    protected $fillable = [
        'product_name',
        'category',
    ];

    public function category (){
        return $this->belongsTo('AppModelsCategory','category','id');
    }
}

view.blade.php

 <tbody>
    @foreach ($products as $key => $product)
      <tr>
        <td>{{$key}}</td>
        <td>{{$product->product_name}}</td>
        <td>{{$product->category->categoryname}}</td>
      </tr>
    @endforeach
    </tbody>

Routes

Route::get('/products', 'ProductController@view')->name('product.view');

ProductController

<?php

namespace AppHttpControllers;
use AppProduct;
use AppCategory;

use IlluminateHttpRequest;

class ProductController extends Controller
{
    
    public function view()
    {
    $products = Product::with('category')->get();
    dd($products);
    $categories = Category::with('products')->get();
    return view ('product.view')-> with([
        'products' => $products,
        'categories' => $categories,
    ]);
    }

}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your Category Model, you have to add the reverse relationship as well, because you are using it when you want to retrieve the list of the categories.

Category Model:

class Category extends Model
{
    protected $fillable = [
        'categoryname',
    ];

    public function products (){
        return $this->hasMany('AppModelsProduct','category','id');
    }
}

The above relationship declaration is important because you are referring to it in this line in your controller:

$categories = Category::with('products')->get();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...