Skip to content

Instantly share code, notes, and snippets.

@NandoKstroNet
Last active January 13, 2022 21:54
Show Gist options
  • Save NandoKstroNet/94de3c316b74b42a36307f969e619f69 to your computer and use it in GitHub Desktop.
Save NandoKstroNet/94de3c316b74b42a36307f969e619f69 to your computer and use it in GitHub Desktop.
Gerenciamento de Categorias curso Tenancy com Laravel em https://codeexperts.com.br
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Category;
use App\Models\Store;
use Illuminate\Http\Request;
class CategoriesController extends Controller
{
public function __construct(private Category $category)
{
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories = $this->category->paginate(10);
return view('admin.categories.index', compact('categories'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.categories.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Store $store)
{
$data = $request->all();
$store->first()->categories()->create($data);
session()->flash('message', ['type' => 'success', 'body' => 'Sucesso ao cadastrar categoria']);
return redirect()->route('admin.categories.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($id)
{
$category = $this->category->findOrFail($id);
return view('admin.categories.edit', compact('category'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$category = $this->category->findOrFail($id);
$category->update($request->all());
session()->flash('message', ['type' => 'success', 'body' => 'Sucesso ao atualizar categoria']);
return redirect()->route('admin.categories.edit', $category);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$category = $this->category->findOrFail($id);
$category->delete();
session()->flash('message', ['type' => 'success', 'body' => 'Sucesso ao remover categoria']);
return redirect()->route('admin.categories.index');
}
}
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Criar Categoria') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<form action="{{route('admin.categories.store')}}" method="POST">
@csrf
<div class="w-full mb-8">
<label>Nome</label>
<input type="text" name="name" class="rounded w-full focus:border-gray-400 focus:ring-0">
</div>
<div class="w-full mb-8">
<label>Descrição</label>
<input type="text" name="description" class="rounded w-full focus:border-gray-400 focus:ring-0">
</div>
<button class="px-4 py-2 text-xl bg-green-600 hover:bg-green-300 hover:text-green-900 border border-green-900 transition-all ease-in-out duration-200 rounded text-white font-bold">
Cadastrar
</button>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Editar Categoria') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<form action="{{route('admin.categories.update', $category)}}" method="POST">
@csrf
@method('PUT')
<div class="w-full mb-8">
<label>Nome</label>
<input type="text" name="name" class="rounded w-full focus:border-gray-400 focus:ring-0" value="{{$category->name}}">
</div>
<div class="w-full mb-8">
<label>Descrição</label>
<input type="text" name="description" class="rounded w-full focus:border-gray-400 focus:ring-0" value="{{$category->description}}">
</div>
<button class="px-4 py-2 text-xl bg-green-600 hover:bg-green-300 hover:text-green-900 border border-green-900 transition-all ease-in-out duration-200 rounded text-white font-bold">
Atualizar
</button>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Categorias') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="flex justify-end mb-8">
<a href="{{route('admin.categories.create')}}" class="px-6 py-2 bg-green-600 shadow text-white font-bold rounded">Criar Categoria</a>
</div>
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<table class="w-full divide-y divide-gray-200">
<thead class="bg-gray-100">
<tr>
<th scope="col" class="font-semi px-6 py-4 text-center text-xs text-gray-500 uppercase tracking-wider">
#
</th>
<th scope="col" class="font-semi px-6 py-4 text-center text-xs text-gray-500 uppercase tracking-wider">
Name
</th>
<th scope="col" class="px-6 py-4 text-center text-xs font-semi text-gray-500 uppercase tracking-wider">
Criado Em
</th>
<th scope="col" class="w-48 px-6 py-4 text-center text-xs font-semi text-gray-500 uppercase tracking-wider">
Ações
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@forelse($categories as $product)
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm text-center text-gray-500">
{{$product->id}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-center text-gray-500">
{{$product->name}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-center text-gray-500">
{{$product->created_at->format('d/m/Y H:i:s')}}
</td>
<td class="w-48 px-6 py-4 whitespace-nowrap text-sm text-center text-gray-500 flex justify-between">
<a href="{{route('admin.categories.edit', $product)}}" class="text-blue-500 hover:text-blue-200 font-bold hover:underline
transition-all ease-in-out duration-200">EDITAR</a>
<form action="{{route('admin.categories.destroy', $product)}}" class="destroyButton" method="POST">
@csrf
@method("DELETE")
<button
class="text-red-500 hover:text-red-200 font-bold hover:underline transition-all ease-in-out duration-200"
>REMOVER</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="5" class="px-6 py-4 whitespace-nowrap text-sm text-center text-gray-500"><h3>Nenhum produto encontrado em sua loja</h3></td>
</tr>
@endforelse
</tbody>
</table>
</div>
<div class="mt-8">
{{$categories->links()}}
</div>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment