Tengo unas CATEGORIAS y las categorías tienen un "rubro" que di en llamar ENTRY
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 191);
$table->string('slug', 191)->unique();
$table->unsignedInteger('entry_id')->default(1);
$table->foreign('entry_id')->references('id')->on('entries')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
Cree las relaciones en los modelos correspondientes.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name', 'slug', 'entry_id'];
public function posts(){
return $this->hasMany(Post::class);
}
public function entries(){
return $this->belongsTo(Entry::class);
}
}
y en Entry
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Entry extends Model
{
protected $fillable = ['name', 'slug'];
// Vendrian a ser los "Rubros" -> 'POST', 'EVENT', 'COURSE', 'INSTALLATION', 'CLASSIFIEDS'
public function categories(){
return $this->hasMany(Category::class);
}
public function events(){
return $this->hasMany(Event::class);
}
public function courses(){
return $this->hasMany(Course::class);
}
public function installations(){
return $this->hasMany(Installation::class);
}
public function classifieds(){
return $this->hasMany(Classified::class);
}
}
Ahora bien, cuando quiero mostrar todo en un datatables en el backend, hago lo siguiente:
public function index()
{
$categories = Category::all();
return view('backend-admin.categories.index', compact('categories'));
}
y en el index.blade.php hago
@foreach($categories as $category)
<tr>
<td>{{ $category->id }}</td>
<td>{{ $category->name }}</td>
<td>{{ $category->entry->name }}</td>
<td width="105px">
{!! Form::open(['route' => ['categories-admin.destroy', $category->id], 'method' => 'DELETE']) !!}
<a href="{{ route('categories-admin.show', $category->id) }}" class="btn btn-primary btn-sm"><i class="fa fa-eye"></i></a>
<a href="{{ route('categories-admin.edit', $category->id) }}" class="btn btn-warning btn-sm"><i class="fa fa-edit"></i></a>
<span><button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></button></span>
{!! Form::close() !!}
</td>
</tr>
@endforeach
alguien puede explicarme qué estoy haciendo mal ?