簡體   English   中英

無法在 laravel 刀片中顯示外鍵值。php

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

我想在刀片中顯示外鍵值。 但我無法證明我不斷收到錯誤 =

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

這是我的遷移文件:

產品遷移:

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

類別遷移

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

產品 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);
    }
}

類別 model:

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

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

這是我的 controller:

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

我的刀片:

<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>

我正在嘗試顯示類別標題。 我不知道為什么我會出錯。 我嘗試了很多方法來解決它,但無法確定主要問題。 我是新 laravel。

將您的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 ,那么連接到那個 ( product ) model 的 function 的名稱也必須是product ,除非您在 ZC1C425268E68385D1AB5074F 中編寫的方法中將外鍵指定為第二個參數

我認為你應該這樣改變:

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

這是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

您的關系和列都有一個名稱category 這不起作用,所以我建議您將列重命名為category_id ,然后代碼將按原樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM