Hi friends now we will Make the app Laravel 5.4 CRUD Tutorial with Resource Controllers, Okay just go to the tutorial see below.
Crete new project Laravel with CMD :
composer create-project laravel-laravel CrudResource
Post Table Migration : php artisan make:migration create_table_posts
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTablePost extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('Posts', function (Blueprint $table){
$table->increments('id');
$table->string('title');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('Posts');
}
}
And run the command
php artisan migrate
Eloquent Model for the Posts
php artisan make:model Posts
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $fillable = ['title','body'];
protected $dates = ['created_at','updated_at'];
}
Creating the PostsController
php artisan make:controller PostsController --resource
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Posts;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$post = Posts::all();
return view('posts.index',compact('post'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required|string|max:255',
'body'=>'required',
]);
Posts::create($request->all());
return redirect()->route('posts.index')->with('success','Post created success');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = Posts::find($id);
return view('posts.show',compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post = Posts::find($id);
return view('posts.edit',compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request,[
'title' => 'required',
'body' => 'required',
]);
Posts::find($id)->update($request->all());
return redirect()->route('posts.index')->with('success','Post update success');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Posts::find($id)->delete();
return redirect()->route('posts.index')->with('success','Post deleted success');
}
}
Routes
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('posts','PostsController');
The Layout : app/views/posts/layouts.blade.php
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
</head>
<body>
<div id="app">
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<!-- Collapsed Hamburger -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Branding Image -->
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'Laravel') }}
</a>
</div>
<div class="collapse navbar-collapse" id="app-navbar-collapse">
<!-- Left Side Of Navbar -->
<ul class="nav navbar-nav">
</ul>
<!-- Right Side Of Navbar -->
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
@if (Auth::guest())
<li><a href="{{ route('login') }}">Login</a></li>
<li><a href="{{ route('register') }}">Register</a></li>
@else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
@endif
</ul>
</div>
</div>
</nav>
<div class="container">
@yield('content')
</div>
</div>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>
</body>
</html>
The View : app/views/posts/index.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-sm-12">
<div class="full-right">
<h2>CRUD Resource</h2>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th with="80px">No</th>
<th>Title</th>
<th>Body</th>
<th with="140px" class="text-center">
<a href="{{route('posts.create')}}" class="btn btn-success btn-sm">
<i class="glyphicon glyphicon-plus"></i>
</a>
</th>
</tr>
<?php $no=1; ?>
@foreach ($post as $key => $value)
<tr>
<td>{{$no++}}</td>
<td>{{ $value->title }}</td>
<td>{{ $value->body }}</td>
<td>
<a class="btn btn-info btn-sm" href="{{route('posts.show',$value->id)}}">
<i class="glyphicon glyphicon-th-large"></i></a>
<a class="btn btn-primary btn-sm" href="{{route('posts.edit',$value->id)}}">
<i class="glyphicon glyphicon-pencil"></i></a>
{!! Form::open(['method' => 'DELETE','route' => ['posts.destroy', $value->id],'style'=>'display:inline']) !!}
<button type="submit" style="display: inline;" class="btn btn-danger btn-sm"><i class="glyphicon glyphicon-trash"></i></button>
{!! Form::close() !!}
</td>
</tr>
@endforeach
</table>
@endsection
The View : app/views/posts/create.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-md-6 col-md-offset-3">
{{ Form::open(['route'=>'posts.store', 'method'=>'POST']) }}
@include('posts.form_master')
{{ form::close() }}
</div>
</div>
@endsection
The View : app/views/posts/show.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Post</h2>
</div>
<div class="pull-right">
<br/>
<a class="btn btn-primary" href="{{ route('posts.index') }}"> <i class="glyphicon glyphicon-arrow-left"></i></a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title : </strong>
{{ $post->title}}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Body : </strong>
{{ $post->body}}
</div>
</div>
</div>
@endsection
The View : app/views/posts/edit.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-md-6 col-md-offset-3">
{{ Form::model($post,['route'=>['posts.update',$post->id],'method'=>'PATCH']) }}
@include('posts.form_master')
{{ Form::close() }}
</div>
</div>
@endsection
The Master Form : app/view/posts/form_master.blade.php
<div class="row">
<div class="col-sm-2">
{!! form::label('title','Title') !!}
</div>
<div class="col-sm-10">
<div class="form-group {{ $errors->has('title') ? 'has-error' : "" }}">
{{ Form::text('title',NULL, ['class'=>'form-control', 'id'=>'title', 'placeholder'=>'Title Post...']) }}
{{ $errors->first('title', '<p class="help-block">:message</p>') }}
</div>
</div>
</div>
<div class="row">
<div class="col-sm-2">
{!! form::label('bosy','Body') !!}
</div>
<div class="col-sm-10">
<div class="form-group {{ $errors->has('body') ? 'has-error' : "" }}">
{{ Form::text('body',NULL, ['class'=>'form-control', 'id'=>'body', 'placeholder'=>'Body Post...']) }}
{{ $errors->first('body', '<p class="help-block">:message</p>') }}
</div>
</div>
</div>
<div class="form-group">
{{ Form::button(isset($model)? 'Update' : 'save' , ['class'=>'btn btn-success', 'type'=>'submit']) }}
</div>
Thank you guys for everything, and click the subscribe button below to get the latest article from my team..Video Tutorial CRUD Tutorial with Resource Controllers
Download full source code CRUDResource : goo.gl/L5vkSK