How To Build A Simple CRUD App With Laravel

How To Build A Simple CRUD App With Laravel

How To Build A Simple CRUD App With Laravel

Programming Assignment Help

Laravel is a popular PHP web framework that makes it easy to build web applications. It offers a variety of features that make development faster and easier, including routing, middleware, Eloquent ORM, Blade templating engine, and more.

In this article, we’ll walk through the process of building a simple CRUD (Create, Read, Update, Delete) app with Laravel. We’ll start with the basics of setting up a new Laravel project and then move on to creating the database and building the CRUD functionality.

Prerequisites Before we start, make sure you have the following software installed on your computer:

PHP 7.4 or later

Composer

MySQL or any other database management system

Getting Started To create a new Laravel project, open your terminal and navigate to the directory where you want to create the project. Then, run the following command:

lua
composer create-project --prefer-dist laravel/laravel crud-app

This will create a new Laravel project called crud-app in the current directory.

Next, navigate into the project directory and start the development server by running the following command:

php artisan serve

This will start the development server and your Laravel app will be accessible at http://localhost:8000.

Creating the Database Next, let’s create a new database for our app. Open your MySQL client and run the following commands:

sql
CREATE DATABASE crud_app;

This will create a new database called crud_app. Next, let’s create a new table in the database called users. Run the following command:

sql
CREATE TABLE users ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );

This will create a new table called users with the following columns:

id: the primary key of the table

name: the name of the user

email: the email address of the user

password: the password of the user

created_at: the timestamp when the user was created

updated_at: the timestamp when the user was last updated

 

Building the CRUD Functionality

Now that we have the database set up, let’s build the CRUD functionality for our app.

First, let’s create a new controller for the users. Run the following command:

go
php artisan make:controller UserController --resource

This will create a new controller called UserController with all the necessary methods for CRUD operations.

Next, let’s define the routes for our app. Open the routes/web.php file and add the following code:

css
Route::resource('users', 'UserController');

This will define the routes for our UserController.

Now, let’s create the views for our app. Create a new folder called users inside the resources/views folder. Inside this folder, create four new Blade templates:

index.blade.php: the view for displaying a list of all users

create.blade.php: the view for creating a new user

edit.blade.php: the view for editing an existing user

show.blade.php: the view for displaying the details of a user

In each of these templates, add the necessary HTML and form elements for displaying and editing the user data.

Finally, let’s implement the CRUD functionality in our UserController. Open the app/Http/Controllers/UserController.php file and add the following code:

php
public function index() { $users = DB::table('users')->get(); return view('users.index', compact('users')); } public function create() { return view('users.create'); } public function store(Request
 

Create a Migration

A migration is a database schema created to manage the database tables and their columns. Laravel provides an easy way to create and manage migrations.

To create a migration, run the following command:

go
php artisan make:migration create_tasks_table --create=tasks

This command will create a migration file named create_tasks_table in the database/migrations directory. The --create option specifies the name of the table to be created.

Open the create_tasks_table file and add the following code:

php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTasksTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('tasks', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description'); $table->boolean('completed')->default(false); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('tasks'); } }

In the up method, we are creating the tasks table with four columns: id, title, description, and completed. The id column is an auto-incrementing primary key. The timestamps method creates two columns: created_at and updated_at.

In the down method, we are dropping the tasks table if it exists.

Run Migrations

To run the migration and create the tasks table in the database, run the following command:

php artisan migrate

You should see a message like the following:

makefile
Migrating: 2019_01_01_000000_create_tasks_table Migrated: 2019_01_01_000000_create_tasks_table

This means that the migration was successful and the tasks table was created in the database.

Create a Task Model

Now that we have created the tasks table in the database, we need to create a corresponding model to interact with the table.

To create a model, run the following command:

arduino
php artisan make:model Task

This command will create a model file named Task.php in the app/Models directory.

Open the Task.php file and add the following code:

php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Task extends Model{ use HasFactory; protected $fillable = ['title', 'description', 'completed']; }

We have defined the Task model class and specified the table name tasks in the $table property. We have also defined the fillable attributes: title, description, and completed.

Create Routes

We need to create routes to handle HTTP requests to our application. Open the web.php file located in the routes directory and add the following code:

php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\TaskController; Route::get('/', [TaskController::class, 'index'])->name('tasks.index'); Route::get('/tasks/create', [TaskController::class, 'create'])->name('tasks.create'); Route::post('/tasks', [TaskController::class, 'store'])->name('tasks.store'); Route::get('/tasks/{task}', [TaskController::class, 'show'])->name
 

Conclusion

In conclusion, Laravel provides a great framework for building robust CRUD applications quickly and easily. It offers a lot of useful features and tools that simplify the development process and allow developers to focus on creating great applications.

By following the steps outlined in this article, you should now have a good understanding of how to build a simple CRUD app with Laravel. You should also be familiar with some of the key concepts and features of Laravel, such as routing, controllers, models, and views.

Of course, there is still a lot more to learn about Laravel and web development in general. However, by building simple CRUD apps like this one and gradually adding more features and complexity, you can gradually build your skills and knowledge.

If you have any questions or comments about this article or about Laravel in general, please feel free to leave a comment below. Good luck with your Laravel development projects!

 
 
 
No Comments

Post A Comment

This will close in 20 seconds