【问题标题】:Undefined property: App\Models\Product::$category Laravel 8未定义的属性:App\Models\Product::$category Laravel 8
【发布时间】:2021-12-21 07:27:21
【问题描述】:

我正在使用 Laravel 8 创建一个简单的库存系统。 我想加入两个表:categoryproducts

分类表:

id categoryname
1 drink
2 biscuits
3 toy

产品表:

id prodname catid
1 fanta 1
2 apple juice 1
3 buildblocks 3

我需要将数据作为以下输出传递给表格

id productname categoryname
1 fanta drink

运行程序时出现以下错误:

未定义属性:App\Models\Product::$category

类别

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

    public function products()
    {
        return $this->hasMany('App\Models\Product', 'id', 'catid');
    }
}

产品

class Product extends Model
{
    protected $fillable = [
        'id',
        'prodname',
        'catid',
    ];

    public function category()
    {
        return $this->belongsTo('App\Models\Category', 'category', 'id');
    }
}

view.blade.php

<html>
    <head>
        <body>
            <tbody>
                @foreach ($products as $key => $product)
                <tr>
                    <td>{{ $key }}</td>
                    <td>{{ $product->prodname }}</td>
                    <td>{{ $product->category->catname }}</td>
                </tr>
                @endforeach
            </tbody>
        </body>
    </head>
</html>

控制器

<?php

namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

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

路线

Route::get('/products', [ProductController::class, 'view']);

【问题讨论】:

  • products表中的外键是catid还是category
  • 是产品表中的外键 catid
  • 我在上面添加数据库表

标签: php laravel


【解决方案1】:

你在模型中弄错了这里是一个例子 将 catid 更改为 category_id 您在产品表中的列应该是数据库中的 category_id 到

在我的用户表中

public function employee()
    {
        return $this->hasOne(Employee::class, 'user_id', 'id');
    }

这是我的员工表

public function user()
    {
        return $this->belongsTo(User::class);
    }

你必须在你的控制器方法上工作看看这里

 public function showEmployee()
    {
       
        $emps   = User::with('employee');
        
        return view('employee.show_emp', compact('emps'));
    }

这样的路线

Route::resource('showEmployee','EmpController@showEmployee');

这是一个你的解决方案,我想安慰一下。

类别

use App\Models\Product;


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

    public function products()
    {
        return $this->hasMany(Product::class, 'catid', 'id');
    }
}

产品

use App\Models\Category;


class Product extends Model
{
    protected $fillable = [
        'id',
        'prodname',
        'catid',
    ];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

控制器

<?php

namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    



public function view()
    {
       
        $products = Product::with('category')->get();
        $categories = Category::with('product')->get();
        
        return view('employee.show_emp', compact('products', 'categories'));
    }
}

路线

Route::get('products','ProductController@view');

将这两行添加到您的两个模型中,分别在 namspace 之后 对于类别

use App\Models\Product;

对于产品

use App\Models\Category;

注意:这是未经测试的代码

我在这里尝试解决您的问题

【讨论】:

  • 如果您找到解决方案,请点赞或批准我的回答
  • 仅在 view.blade.php 上解决此问题 尝试在 null 上读取属性“catname”(视图:E:\laravelprjo\pos\resources\views\products\view.blade.php)
  • 它通过null 表示错误,他们无法使用产品从类别表中找到catname。请参阅我的模型示例 hasOne 和 belongsTo 都用于制作关系 b/w 两个表。如果可以访问,请尝试按类别访问产品名称意味着您必须更改关系基本规则
  • 请帮我写,我累了很多次加入两个表,我不能
  • 仔细查看我的示例,按照我的代码更改您的代码,然后尝试刷新。您在模型上建立关系的代码是错误的。
【解决方案2】:

传递给关系的外键不正确,您尝试在视图中访问的属性不存在。

请尝试以下更改:

Products.php:

public function category()
{
    return $this->belongsTo('App\Models\Category', 'catid', 'id');
}

查看:

{{ $product->category->categoryname }}

One To Many (Inverse) / Belongs To

【讨论】:

  • 是的。更改后我得到的错误是调用模型 [App\Models\Category] ​​上的未定义关系 [产品]。
  • 类别模型的关系定义为products 而不是product
  • 我要在哪里改变它
  • 在您的控制器中,Category::with('products')-&gt;get();
  • 未定义变量 $products 我收到一个错误,我认为错误来自 view.blade.php
【解决方案3】:

简单地遵循 Laravel 约定,避免将来出现问题。

https://github.com/alexeymezenin/laravel-best-practices#follow-laravel-naming-conventions

表结构:

categories
    id - integer
    name - string

products
    id - integer
    category_id - integer
    name - string

类别模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

产品型号

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['name'];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

路线

Route::get('products', [ProductController::class, 'index']);

控制器

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index()
    {
        $products = Product::with('category')->get();

        return view('product.index', compact('products'));
    }
}

index.blade.php

<table>
    <tbody>
        @foreach ($products as $product)
            <tr>
                <td>{{ $product->id }}</td>

                <td>{{ $product->name }}</td>

                <td>{{ $product->category->name }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 2021-03-11
    • 2021-11-12
    • 1970-01-01
    • 2019-10-14
    • 2018-04-12
    相关资源
    最近更新 更多