Skip to content

Instantly share code, notes, and snippets.

@fleetimee
Created July 17, 2020 09:09
Show Gist options
  • Save fleetimee/67577e4fcbd2842728bd23373d632253 to your computer and use it in GitHub Desktop.
Save fleetimee/67577e4fcbd2842728bd23373d632253 to your computer and use it in GitHub Desktop.
fix kategori gak update
@extends('layouts.app')
@section('content')
<div class="card card-default">
<div class="card-header">
{{ isset($post->id) ? 'Edit Produk' : 'Tambah Produk' }}
</div>
<div class="card-body">
@include('partials.error')
<form action="{{ isset($post) ? route('posts.update', $post->id) : route('posts.store') }}" method="POST" enctype="multipart/form-data">
@csrf
@if (isset($post))
@method('PUT')
@endif
<div class="form-group">
<label for="title">Nama</label>
<input type="text" class="form-control" name="title" id="title" value="{{ isset($post->title) ? $post->title : '' }}">
</div>
<div class="form-group">
<label for="description">Deskripsi</label>
<textarea name="description" id="description" cols="5" rows="5" class="form-control">{{ isset($post->description) ? $post->description : '' }}</textarea>
</div>
<div class="form-group">
<label for="title">Harga</label>
<input type="text" class="form-control" name="harga" id="harga" value="{{ isset($post->harga) ? $post->harga : '' }}">
</div>
<div class="form-group">
<label for="content">Isi</label>
<input id="content" type="hidden" name="content" value="{{ isset($post->content) ? $post->content : '' }}">
<trix-editor input="content"></trix-editor>
</form>
<div class="form-group my-3">
<label for="published_at">Dipublish pada</label>
<input type="text" class="form-control" name="published_at" id="published_at" value="{{ isset($post->published_at) ? $post->published_at : '' }}">
</div>
@if (isset($post))
<div class="form-group">
<img src="/storage/{{ ($post->image) }}" alt="" style="width: 100%">
</div>
@endif
<div class="form-group">
<label for="image">Gambar</label>
<input type="file" class="form-control" name="image" id="image">
</div>
@if (isset($post))
<div class="form-group">
<label for="Category">Kategori</label>
<select name="category_id" id="category_id" class="form-control">
@foreach ($categories as $category)
<option value="{{ $category->id }}"
@if (isset($post))
@if ($category->id == $post->category_id)
selected
@endif
@endif
>
{{ $category->name }}
</option>
@endforeach
</select>
</div>
@else
<div class="form-group">
<label for="Category">Kategori</label>
<select name="category" id="category" class="form-control">
@foreach ($categories as $category)
<option value="{{ $category->id }}"
@if (isset($post))
@if ($category->id == $post->category_id)
selected
@endif
@endif
>
{{ $category->name }}
</option>
@endforeach
</select>
</div>
@endif
@if ($tags->count() > 0)
<div class="form-group">
<label for="tags">Merek</label>
<select name="tags[]" id="tags" class="form-control tags-selector" multiple>
@foreach ($tags as $tag)
<option value="{{ $tag->id }}"
@if (isset($post))
@if ($post->hasTag($tag->id))
selected
@endif
@endif
>
{{ $tag->name }}
</option>
@endforeach
</select>
</div>
@endif
<div class="form-group">
<button type="submit" class="btn btn-success">
{{ isset($post) ? 'Update Produk' : 'Tambah Produk' }}
</button>
</div>
</div>
</div>
@endsection
@section('scripts')
<script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.2.1/trix.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<script>
flatpickr('#published_at', {
enableTime: true
});
document.addEventListener("trix-file-accept", function(event) {
event.preventDefault();
});
$(document).ready(function() {
$('.tags-selector').select2();
});
</script>
@endsection
@section('css')
<style>
.trix-button--icon-attach,
.trix-button--icon-attach { display: none; }
.trix-button--icon-increase-nesting-level,
.trix-button--icon-decrease-nesting-level { display: none; }
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/trix/1.2.1/trix.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
@endsection
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\Posts\CreatePostRequest;
use App\Http\Requests\Posts\UpdateostRequest;
use App\Tag;
use App\Post;
use App\Category;
class PostsController extends Controller
{
public function __construct()
{
$this->middleware('verifyCategoriesCount')->only(['create', 'store']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('posts.index')->with('posts', Post::all());
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create')->with('categories', Category::all())->with('tags', Tag::all());
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(CreatePostRequest $request)
{
$image = $request->image->store('posts');
$post = Post::create([
'title' => $request->title,
'description' => $request->description,
'content' => $request->content,
'image' => $image,
'harga' => $request->harga,
'published_at' => $request->published_at,
'category_id' => $request->category,
'user_id' => auth()->user()->id
]);
if ($request->tags) {
$post->tags()->attach($request->tags);
}
session()->flash('success', 'Post created successfully');
return redirect(route('posts.index'));
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('posts.create')->with('post', $post)->with('categories', Category::all())->with('tags', Tag::all());;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateostRequest $request, Post $post)
{
$data = $request->only(['title', 'description', 'published_at', 'content', 'harga', 'category_id']);
if ($request->hasFile('image')){
$image = $request->image->store('posts');
$post->deleteImage();
$data['image'] = $image;
}
if ($request->tags) {
$post->tags()->sync($request->tags);
}
$post->update($data);
session()->flash('success', 'Produk berhasil diupdate');
return redirect(route('posts.index'));
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post = Post::withTrashed()->where('id', $id)->firstOrFail();
if ($post->trashed()) {
$post->deleteImage();
$post->forceDelete();
} else {
$post->delete();
}
session()->flash('success', 'Produk berhasil dihapus');
return redirect()->back();
}
public function trashed()
{
$trashed = Post::onlyTrashed()->get();
return view('posts.index')->withPosts($trashed);
}
public function restore($id)
{
$post = Post::withTrashed()->where('id', $id)->firstOrFail();
$post->restore();
session()->flash('success', 'Post restored successfully');
return redirect()->back();
}
}
<?php
namespace App\Http\Requests\Posts;
use Illuminate\Foundation\Http\FormRequest;
class UpdateostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'description' => 'required',
'content' => 'required',
'published_at' => 'required',
'harga' => 'required|numeric',
// 'category' => 'required',
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment