简体   繁体   English

无法在 laravel 刀片中显示外键值。php

[英]can't display foreign key value in laravel blade.php

I want to show foreign key value in a blade.我想在刀片中显示外键值。 But I can't show I keep getting an error =但我无法证明我不断收到错误 =

Trying to get property 'title' of non-object (View: C:\xampp\htdocs\CRUDAPP\resources\views\products\index.blade.php)

Here is my migration file:这是我的迁移文件:

product migration:产品迁移:

Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('product_name');
            $table->decimal('price');
            $table->unsignedBigInteger('category');
            $table->foreign('category')
                ->references('id')
                ->on('categories')
                ->onDelete('cascade');
            $table->integer("quantity")->nullable();
            $table->boolean("is_out")->default(0);
        });

Category migration类别迁移

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

Product Model:产品 Model:

class Product extends Model
{
    use HasFactory;
    protected $table = "products";
    protected $fillable = [
        "product_name",
        "price",
        "category",
        "quantity",
        "is_out"
    ];

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

Category model:类别 model:

class Category extends Model
{
    use HasFactory;
    protected $table = "categories";
    protected $fillable = [
        "title"
    ];

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

Here is my controller:这是我的 controller:

public function index()
    {
        $products = Product::with('category')->get();
        print_r($products);
        return view('products.index',[
            "products"=>$products
        ]);
    }

My blade:我的刀片:

<table border="1">
    <thead>
        <th>PRODUCTS NAME</th>
        <th>PRODUCTS PRICE</th>
        <th>PRODUCTS CATEGORY</th>
        <th>PRODUCTS STOCK</th>
        <th>PRODUCTS STATUS</th>
    </thead>

    <tbody>
    @foreach($products as $product)
        <tr>
            <td>{{ $product->product_name }}</td>
            <td>{{ $product->price }}</td>
            **<td>{{ $product->category->title }}</td>**
            <td>{{ $product->quantity }}</td>
            <td>{{ $product->is_out }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

I am trying to show category title.我正在尝试显示类别标题。 I don't know why I am getting error.我不知道为什么我会出错。 I have tried a lot of ways to solve it but can't figure the main problem.我尝试了很多方法来解决它,但无法确定主要问题。 I am new laravel.我是新 laravel。

Change public function products() to public function product() at your Category model.将您的Category model 中的public function products()更改为public function product()

You have an excess of the letter s at the end of the function name (product s ), you must give the function name the exact same as the model you want to connect to, because the name of the model you want to connect to is product , then the name of the function that connects to that ( product ) model must also be product , unless you specify the foreign key as the second parameter in the method you write in the function You have an excess of the letter s at the end of the function name (product s ), you must give the function name the exact same as the model you want to connect to, because the name of the model you want to connect to is product ,那么连接到那个 ( product ) model 的 function 的名称也必须是product ,除非您在 ZC1C425268E68385D1AB5074F 中编写的方法中将外键指定为第二个参数

I think you should change like this:我认为你应该这样改变:

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

This is belongsTo relationship这是belongsTo关系

public function belongsTo(
    $related,
    $foreignKey = null,
    $ownerKey = null,
    $relation = null
) { }
@param string $related

@param string|null $foreignKey

@param string|null $ownerKey

@param string|null $relation

Your relation and column both have a name category .您的关系和列都有一个名称category This will not work, so I suggest you rename the column to category_id , then the code will work as is.这不起作用,所以我建议您将列重命名为category_id ,然后代码将按原样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM