Learn from me the quick start of Laravel

  • 2021-07-21 07:54:53
  • OfStack

Installation

The Laravel framework uses Composer for installation and dependency management. If you haven't installed it yet, start installing Composer now.

After installing Composer, you can install Laravel from the command line using the following command:

composer create-project laravel/laravel your-project-name
Alternatively, you can download it from Github repository. Next, after installing Composer, execute the composer install command under the project root. This command will download and install the dependent components of the framework.

Write permission

After installing Laravel, you also need to set write permissions to the app/storage directory for the web server. Please refer to the Installation 1 section for more information on configuration.

Directory structure

After installing the framework, you need to be familiar with the directory structure of the project under 1. The app folder contains directories such as views, controllers, and models. Most of the code in the program will be stored in these directories. You can also view 1 configuration items in the app/config folder under 1.

Route

We started to create our first route. In Laravel, the simple routing method is closure. Open the app/routes. php file and add the following code:

Route::get('users', function()
{
return 'Users!';
});
Now, if you type/users in the web browser, you should see Users! Output. Awesome! Your first route has been created.

Routing can also be assigned to controller classes. For example:

Route::get('users', 'UserController@getIndex');
This route tells the frames/users that the route request should invoke the getIndex method of the UserController class. To see more information about routing controllers, view the controller documentation.

Create a view

Next, we will create a view to display our user data. Views are stored in the app/views folder as HTML code. We will put two view files into this folder: layout. blade. php and users. blade. php. First, let's create the layout. blade. php file:


<html>
    <body>
        <h1>Laravel Quickstart</h1>         @yield('content')
    </body>
</html>

Next, we create the users. blade. php view:


@extends('layout') @section('content')
    Users!
@stop

The grammar here may be strange to you. Because we are using the Laravel template system: Blade. Blade is very fast because only a few regular expressions are used to compile your template into the original PHP code. Blade provides powerful features, such as template inheritance, as well as some common PHP control structure syntax sugars, such as if and for. See the Blade documentation for more.

Now that we have our view, let's go back to the/users route. Instead of returning Users, we use views! :


Route::get('users', function()
{
    return View::make('users');
});

Beautiful! Now you have successfully created the view inherited from layout. Next, let's start with the database layer.

Create a migration

To create a table to hold our data, we will use Laravel to migrate the system. Migration describes changes to the database, which makes it very simple to share with their team members.

First, we configure the database connection. You can configure all database connection information in the app/config/database. php file. By default, Laravel is configured to use SQLite, and one SQLite database is stored in the app/database directory. You can change the driver option of the database configuration file to mysql and configure the mysql connection information.

Next, to create the migration, we will use Artisan CLI. In the project root, execute the following command in the terminal:


php artisan migrate:make create_users_table

Then, locate the generated migration file app/database/migrations directory. This file contains a class with two methods: up and down. In the up method, you name a database table modification; In the down method, you simply remove it.

Let's define the following migration:


public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('name');
        $table->timestamps();
    });
} public function down()
{
    Schema::drop('users');
}

Then, we run the migrate command using the terminal in the project root directory to perform the migration:


php artisan migrate

If you want to roll back the migration, you can execute the command migrate: rollback. Now that we have the database table, let's add 1 some data!

Eloquent ORM

The Laravel offers a great ORM: Eloquent. If you have used the Ruby on Rails framework, you will find that Eloquent is very similar because it follows the ActiveRecord ORM style of database interaction.

First, let's define a model. The ELoquent model can be used to query related data tables, as well as a row within the table. Don't worry, we'll talk soon! Models are usually stored in the app/models directory. Let's define an User. php model in this directory, such as:


class User extends Eloquent {}

Note that we didn't tell Eloquent which table to use. Eloquent has several conventions, one is a database table that uses the plural form of the model as the model. Very convenient!

Using your favorite database management tool, insert a few rows of data into the users table. We will use Eloquent to get them and pass them to the view.

Now we modify our/users route as follows:


Route::get('users', function()
{
    $users = User::all();     return View::make('users')->with('users', $users);
});

Let's look at the route. First, the all method of the User model will fetch all records from the users table. Next, we pass these records to the view through the with method. The with method accepts 1 key and 1 value, which is available for use in the view.

Excited. Now we are ready to display the user in our view!

Display data

Now that we have access to the users classes in our view, we can show them as follows:


@extends('layout') @section('content')
    @foreach($users as $user)
        <p>{{ $user->name }}</p>
    @endforeach
@stop

You can find that the echo statement was not found. When using Blade, you can use two curly braces to output data. Quite simply, you should now be able to view the user name as a response output via the/users route.

This is just the beginning. In this series of tutorials, you've learned the basics of Laravel, but there are more exciting things to learn. Continue to read the document and dig into the powerful features of Eloquent and Blade. Or you are interested in queues and unit tests. Maybe you want to know IoC Container, the choice is yours!


Related articles: