Home > database >  can't display foreign key value in laravel blade.php
can't display foreign key value in laravel blade.php

Time:08-22

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:

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:

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

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

Here is my 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.

CodePudding user response:

Your relation and column both have a name category. This will not work, so I suggest you rename the column to category_id, then the code will work as is.

CodePudding user response:

Change public function products() to public function product() at your Category model.

You have an excess of the letter s at the end of the function name (products), 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

CodePudding user response:

I think you should change like this :

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

This is belongsTo relationship

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