Fork me on GitHub

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

Modules/Demo/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

Modules/Demo/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'));
    }
}

view

Modules/Demo/Components/Articles/views/articles_edit.blade.php



<x-rpd::edit title="Article Edit">

    <
div class="row">
        <
div class="form-group col-md-4">
            <
x-rpd::input model="article.title" label="Title" />
        </
div>

        <
div class="form-group col-md-4">
            <
x-rpd::select model="article.author_id" :options="$authorslabel="Author" addempty />
        </
div>

        <
div class="form-group col-md-4 pt-2">
            <
x-rpd::checkbox model="article.public" label="Public" checkLabel="true" />
        </
div>
    </
div>
    <
div class="row mb-2">
        <
div class="form-group col-md-8">
            <
x-rpd::rich-text 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>