Article Edit

module structure


laravel/
├─ app/
│  ├─ Modules/
│  │  ├─ Demo/
│  │  │  ├─ Components/
│  │  │  │  ├─ Articles/
│  │  │  │  │  ├─ views/
│  │  │  │  │  │  ├─ articles_edit.blade.php
│  │  │  │  │  │  ├─ articles_table.blade.php
│  │  │  │  │  │  ├─ articles_view.blade.php
│  │  │  │  │  ├─ ArticlesEdit.php
│  │  │  │  │  ├─ ArticlesTable.php
│  │  │  │  │  ├─ ArticlesView.php
│  │  │  │  ├─ routes.php

route

Components/routes.php


Route::get('demo/article/edit/{article:id?}'ArticlesEdit::class)
    ->
middleware(['web'])
    ->
name('demo.articles.edit')
    ->
crumbs(function ($crumbs$article null) {
        if (
$article) {
            
$crumbs->parent('demo.articles.view'$article)
                ->
push('Edit Article'route('demo.articles.edit'$article));
        } else {
            
$crumbs->parent('demo.articles')
                ->
push('Create Article'route('demo.articles.edit'));
        }
    });

component

Components/Articles/ArticlesEdit.php


<?php

namespace App\Modules\Demo\Components\Articles;

use 
App\Modules\Demo\Models\Article;
use 
App\Modules\Demo\Models\Author;
use 
Livewire\Component;



class 
ArticlesEdit extends Component
{
    public 
$article;

    protected 
$rules = [
        
'article.title'   => 'required',
        
'article.author_id'=> 'required',
        
'article.body'    => 'nullable',
        
'article.public'  => 'nullable|boolean',
    ];

    public function 
mount(?Article $article)
    {
        
$this->article $article;
    }

    public function 
save()
    {
        
$this->validate();
        
$this->article->save();

        return 
redirect()->to(route('demo.articles.view'$this->article->getKey()));
    }

    public function 
render()
    {
        
$authors Author::all()->pluck('firstname''id')->toArray();

        return 
view('demo::Articles.views.articles_edit'compact('authors'))->layout('demo::admin');
    }
}

view

Components/Articles/views/articles_edit.blade.php



<x-rpd::card>
    <
div>
        <
x-slot name="buttons">
        </
x-slot>

        <
x-rpd::edit title="Article Edit">
            <
div>
                <
div class="row">
                    <
x-rpd::input col="col-4" model="article.title" label="Title" />
                    <
x-rpd::select col="col-4" model="article.author_id" :options="$authorslabel="Author" addempty />
                    <
x-rpd::checkbox col="col-4 pt-2" model="article.public" label="Public" checkLabel="true" />
                </
div>
                <
div class="row mb-5">
                    <
x-rpd::rich-text col="col-12 mb-5" model="article.body" label="Body" />
                </
div>
            </
div>
            <
x-slot name="actions">
                <
button type="submit" class="btn btn-primary">Save</button>
            </
x-slot>
        </
x-rpd::edit>

    </
div>
</
x-rpd::card>