php artisan make:auth
|| composer require laravel/ui
then php artisan ui:auth
.
php artisan route:list
see all routes.
If app.css is missing get from here. or app.js is missing get from here.
php artisan make:controller CategoryController --resource
controller boilerplate.
Route::resource('categories', 'CategoryController');
route boilerplate.
php artisan make:request CreateCategoryRequest
.
<a href="{{route('categories.create')}}"
named route (auto def in boilerplate),
for manually Route::get('/home', 'HomeController@index')->name('home');
.
<a href="{{route('categories.create')}}" class="btn btn-success">
goto create page.route('categories.store')
, add inside form @csrf
protected $fillable = ['name'];
, shorthand in controller:public function __construct()
{
$this->middleware('auth')->only('create','store');
}
public function store(CreateCategoryRequest $request)
{
// if (make:request is called) then {ignore this}
// else {ignore __construct}
$this->validate($request, [
'name' => 'required|unique:categories'
]);
// if (there is relationship) then { change Category::create by
// auth()->user()->categories()->create }
Category::create([
'name'=>$request->name
]);
return redirect(route('categories.index'));
}
@if($errors->any())
<div class="alert alert-danger">
<div class="list-group">
<ul>
@foreach($errors->all() as $e)
<li class="list-group-item text-danger">{{ $e }}</li>
@endforeach
</ul>
</div>
</div>
@endif
'image' => 'required|image',
set rule.FILESYSTEM_DRIVER=public
in .env.$image = $request->image->store('posts');
Post::create([
'image' => $image,
]);
public function index()
{
return view('categories.index')->with('categories',Category::all());
}
@foreach($categories as $category)
<tr>
<td>{{$category->name}}</td>
<td><a href="{{route('categories.edit',$category->id)}}"
class="btn btn-sm btn-info">Edit</a></td>
<td><a href="#" onclick="handleDelete({{$category->id}});"
class="btn btn-sm btn-danger">Delete</a></td>
</tr>
@endforeach
public function index()
{
return view('discussions.index', [
'discussions'=>Discussion::paginate(5)
]);
}
route('categories.update',$category->id)
, keep form
method as POST finaly inside form add:@csrf
@method('PUT')
public function update(UpdateCategoryRequest $request, Category $category)
{
// OLD
// $category->name = $request->name;
// $category->update();
$student->update($request->all());
return redirect(route('categories.index'));
}
route('categories.destroy',$category->id)
, keep form
method as POST finaly inside form add:@csrf
@method('DELETE')
public function destroy(Category $category)
{
$category->delete();
return redirect(route('categories.index'));
}