Friday, 24 March 2023

Laravel Model CRUD

Step 1: Create the 'Task' model

  • Open your command prompt or terminal and navigate to your Laravel project's directory
  • Run the following command to create the 'Task' model:
arduino
php artisan make:model Task -m
  • This command will create a new model named 'Task' and also create a migration file for the 'tasks' table in the database.

Step 2: Define the 'tasks' table schema

  • Open the newly created migration file located in the 'database/migrations' directory and define the 'tasks' table schema:
php
public function up() { Schema::create('tasks', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description')->nullable(); $table->timestamps(); }); }
  • This schema will create a 'tasks' table with an auto-incrementing 'id' column, a 'title' column of type string, a 'description' column of type text and nullable, and two timestamp columns named 'created_at' and 'updated_at'.

Step 3: Run the migration

  • Run the following command to run the migration:
php artisan migrate
  • This command will create the 'tasks' table in the database.

Step 4: Define the 'Task' model attributes

  • Open the 'Task' model located in the 'app' directory and define the attributes that correspond to the 'tasks' table columns:
php
namespace App; use Illuminate\Database\Eloquent\Model; class Task extends Model { protected $fillable = ['title', 'description']; }
  • This model defines two attributes, 'title' and 'description', that are fillable for mass assignment.

Step 5: Create the 'Task' controller

  • Run the following command to create a new controller named 'TaskController':
go
php artisan make:controller TaskController --resource
  • This command will create a new controller named 'TaskController' with all the necessary CRUD methods.

Step 6: Implement the 'Task' controller methods

  • Open the 'TaskController' located in the 'app/Http/Controllers' directory and implement the following methods:
php
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Task; class TaskController extends Controller { public function index() { $tasks = Task::all(); return view('tasks.index', compact('tasks')); } public function create() { return view('tasks.create'); } public function store(Request $request) { $task = new Task(); $task->title = $request->input('title'); $task->description = $request->input('description'); $task->save(); return redirect()->route('tasks.index'); } public function show(Task $task) { return view('tasks.show', compact('task')); } public function edit(Task $task) { return view('tasks.edit', compact('task')); } public function update(Request $request, Task $task) { $task->title = $request->input('title'); $task->description = $request->input('description'); $task->save(); return redirect()->route('tasks.index'); } public function destroy(Task $task) { $task->delete(); return redirect()->route('tasks.index'); } }
  • These methods correspond to the CRUD operations for the 'Task' model.
  • The 'index' method retrieves all the tasks from the database

In Laravel, the view files are usually located in the 'resources/views' directory. You can create a subdirectory inside the 'resources/views' directory with the same name as your controller to organize your views for that controller. For example, if your controller is named 'TaskController', you can create a 'tasks' subdirectory inside the 'resources/views' directory and store all the view files for the 'TaskController' in that directory.

So, for example, you can create the following view files for the 'TaskController':

  • 'index.blade.php' located in 'resources/views/tasks/'
  • 'create.blade.php' located in 'resources/views/tasks/'
  • 'edit.blade.php' located in 'resources/views/tasks/'
  • 'show.blade.php' located in 'resources/views/tasks/'

Note that you can name your view files anything you want, as long as they end with '.blade.php' and are located in the 'resources/views' directory or one of its subdirectories. Also, remember to update the view file names in the 'TaskController' methods where necessary.


example of index.blade.php


here is an example of an 'index.blade.php' view file for the 'TaskController':

html
@extends('layouts.app') @section('content') <h1>Tasks</h1> <a href="{{ route('tasks.create') }}" class="btn btn-primary mb-3">Create Task</a> <table class="table"> <thead> <tr> <th>Title</th> <th>Description</th> <th>Created At</th> <th>Actions</th> </tr> </thead> <tbody> @foreach ($tasks as $task) <tr> <td>{{ $task->title }}</td> <td>{{ $task->description }}</td> <td>{{ $task->created_at }}</td> <td> <a href="{{ route('tasks.show', $task) }}" class="btn btn-primary">Show</a> <a href="{{ route('tasks.edit', $task) }}" class="btn btn-secondary">Edit</a> <form action="{{ route('tasks.destroy', $task) }}" method="POST" class="d-inline-block"> @csrf @method('DELETE') <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this task?')">Delete</button> </form> </td> </tr> @endforeach </tbody> </table> @endsection

This view file displays a table of all the tasks with their title, description, and created_at date. It also includes buttons to show, edit, and delete each task, as well as a button to create a new task.


you can add the following route to your 'routes/web.php' file:

php
Route::resource('tasks', 'TaskController');

This route will create the necessary routes for the 'TaskController', including a route that points to the 'index' method.



here is an example of a 'create.blade.php' view file for the 'TaskController':

html
@extends('layouts.app') @section('content') <h1>Create Task</h1> <form action="{{ route('tasks.store') }}" method="POST"> @csrf <div class="form-group"> <label for="title">Title</label> <input type="text" name="title" id="title" class="form-control" required> </div> <div class="form-group"> <label for="description">Description</label> <textarea name="description" id="description" class="form-control" rows="5" required></textarea> </div> <button type="submit" class="btn btn-primary">Create</button> </form> @endsection


here is an example of a 'show.blade.php' view file for the 'TaskController':

html
@extends('layouts.app') @section('content') <h1>{{ $task->title }}</h1> <p>{{ $task->description }}</p> <p>Created At: {{ $task->created_at }}</p> <p>Updated At: {{ $task->updated_at }}</p> <a href="{{ route('tasks.edit', $task) }}" class="btn btn-secondary">Edit</a> <form action="{{ route('tasks.destroy', $task) }}" method="POST" class="d-inline-block"> @csrf @method('DELETE') <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this task?')">Delete</button> </form> @endsection

here is an example of an 'edit.blade.php' view file for the 'TaskController':

html
@extends('layouts.app') @section('content') <h1>Edit Task</h1> <form action="{{ route('tasks.update', $task) }}" method="POST"> @csrf @method('PUT') <div class="form-group"> <label for="title">Title</label> <input type="text" name="title" id="title" class="form-control" value="{{ $task->title }}" required> </div> <div class="form-group"> <label for="description">Description</label> <textarea name="description" id="description" class="form-control" rows="5" required>{{ $task->description }}</textarea> </div> <button type="submit" class="btn btn-primary">Update</button> </form> @endsection

This view file displays a form with fields for editing a task's title and description. It also includes an 'Update' button to submit the form.

email mailto: pretext

 <a href="mailto:designoutsourced.com+info@gmail.com?subject=Maklumat%20lanjut%20pakej&body=Hai,%20saya%20berminat%20tahu%20lebi...