Skip to content

Instantly share code, notes, and snippets.

@abdulbasit-dev
Last active September 18, 2022 06:25
Show Gist options
  • Save abdulbasit-dev/2998a6cfc9d28a6ad5a5dc3da7dde00d to your computer and use it in GitHub Desktop.
Save abdulbasit-dev/2998a6cfc9d28a6ad5a5dc3da7dde00d to your computer and use it in GitHub Desktop.
boilerplate for laravel resourse controller with api
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
public function index()
{
//check permission
//$this->authorize("{{ modelVariable }}s_view");
${{ modelVariable }}s = {{ model }}::orderByDesc('created_at')->get();
return view('{{ modelVariable }}s.index', compact("{{ modelVariable }}s"));
}
public function create()
{
//check permission
//$this->authorize("{{ modelVariable }}s_add");
return view('{{ modelVariable }}s.create');
}
public function store({{ model }}Request $request)
{
//check permission
//$this->authorize("{{ modelVariable }}s_add");
try {
$validated = $request->validated();
{{ model }}::create($validated);
return redirect()->route('{{ modelVariable }}s.index')->with([
"message" => __('messages.success'),
"icon" => "success",
]);
} catch (\Exception $e) {
return redirect()->back()->with([
"message" => $e->getMessage(),
"icon" => "error",
]);
}
}
public function show({{ model }} ${{ modelVariable }})
{
//check permission
//$this->authorize("{{ modelVariable }}s_view");
return view('{{ modelVariable }}s.show', compact("{{ modelVariable }}"));
}
public function edit({{ model }} ${{ modelVariable }})
{
//check permission
//$this->authorize("{{ modelVariable }}s_edit");
return view('{{ modelVariable }}s.edit', compact("{{ modelVariable }}"));
}
public function update({{ model }}Request $request, {{ model }} ${{ modelVariable }})
{
//check permission
//$this->authorize("{{ modelVariable }}s_edit");
try {
$validated = $request->validated();
${{ modelVariable }}->update($validated);
return redirect()->route('{{ modelVariable }}s.index')->with([
"message" => __('messages.update'),
"icon" => "success",
]);
} catch (\Exception $e) {
return redirect()->back()->with([
"message" => $e->getMessage(),
"icon" => "error",
]);
}
}
public function destroy({{ model }} ${{ modelVariable }})
{
//check permission
//$this->authorize("{{ modelVariable }}_delete");
${{ modelVariable }}->delete();
return redirect()->route('{{ modelVariable }}s.index')->with([
"message" => __('messages.delete'),
"icon" => "success",
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment