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:
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><ahref="{{ route('tasks.create') }}"class="btn btn-primary mb-3">Create Task</a><tableclass="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><ahref="{{ route('tasks.show', $task) }}"class="btn btn-primary">Show</a><ahref="{{ route('tasks.edit', $task) }}"class="btn btn-secondary">Edit</a><formaction="{{ route('tasks.destroy', $task) }}"method="POST"class="d-inline-block">
@csrf
@method('DELETE')
<buttontype="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':