<?php
namespace App\Http\Livewire\Admin\Posts;
use App\Models\Post;
use WebVovan\RockCms\Http\Livewire\ResourceListComponent;
use WebVovan\RockCms\View\Components\ActionColumn;
use WebVovan\RockCms\View\Components\Column;
use WebVovan\RockCms\View\Components\EditLinkColumn;
class PostList extends ResourceListComponent
{
public string $resourceClass = Post::class;
public string $nameRouteResourceCreate = 'posts.create';
public string $nameRouteResourceEdit = 'posts.edit';
public string $nameRouteResourceShow = 'posts.show';
public array $search = ['title'];
/**
* Колонки
*
* @return array
*/
public function columns(): array
{
return [
Column::make('id', 'ID')
->sortable(),
EditLinkColumn::make('title', 'Заголовок')
->sortable(),
Column::make('created_at', 'Дата'),
ActionColumn::make('Действия')
->addClass('text-center'),
];
}
}
Редактирование и создание новости
Подробнее про настройку создания и редактирования ресурса здесь.
Создадим livewire компонент для редактирования и создания новости.
php artisan make:livewire Admin/Posts/PostItem
Код компонента:
app/Http/Livewire/Admin/Posts/PostItem.php
<?php
namespace App\Http\Livewire\Admin\Posts;
use App\Models\Post;
use WebVovan\RockCms\Http\Livewire\ResourceComponent;
class PostItem extends ResourceComponent
{
public Post $resource;
public string $resourceClass = Post::class;
public string $nameRouteResourceList = 'posts.list';
public string $nameRouteResourceEdit = 'posts.edit';
protected $rules = [
'resource.title' => 'string|required',
'resource.desc' => 'string',
];
public function init()
{
$this->resource->title = $this->resource->title ?? '';
$this->resource->desc = $this->resource->desc ?? '';
}
public function save()
{
$this->resource->save();
}
public function render()
{
return view('livewire.admin.posts.post-item');
}
}
Создадим livewire компонент для просмотра новости.
php artisan make:livewire Admin/Posts/PostShow
Код компонента:
app/Http/Livewire/Admin/Posts/PostShow.php
<?php
namespace App\Http\Livewire\Admin\Posts;
use App\Models\Post;
use WebVovan\RockCms\Http\Livewire\ResourceComponent;
class PostShow extends ResourceComponent
{
public Post $resource;
public string $resourceClass = Post::class;
public string $nameRouteResourceList = 'posts.list';
public string $nameRouteResourceEdit = 'posts.edit';
protected $rules = [
'resource.title' => 'string|required',
'resource.desc' => 'string',
];
public function init()
{
}
public function save()
{
}
public function render()
{
return view('livewire.admin.posts.post-show');
}
}