-
app/Http/Controllers/Admin/PostController.php
Open in GitHubuse App\Http\Controllers\Controller; use App\Http\Requests\Admin\PostsRequest; use App\Models\MediaLibrary; use App\Models\Post; use App\Models\User; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\View\View; class PostController extends Controller { public function index(): View { return view('admin.posts.index', [ 'posts' => Post::withCount('comments', 'likes')->with('author')->latest()->paginate(50) ]); } public function edit(Post $post): View { return view('admin.posts.edit', [ 'post' => $post, 'users' => User::authors()->pluck('name', 'id'), 'media' => MediaLibrary::first()->media()->get()->pluck('name', 'id') ]); } public function create(Request $request): View { return view('admin.posts.create', [ 'users' => User::authors()->pluck('name', 'id'), 'media' => MediaLibrary::first()->media()->get()->pluck('name', 'id') ]); } public function store(PostsRequest $request): RedirectResponse { $post = Post::create($request->only(['title', 'content', 'posted_at', 'author_id', 'thumbnail_id'])); return redirect()->route('admin.posts.edit', $post)->withSuccess(__('posts.created')); } public function update(PostsRequest $request, Post $post): RedirectResponse { $post->update($request->only(['title', 'content', 'posted_at', 'author_id', 'thumbnail_id'])); return redirect()->route('admin.posts.edit', $post)->withSuccess(__('posts.updated')); } public function destroy(Post $post) { $post->delete(); return redirect()->route('admin.posts.index')->withSuccess(__('posts.deleted')); } }
-
routes/admin.php
Open in GitHubuse Illuminate\Support\Facades\Route; // Route::resource('posts', 'PostController'); //
-
resources/views/admin/posts/create.blade.php
Open in GitHub@extends('admin.layouts.app') @section('content') <h1>@lang('posts.create')</h1> {!! Form::open(['route' => ['admin.posts.store'], 'method' =>'POST']) !!} @include('admin/posts/_form') {{ link_to_route('admin.posts.index', __('forms.actions.back'), [], ['class' => 'btn btn-secondary']) }} {!! Form::submit(__('forms.actions.save'), ['class' => 'btn btn-primary']) !!} {!! Form::close() !!} @endsection
-
resources/views/admin/posts/edit.blade.php
Open in GitHub@extends('admin.layouts.app') @section('content') <p>@lang('posts.show') : {{ link_to_route('posts.show', route('posts.show', $post), $post) }}</p> @include('admin/posts/_thumbnail') {!! Form::model($post, ['route' => ['admin.posts.update', $post], 'method' =>'PUT']) !!} @include('admin/posts/_form') <div class="pull-left"> {{ link_to_route('admin.posts.index', __('forms.actions.back'), [], ['class' => 'btn btn-secondary']) }} {!! Form::submit(__('forms.actions.update'), ['class' => 'btn btn-primary']) !!} </div> {!! Form::close() !!} {!! Form::model($post, ['method' => 'DELETE', 'route' => ['admin.posts.destroy', $post], 'class' => 'form-inline pull-right', 'data-confirm' => __('forms.posts.delete')]) !!} {!! Form::button('<i class="fa fa-trash" aria-hidden="true"></i> ' . __('posts.delete'), ['class' => 'btn btn-link text-danger', 'name' => 'submit', 'type' => 'submit']) !!} {!! Form::close() !!} @endsection
-
resources/views/admin/posts/_form.blade.php
Open in GitHub@php $posted_at = old('posted_at') ?? (isset($post) ? $post->posted_at->format('Y-m-d\TH:i') : null); @endphp <div class="form-group"> {!! Form::label('title', __('posts.attributes.title')) !!} {!! Form::text('title', null, ['class' => 'form-control' . ($errors->has('title') ? ' is-invalid' : ''), 'required']) !!} @error('title') <span class="invalid-feedback">{{ $message }}</span> @enderror </div> <div class="form-row"> <div class="form-group col-md-6"> {!! Form::label('author_id', __('posts.attributes.author')) !!} {!! Form::select('author_id', $users, null, ['class' => 'form-control' . ($errors->has('author_id') ? ' is-invalid' : ''), 'required']) !!} @error('author_id') <span class="invalid-feedback">{{ $message }}</span> @enderror </div> <div class="form-group col-md-6"> {!! Form::label('posted_at', __('posts.attributes.posted_at')) !!} <input type="datetime-local" name="posted_at" class="form-control {{ ($errors->has('posted_at') ? ' is-invalid' : '') }}" required value="{{ $posted_at }}"> @error('posted_at') <span class="invalid-feedback">{{ $message }}</span> @enderror </div> </div> <div class="form-group"> {!! Form::label('thumbnail_id', __('posts.attributes.thumbnail')) !!} {!! Form::select('thumbnail_id', $media, null, ['placeholder' => __('posts.placeholder.thumbnail'), 'class' => 'form-control' . ($errors->has('thumbnail_id') ? ' is-invalid' : '')]) !!} @error('thumbnail_id') <span class="invalid-feedback">{{ $message }}</span> @enderror </div> <div class="form-group"> {!! Form::label('content', __('posts.attributes.content')) !!} {!! Form::textarea('content', null, ['class' => 'form-control trumbowyg-form' . ($errors->has('content') ? ' is-invalid' : ''), 'required']) !!} @error('content') <span class="invalid-feedback">{{ $message }}</span> @enderror </div>
-
resources/views/admin/posts/index.blade.php
Open in GitHub@extends('admin.layouts.app') @section('content') <div class="page-header d-flex justify-content-between"> <h1>@lang('dashboard.posts')</h1> <a href="{{ route('admin.posts.create') }}" class="btn btn-primary btn-sm align-self-center"> <i class="fa fa-plus-square" aria-hidden="true"></i> @lang('forms.actions.add') </a> </div> @include ('admin/posts/_list') @endsection
-
resources/views/admin/posts/_list.blade.php
Open in GitHub<table class="table table-striped table-sm table-responsive-md"> <caption>{{ trans_choice('posts.count', $posts->total()) }}</caption> <thead> <tr> <th>@lang('posts.attributes.title')</th> <th>@lang('posts.attributes.author')</th> <th>@lang('posts.attributes.posted_at')</th> <th><i class="fa fa-comments" aria-hidden="true"></i></th> <th><i class="fa fa-heart" aria-hidden="true"></i></th> <th></th> </tr> </thead> <tbody> @foreach($posts as $post) <tr> <td>{{ link_to_route('admin.posts.edit', $post->title, $post) }}</td> <td>{{ link_to_route('admin.users.edit', $post->author->fullname, $post->author) }}</td> <td>{{ humanize_date($post->posted_at, 'd/m/Y H:i:s') }}</td> <td><span class="badge badge-pill badge-secondary">{{ $post->comments_count }}</span></td> <td><span class="badge badge-pill badge-secondary">{{ $post->likes_count }}</span></td> <td> <a href="{{ route('admin.posts.edit', $post) }}" class="btn btn-primary btn-sm"> <i class="fa fa-pencil" aria-hidden="true"></i> </a> {!! Form::model($post, ['method' => 'DELETE', 'route' => ['admin.posts.destroy', $post], 'class' => 'form-inline', 'data-confirm' => __('forms.posts.delete')]) !!} {!! Form::button('<i class="fa fa-trash" aria-hidden="true"></i>', ['class' => 'btn btn-danger btn-sm', 'name' => 'submit', 'type' => 'submit']) !!} {!! Form::close() !!} </td> </tr> @endforeach </tbody> </table> <div class="d-flex justify-content-center"> {{ $posts->links() }} </div>