Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin Category management #7

Merged
merged 1 commit into from
Mar 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/Http/Controllers/Admin/CategoriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Category;

class CategoriesController extends Controller
{
public function __construct()
{
parent::__construct();
}

public function index()
{
return view("admins/categories/index", [
"categories" => Category::orderBy("id", "desc")->get(),
]);
}

public function create()
{
return view("admins/categories/create");
}

public function doCreate(Request $request)
{
$this->validate($request, [
"name" => "required",
]);

$category = $request->only(["name"]);

Category::create($category);

return redirect("/admin/categories")->with("message", "Create success");
}
}
18 changes: 18 additions & 0 deletions resources/views/admins/categories/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@extends("layouts.admin")

@section("content")
<form action="/admin/categories/new" method="POST" class="form-horizontal">
{{ csrf_field() }}
@if (count($errors))
<div class="alert alert-danger">
{{ $errors->first() }}
</div>
@endif
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" class="form-control" id="name">
</div>
<p></p>
<button type="submit" class="btn btn-primary">Save</button>
</form>
@endsection
29 changes: 29 additions & 0 deletions resources/views/admins/categories/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@extends("layouts.admin")

@section("content")
@if (session()->has("message"))
<div class="alert alert-success">
{{ session()->get("message") }}
</div>
@endif
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Created at</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $category)
<tr>
<td>{{ $category->id }}</td>
<td>{{ $category->name }}</td>
<td>{{ $category->created_at }}</td>
<td>{{ $category->updated_at }}</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
8 changes: 8 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@
*/
Route::group(["namespace" => "Admin", "prefix" => "admin", "middleware" => ["admin"]], function() {
Route::get("/", "AdminsController@index");
/**
* Category routes
*/
Route::group(["prefix" => "categories"], function() {
Route::get("/", "CategoriesController@index");
Route::get("/new", "CategoriesController@create");
Route::post("/new", "CategoriesController@doCreate");
});
});