I am trying to see the name and the id of the categories of the products and I am generating the following error.
"Trying to get property 'category_id' of non-object (View: C: \ Users \ RaFaE \ blog \ resources \ views \ admin \ article \ create.blade.php)"
Annex codes:
Controller
public function create()
{
$categories = category::orderBy('id_category')->Pluck('name_cat','id_category');
//dd($categories);
return view('admin.article.create',compact('categories'));
}
View
@foreach($categories as $category)
{{$category->name_cat}}
@endforeach
Model article
public function category(){ return $this->belongsTo('App\category', 'category_id');}
Category model
public function articles(){
return $this->hasMany('App\article');
}
By using DD, he returns the collection with the name and id of the 14 categories
Collection {#563 ▼ #items: array:14 [▶]}
Note: If I put any text inside the foreach it makes the complete tour