i am trying to get the name using a relationship

Viewed 86

image of category table

image of products tablehere what I am trying to get the name of the category by using a relationship here so far what I have done is I have defined a relationship in my product model as such

Public function category()
    {
        return $this->belongsto(Category::class);
    }

I have also stored the field category id in my products table

  Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('Category_id');
            $table->string('name');
            $table->string('code');
            $table->string('color');
            $table->string('description');
            $table->double('price');
            $table->integer('quantity');
            $table->string('image');
            $table->timestamps();
        });
    

also I have defined a relationship in my Category model such as

public function product()
    {
        return $this->hasmany(Product::class);
    }

ALSO I HAVE DEFINED Relationship IN PRODUCT AS

Public function category()
    {
        return $this->belongsto(Category::class);
    }

in my view I am trying to get the name of the category


    @foreach($products as $pro)
                            <tr>
                              
                            <td><img src="{{asset('uploads/maj/'.$pro->image)}}" class="img-circle" alt="User Image" width="50" height="50"> </td>
                            <td>{{$pro->category->name}}</td>
                            <td>{{$pro->name}}</td>
                            <td>{{$pro->code}}</td>
                            <td>{{$pro->color}}</td>
                            <td>{{$pro->description}}</td>
                            <td>{{$pro->price}}</td>
                            <td>{{$pro->quantity}}</td>

I am receiving the error

Trying to get property 'name' of non-object

how can I get the name of the category ?? I am trying to get it like this but this is giving error

{{$pro->category->name}}


the category table row

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

my laravel version is 7 thanks

4 Answers

try this

  Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->foreignId('category_id')->nullable()->constrained()->onDelete('cascade');
            $table->string('name');
            $table->string('code');
            $table->string('color');
            $table->string('description');
            $table->double('price');
            $table->integer('quantity');
            $table->string('image');
            $table->timestamps();
        });

in model Product

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

thin call products

$products = product::orderBy('created_at','asc')->with('category')->get();

you can chick if has relationship or not

 @if(! empty($pro->category)) {{ $pro->category->name }} @else <span>no category</span> @endif

Maybe one or more product doesn't have category or category_id is wrong.

you can use condition to check if category is not exist:

{{$pro->category ? $pro->category->name : "no category"}}

or can simply using null coalescing operator:

{{$pro->category->name ?? "no category"}}

if category is required you should prevent it by add table relation/foreign.

You should try like this way :

(!empty($pro->category)) ? $pro->category->name : 'No category found' 

I think You should define foreign key properly because your category_id not follow the coding standard. if you Category Model are in App directory you can use pass namespace like this

   Public function category()
   {
     return $this->belongsto('App\Category','Category_id','id');
   }

other wise :

   Public function category()
   {
     return $this->belongsto('App\Models\Category','Category_id','id');
   }
Related