What is CRUD? (Create the database, read, update and delete) Utilizing Laravel - (r)

Aug 9, 2023

[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">

CRUDPosts   Add Post      @foreach ($posts as $post)

$post->title

$post->body      Edit    @csrf @method('DELETE') Delete       @endforeach

This code generates an easy HTML webpage that utilizes Bootstrap for styling. The page is designed with the navigation bar, as well as grid templates that list all the posts in the database along with their details. There are two action buttons -- edit and delete -- using it with the @foreach Blade helper.

Edit button button to edit button will take the user to an editing post page, from where they can edit the post. The Delete button deletes the post from the database using route('posts.destroy', $post->id) with a DELETE method.

Note Note: The code for the navigation bar for all the files is identical to the first file.

  1. Create the create.blade.php page. The Blade file known as create adds posts to the database. The following commands can be used to generate the file:
php artisan make:view posts.create

This generates a create.blade.php file inside the /resources/views/posts folder.

  1. Add the following code to your create.blade.php file:
The same code is used as in similar to the first file. The following should be added in the middle of the nav tag and before the closing body tag.  
 
 Add the following after the nav tag and before the body tag.  Include an Post
Post 
 @csrf
 Title title Title
 
 Body
  Body Body
Title
•Create Post
 Add Post
Create Post
 Create Post
 

The code above creates an application form, with the title and body fields as well as a submit button for adding posts into the database using the route('posts.store') } operation using the POST method.

  1. Create the Edit post page that allows you to modify posts in the database. Utilize the following command to generate the document:
php artisan make:view posts.edit

This creates an edit.blade.php file inside the /resources/views/posts folder.

  1. Add the following code to edit.blade.php. edit.blade.php file:
 Update Post ID) 
  Update Post
 " method="post">
 @csrf
method="post"> @csrf@method('PUT')
 
 Title
 
  Required>HTML1 Body
 $post->body  ||}
 Post HTML11


  

The code above creates a form with title and body fields and a submit button for editing a post with the specified id in the database through the  route('posts.update')  action with a PUT method.

  1. Restart your application server by using the code below:
php artisan serve

Visit http://127.0.0.1:8000 on your browser to view your new blog. Click Add Post to add new posts. Add Post button to make new blog posts.

Install and test your CRUD Application

Prepare your application for deployment as follows.

  1. The deployment process should be easy and simple by declaring the folder as public. Create an .htaccess file to the root of the application folder by using the following codes:

 RewriteEngine On
 RewriteRule ^(. *)$ public/$1 [L]
 
  1. Make your application use HTTPS by putting the following code to your web.php routes in the routes/web.php file:
use Illuminate\Support\Facades\URL;
 URL::forceScheme('https');
  1. Upload your code to a Git repository. supports deployments from GitHub, GitLab, or Bitbucket.

Create a project on My

  1. Log into your account, then click to click the Add Service option on the Dashboard. This will allow you to add a brand new application.
  1. If you're new to the app, connect directly to either the account you have on GitHub, GitLab, or Bitbucket account, and grant certain permissions.
  1. Please fill out the application form and include the app_key. You can find its corresponding value in your .env file.
  1. Select your build resources and decide if you wish to build your app using the route or build the application by using Dockerfile. This is a demonstration of how to let My build the app based on your repository.
  1. Specify the different processes you want to run when you deploy. The process can be left blank for now.
  1. Finally, you must add the payment method.

After confirming your payment method, My launches your app and gives you a unique URL, which is shown in the following:

Successful deployment using My
Successful deployment.

You can visit the link, but you get a 500 Server Error page due to the fact that the app requires a database connection to run. The following section resolves the issue.

Create a Database using My

  1. For creating a database go to your My dashboard and click "Add Service".
  1. Choose your database and fill in the form by entering your preferred database's name and username. Type, click, and password. Include a location for your data center and a database size suitable for your needs.
  1. The next page displays the price summary as well as your payment method. Click Create Database to finish the process.
  1. When you have created the database, My redirects you to the list of services. Click the database you made and then go into External Connections. The credentials for the database must be copied.
  1. Reload the application's deployment page and click Settings. Then, scroll down to Environment Variables and then click "Add Environment Variables". Add the database credentials as environmental variables in the following order:
DB_CONNECTION=mysql
DB_HOST=External hostname
DB_PORT=External port Database name
DB_USERNAME=Username
 DB_PASSWORD=Password

The list of variables for the application will now appear as follows:

The .env variables list.
The .env variable list.
  1. Go to your application's Deployments page and manually deploy your application using "Deploy Now"to apply these changes. So far, you've created a database and connected the database to your app.
  1. To create your database tables within the My database, first create tables in your My database, connect the database to your local app by updating the.env file. .env file with identical credentials as you have entered within your application at My and execute the following command:
php artisan migrate

This command runs all the file migrations. It will create all the tables in your My application.

You can now test your application with the URL you were assigned following the initial launch.

Summary

Laravel is a comprehensive framework that allows you to build powerful and flexible web applications that demand CRUD functionality. With its intuitive syntax and powerful capabilities, Laravel makes it easy to build CRUD operations within your app.

The article outlined the basic notions of CRUD operations as well as how to implement them using the built-in features of Laravel. The article also explained:

  • How do you create an account inside My and link it with your application
  • How to use Laravel's migrations to define the database table Create the controller file, and perform its functions
  • Define a model and link it with the controller. The routing engine in Laravel generates Blade files to create templates and webpages and to deploy and test applications using My

Marcia Ramos

I'm the editor's team lead at . I'm an open source lover and am a huge fan of coding. Over the past seven years of technical writing and editing in the technology industry I am a fan of working alongside individuals to produce short and precise articles and enhance the workflows.