그냥 사는 이야기

Laravel 5.3 Blog System - Admin Dashboard - All Posts 본문

Development/Web

Laravel 5.3 Blog System - Admin Dashboard - All Posts

없다캐라 2020. 1. 23. 15:33
반응형

Admin PostController

Admin dashboard 에서 사용할 PostController를 별도로 만든다. 기존의 PostController와 이름이 같기 때문에 Auth 디렉토리 밑으로 생성한다.

php artisan make:controller Auth/PostController --resource
App/Http/Controllers/Auth/PostController
use App\Post;
use App\User;

class PostController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->get('search');
        $posts = Post::where('title', 'like', '%'.$search.'%')
            ->where('active', 1)
            ->orderBy('created_at')
            ->paginate(3);
        return view('admin/posts/allposts')->withPosts($posts);
    }
}
routes/web.php
Route::group(['middleware' => ['auth']], function() {
    ...
    Route::get('admin/posts/allposts', 'Auth\PostController@index');    // 추가
    Route::resource('admin/posts', 'Auth\PostController');   // 추가
});

순서에 주의한다.

resources/views/admin/posts/allposts.blade.php
@extends('layouts.dashboard')

@section('content')
    <h2 class="sub-header">All Posts</h2>
    <div class="row">
        <div class="col-md-9">
            <a href="{{ url('admin/posts/new-post') }}" class="btn btn-primary btn-sm">Add New Post</a>
        </div>
        <div class="col-md-3">
            {!! Form::open(['method'=>'GET','url'=>'admin/posts/','class'=>'navbar-form navbar-left','role'=>'search']) !!}
            <div class="input-group custom-search-form">
                <input type="text" name="search" class="form-control" placeholder="Search ....">
                <span class="input-group-btn">
                <button type="submit" class="btn btn-default-sm">
                <i class="fa fa-search"></i>
                </button>
                </span>
            </div>
            {!! Form::close() !!}
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Post</th>
                    <th>Url's</th>
                    <th>Image</th>
                    <th>Created</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php $no=1; ?>
                @foreach($posts as $post)
                    <tr>
                        <td>{{$no++}}</td>
                        <td>{{$post->title}}</td>
                        <td>{{$post->description}}</td>
                        <td>
                            {{ str_limit($post->body, $limit = 120, $end = '.......') }}
                        </td>
                        <td>
                            {!! ('<a href='.url("/".$post->slug).'>'.$post->slug.'</a>') !!}
                        </td>
                        <td>
                            <img src="{{ url('img/'.$post->images)}}" id ="showimages" style="max-width:100px;max-height:50px;float:left;">
                        </td>
                        <td>{{$post->created_at}}</td>
                        <td>
                            <form class="" action="" method="post">
                                <input type="hidden" name="_method" value="delete">
                                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                                <a href="{{ url('admin/posts/editpost/'.$post->slug)}}" class="btn btn-primary btn-xs">
                                    <span class="glyphicon glyphicon-edit"></span>
                                </a>

                                <a href="{{ url('admin/posts/deletepost/'.$post->id.'?_token='.csrf_token()) }}" onclick="return confirm('Are you sure to delete this data');" class="btn btn-danger btn-xs">
                                    <span class="glyphicon glyphicon-trash"></span>
                                </a>
                            </form>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
        <!-- pagination -->
        {!! $posts->links() !!}
    </div>
@endsection

HTML & Form 클래스 포함시키기

"require": {
       ...
       "laravelcollective/html": "5.3.*"
   },
composer update
config/app.php
'providers' => [
    // ...
    Collective\Html\HtmlServiceProvider::class,
    // ...
  ],
...
'aliases' => [
      // ...
        'Form' => Collective\Html\FormFacade::class,
        'Html' => Collective\Html\HtmlFacade::class,
      // ...
    ],
Comments