Home  >  Article  >  PHP Framework  >  How to add data through Laravel framework

How to add data through Laravel framework

PHPz
PHPzOriginal
2023-04-14 18:38:592171browse

Laravel is a web development framework based on the PHP language. It provides developers with a wealth of tools and resources to help them quickly build powerful web applications. How to add data in Laravel framework? This article will introduce in detail how to add data through the Laravel framework.

Step 1: Create a database

Before using the Laravel framework to add data, you first need to create a database. You can use a relational database management system such as MySQL, MariaDB or SQLite, choose one and create a database. This article uses MySQL as an example to demonstrate how to create a database named "testdb".

Open the command line tool or MySQL client and enter the following command:

CREATE DATABASE testdb;

At this point, a database named testdb has been successfully created, and the database can be called in the Laravel framework.

Step 2: Create a model

In the Laravel framework, ORM (Object Relational Mapping) provides a way to interact with the database. Operations such as addition, deletion, modification, and query of data can be implemented through ORM. Before doing this, you need to create a model to interact with the tables in the database.

Enter the following command on the command line:

php artisan make:model Test

The above command means to create a model file named Test, which will be stored in the app directory.

Next, open the Test.php file and add data as follows:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
    protected $fillable = [&#39;name&#39;, &#39;age&#39;, &#39;sex&#39;];
}

In the above code, the $fillable attribute specifies fields that can be batch assigned in the model. In this example, the fields that can be added and assigned include: name, age, and gender.

Step 3: Create a controller

In the Laravel framework, the controller (Controller) is responsible for processing each HTTP request and returning the corresponding response. Before doing this, you need to create a controller to add data.

Enter the following command on the command line:

php artisan make:controller TestController

The above command means to create a controller file named TestController, which will be stored in the app/Http/Controllers directory.

Next, open the TestController.php file and add data in the following way:

<?php

namespace App\Http\Controllers;

use App\Test;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function addData(Request $request)
    {
        $data = [
            &#39;name&#39; => $request->name,
            'age' => $request->age,
            'sex' => $request->sex,
        ];

        Test::create($data);

        return redirect('/')->with('success', 'Data Added Successfully!');
    }
}

In the above code, the addData method receives a request object named $request, which contains The data to add. Then, assign the data to the variable $data and use Test::create($data) to add the data to the test table.

Step 4: Create a route

In the Laravel framework, routing (Route) is responsible for mapping HTTP requests to the corresponding controller methods. Before doing this, you need to create a routing rule to add data.

In the routes/web.php file, use the following code to create a route:

Route::post('/add', 'TestController@addData');

The above code means to create a post request with the URL "/add", which will be used by TestController addData method processing.

Step 5: Create a view

In the Laravel framework, the view (View) is responsible for displaying data and receiving user input. Before doing this, you need to create a view file to add data.

Create a view file named add.blade.php in the resources/views directory, which contains the following code:

<!DOCTYPE html>
<html>
<head>
    <title>Add Data</title>
</head>
<body>
    <h2>Add Data</h2>

    <form method="post" action="/add">
        {{ csrf_field() }}

        <label>Name:</label><br>
        <input type="text" name="name"><br>

        <label>Age:</label><br>
        <input type="text" name="age"><br>

        <label>Sex:</label><br>
        <select name="sex">
            <option value="male">Male</option>
            <option value="female">Female</option>
        </select><br>

        <button type="submit">Add Data</button>
    </form>

</body>
</html>

The above code means to create a view file named "Add Data" The form includes three input boxes: name, age and gender. The submit button of the form will submit the form data to the "/add" route.

Step Six: Test the Application

Now, you can test whether the Laravel application can successfully add data. The test can be completed by following the steps:

  1. Start the Laravel development server

Enter the following command on the command line:

php artisan serve

This command will start the Laravel development The server will output a URL address in the terminal, which can be accessed in the browser.

  1. Access the Add Data View

Enter the following URL address in the browser:

http://localhost:8000/add

to access the form named "Add Data".

  1. Add data

Enter the data to be added in the form and click the "Add Data" button to submit the form data. After successfully adding data, you should be redirected to a new page with the "Data Added Successfully!" prompt message.

Summary

Through the Laravel framework, data can be added quickly and easily. Through the above steps, you can create a database named "testdb" and create a table named "test" in it; create a model file named "Test", which contains three fields that allow batch assignment; create a A controller file named "TestController" and a view file named "add.blade.php" for adding data. Finally, the application can be tested in a browser to ensure that the data was successfully added to the database.

The above is the detailed content of How to add data through Laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn