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
439 views
in Technique[技术] by (71.8m points)

laravel 7 - Call to undefined relationship [categories] on model [AppProduct]

I have three tables (products, categories, sizes) and models (Product, Category, Size) respectively.

In my product table I saved category_id and size_id respectively.

For list view I want to fetch category_name from category table and size_name using size table respectively.

I am using relation for that purpose. Below is model structure of relationship.

Product:

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Product extends Model
{
    public function category()
    {
        return $this->belongsTo(Category::class);
    }
    public function size()
    {
        return $this->belongsTo(Size::class);
    }
}

Category:

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Category extends Model
{
    public function product()
    {
        return $this->hasMany(Product::class);
    }
}

Size:

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Size extends Model
{
    public function product()
    {
        return $this->hasMany(Product::class);
    }
}

Below is my controller where I want fetch data with category name and size name. Then want to send data to list view with category name and size name to display:

public function productList()
{
    // $product = Product::get();
    $data = Product::with('categories','sizes')->get();
    echo "<pre>";
    //print_r($product->category->name);
    // return view('admin.productlist',['productdata' => $data]);
}

Below is my migration file of product:

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('product_name');
    $table->string('product_category')->nullable();
    $table->string('product_price');
    $table->string('product_size')->nullable();
    $table->string('product_stock')->nullable();
    $table->string('product_description')->nullable();
    $table->string('created_user_id')->nullable();
    $table->timestamps();
});

Size migration:

Schema::create('sizes', function (Blueprint $table) {
    $table->id();
    $table->string('product_size');
    $table->timestamps();
});

Category Migration :

Schema::create('categories', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});

Anyone have any idea what I am doing wrong then please help me in that.

My list blade file is below:

@foreach ($productdata as $product)
    <tr>
        <td>{{$product->product_name}}</td>
        <td>{{$product->product_category}}</td>
        <td>{{$product->product_size}}</td>
        <td>{{$product->product_price}}</td>
    </tr>
@endforeach

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

1 Reply

0 votes
by (71.8m points)
等待大神答复

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

...