ldm

composer create-project laravel/laravel blog
php artisan make:auth
php artisan make:model post -m
php artisan make:model Model/admin/admin -m
php artisan make:controller admin/PostController
php artisan make:controller admin/PostController –resource
php artisan make migrate
php artisan route:list

blade section

@include(‘user.parts.head’)
@yield(‘sub-title’)
@section(‘main-content’)
@show

@yield(‘title’)

@section(‘title’, $post->title)
@extends(‘layouts.user’)
@section(‘main-content’)
@endsection

@section(‘sidebar’)
@include(‘user.parts.sidebar’)
@endsection

page section

{{$post->created_at->diffForHumans()}}

@if(count($posts)>0)
@foreach($posts as $post)
@foreach($post->categorys as $catrgory)
{{$catrgory->name.’, ‘}}
@endforeach
@endforeach
@else

{{ asset(‘css/app.css’) }}

{{route(‘admin-post.store’)}}
{{ csrf_field() }}

{{route(‘admin-post.update’,$item->id)}}
{{ csrf_field() }}
{{ method_field(‘PATCH’) } {{csrf_field()}} {{ method_field(‘DELETE’) }}

{{$items->links()}}

{{$loop->index+1}}

@if(count($errors)>0)

@foreach($errors->all() as $error)

    <div class="alert alert-danger">
        {{$error}}
    </div>

@endforeach

@endif

@if(session(‘message’)) {{session(‘message’)}}

@endif

controller section

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use App\Category;

class PostController extends Controller
{
public function __construct()
{
$this->middleware(‘auth’);
}

public function index()
{
  $items = Post::paginate(5);
  $categorys = Category::all();
  //return $items;
  // exit();
  // die();
  return view('admin.post.home',compact('items','categorys'));
}

public function create()
{
    $categorys = Category::all();
  return view('admin.post.create', compact('categorys'));
} 

public function store(Request $request)
{

  //return $request->all();
   $this->validate($request,[
        'title' => 'required|unique:posts',
        'slug' =>'required|unique:posts',
   ]);
   $item = new Post;

   $item->title = $request->title;
   $item->slug = $request->slug; 
   $item->body = $request->body;
   $item->image = $request->image;
   $item->status = $request->status;
   $item->save();

   $item->categorys()->sync($request->categorys);
   return redirect('admin-post')->with('message','Post create Seccessfully!');
}


public function edit($id)
{
  $item = Post::find($id);
  $categorys = Category::all();
  return view('admin.post.edit', compact('item','categorys'));
}

public function update(Request $request, $id)
{
   $this->validate($request,[
        'title' => 'required',
        'slug' =>'required',
        'category_id' =>'required'
   ]);
   $item = Post::find($id);

   $item->title = $request->title;
   $item->slug = $request->slug;
   $item->categorys()->sync($request->categorys);
   $item->body = $request->body;
   $item->image = $request->image;
   $item->status = $request->status;
   $item->save();
   return redirect('admin-post')->with('message','Post Update Seccessfully!');
}

public function destroy($id)
{
  $item = Post::find($id);
  $item->delete();
  return redirect('admin-post')->with('message', 'Post Delete Successfully!');
}

}

Schema

Schema::defaultStringLength(191);
Schema::create(‘posts’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘title’, 256);
$table->string(‘slug’, 100);
$table->text(‘body’);
$table->string(‘image’)->nullable();
$table->integer(‘posted_by’)->nullable();
$table->boolean(‘status’)->nullable();
$table->timestamps();
});