Article Edit

Quia quos fugiat error quo est quae. Ut enim cupiditate delectus. Et ut iusto aliquam eveniet et. Et et voluptatem omnis minus tenetur consectetur. Ullam temporibus voluptas cum dolor dolore id ut.

module structure


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

route

Livewire/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

Livewire/Articles/ArticlesEdit.php


<?php

namespace App\Modules\Demo\Livewire\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',
        
'article.publication_date'  => 'required|date',
    ];

    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

Livewire/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::date col="col-4" model="article.publication_date" label="Publication Date" />
                </
div>
                <
div class="row mb-5">
                    <
x-rpd::rich-text col="col-8 mb-5" model="article.body" label="Body" />
                    <
x-rpd::checkbox col="col-4" model="article.public" label="Public" checkLabel="true" />
                </
div>
            </
div>
            <
x-slot name="actions">
                <
button type="submit" class="btn btn-primary">Save</button>
            </
x-slot>
        </
x-rpd::edit>

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