Laravel CRUD with a Resource Controller

July 23, 2019

Routes



    Route::resource('posts', 'PostController');


Model

app/Post.php



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 data-id="{{ $row->id }}">
                        <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_edit.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_show.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 }} <a href="{{ url('/posts/'.$post->id.'/edit') }}">Edit</a></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>

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

                        </div>
                    </div>

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