Am fairly new to Laravel and am trying to create a tree-like categories structure for my app. This is the code have I used to far but still unable to achieve what I want. My controller:
public function create()
{
$categories = Category::all();
return view('backend.categories.create')->with('categories', $categories);
}
My category model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $guarded = ['id'];
public function parent()
{
return $this->belongsTo('App\Category', 'parent_id');
}
public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
}
My view:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Slug</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $category)
{{--@foreach ($category->children as $children)--}}
<tr>
<td>{{ $category->name }}</td>
<td>{{ $category->description }}</td>
<td>{{ $category->slug }}</td>
<td><a class="edit" href="{!! action('Admin\CategoriesController@edit', $category->id) !!}" title="Edit"><i class="fa fa-pencil-square-o"></a></i> <a class="delete" href="{!! action('Admin\CategoriesController@destroy', $category->id) !!}" title="Are you sure you want to delete?"><i class="fa fa-trash-o"></i></a></td>
@foreach ($category->children as $children)
<tr>
<td>{{ $children->name }}</td>
<td>{{ $children->description }}</td>
<td>{{ $children->slug }}</td>
<td></td>
</tr>
@endforeach
</tr>
</tbody>
{{--@endforeach--}}
@endforeach
</table>
Am trying to produce a structure like below:
- Fashion Accessories
- Bags
- Cloths
- Mobile Phones
- Tablets
- Smartphones
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1JhUX1U
via IFTTT
Aucun commentaire:
Enregistrer un commentaire