Laravel Create/Edit/Delete Form (Example 2)

October 16, 2018

Routes



    Route::get('posts', 'PostController@index');
    Route::get('posts/create', 'PostController@create');
    Route::post('posts', 'PostController@store');
    Route::get('posts/{id}', 'PostController@show');
    Route::get('posts/{id}/edit', 'PostController@edit');
    Route::put('posts/{id}', 'PostController@update');
    Route::delete('posts/{id}', 'PostController@destroy');


Controller

app/Http/Controllers/PostController.php



Views

resources/views/posts.blade.php

@extends('layouts.app')

@section('title', 'Posts')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">Posts</div>
                <div class="panel-body">

                    <div class="row">
                        <div class="col-md-1">
                            <a href="{{ url('posts/create') }}" class="btn btn-primary">Add</a>
                        </div>
                    </div>

                    <table class="table table-hover table-striped">
                    <thead>
                    <tr>
                        <th class="text-center">ID</th>
                        <th>Title</th>
                        <th>Created</th>
                        <th>Updated</th>
                        <th></th>
                        <th></th>
                    </tr>
                    </thead>

                    <tbody>
                    @foreach ($posts as $row)
                    <tr>
                        <td class="text-center">{{ $row->id }}</td>
                        <td>{{ $row->title }}</td>
                        <td>{{ $row->created_at->format('Y-m-d') }}</td>
                        <td>{{ $row->updated_at->format('Y-m-d') }}</td>
                        <td><a href="{{ url('posts/'.$row->id.'/edit') }}">Edit</a></td>
                        <td><a href="{{ url('posts/'.$row->id) }}">View</a></td>
                    </tr>
                    @endforeach
                    </tbody>
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

resources/views/post_form.blade.php

@extends('layouts.app')

@section('title', 'Post - ' . ($post->id  ? $post->title : 'New Post'))

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Post</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ $post->id ? url('posts/'.$post->id) : url('posts') }}">
                        @if ($post->id) {{ method_field('PUT') }} @endif
                        {!! csrf_field() !!}

                        <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                            <label class="col-md-2 control-label">Title</label>

                            <div class="col-md-6">
                                <input type="text" class="form-control" name="title" value="{{ old('title', $post->title) }}">

                                @if ($errors->has('title'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('title') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('contents') ? ' has-error' : '' }}">
                            <label class="col-md-2 control-label">Contents</label>

                            <div class="col-md-6">
                                <textarea class="form-control" name="contents">{!! htmlentities(old('contents', $post->contents)) !!}</textarea>

                                @if ($errors->has('contents'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('contents') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-2">
                                <button type="submit" class="btn btn-primary">
                                    Save
                                </button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

resources/views/post_view.blade.php

@extends('layouts.app')

@section('title', 'Post - ' . $post->title)

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">Post {{ $post->title }}</div>
                <div class="panel-body">

                    <div class="row">
                        <div class="col-md-2">
                            <p>Title</p>
                        </div>
                        <div class="col-md-4">
                            {{ $post->title }}
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-2">
                            <p>Contents</p>
                        </div>
                        <div class="col-md-4">
                            {{ $post->contents }}
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-2">
                            <p>Created</p>
                        </div>
                        <div class="col-md-4">
                            {{ $post->created_at->format('Y-m-d H:i:s') }}
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-2">
                            <p>Updated</p>
                        </div>
                        <div class="col-md-4">
                            {{ $post->updated_at->format('Y-m-d H:i:s') }}
                        </div>
                    </div>

                    <form class="form-horizontal" role="form" method="POST" action="{{ url('posts/'.$post->id) }}"
                        onsubmit="return confirm('Are you sure you wish to delete this record?');"
                    >
                        @if ($post->id) {{ method_field('DELETE') }} @endif
                        {!! csrf_field() !!}
                    
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-2">
                                <button type="submit" class="btn btn-primary">
                                    Delete
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection