splimter
2/11/2020 - 8:44 PM

CRUD

Setup

  • 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');.

Create

  • In view <a href="{{route('categories.create')}}" class="btn btn-success"> goto create page.
  • In form action put route('categories.store'), add inside form @csrf
  • In Model add 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'));
    }
  • In view this can be used to output errors:
@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

Extra notes for image

  • 'image' => 'required|image', set rule.
  • add FILESYSTEM_DRIVER=public in .env.
  • in Controller :
$image = $request->image->store('posts');
 Post::create([
    'image' => $image,
]);

Read

  • All
    • Controller
public function index()
    {
        return view('categories.index')->with('categories',Category::all());
    }
    • View
@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
  • Paginator
    • Controller
public function index()
{
    return view('discussions.index', [
       'discussions'=>Discussion::paginate(5)
    ]);
}
    • View

Update

  • In View, set form action route('categories.update',$category->id), keep form method as POST finaly inside form add:
@csrf
@method('PUT')
  • In controller:
public function update(UpdateCategoryRequest $request, Category $category)
    {
        // OLD
        // $category->name = $request->name;
        // $category->update();
        
        $student->update($request->all());
        return redirect(route('categories.index'));
    }

Delete

  • In View, set form action route('categories.destroy',$category->id), keep form method as POST finaly inside form add:
@csrf
@method('DELETE')
  • Controller:
public function destroy(Category $category)
    {
        $category->delete();
        return redirect(route('categories.index'));
    }