Detailed introduction of laravel data migration and Eloquent ORM (code example)

01-28-2024

This article brings you a detailed introduction of laravel data migration and Eloquent ORM (code example), which has certain reference value. Friends who need it can refer to it and hope to help you. Database can be said to be the most commonly used and important part of back-end development. Laravel provides a very practical Eloquent ORM model class to interact with the database simply and intuitively. At the same time, the data migration management database can be shared and edited with the team. For more information about the two, please see the documents below. The following uses both as examples, and the requirement is to record the user's browsing records. Please do not bring this example into the actual project, this article is only an example. The actual project is recorded according to the requirements, and the storage method is selected.

Create data table

The first step, of course, is to create a data table. Using artisan command can easily create models and data migration. Php artisan make: model models/browse log -m, and the-m parameter creates a data migration file while creating a model. After executing the above command, two files, app/Models/BrowseLog.php and database/migrations/{now _ date} _ create _ browse _ logs _ table.php, have been added. Next, edit {now _ date} _ create _ browse _ logs _ table.php to create a data table.

/** * Run the migrations. * * @return void */ public function up() { Schema::create('browse_logs', function (Blueprint $table) { $table->increments('id'); $ table-> IP address ('IP _ addr')-> comment ('IP address'); $ table-> string ('request _ url', 20)-> comment ('request URL'); $ table-> char ('city _ name', 10)-> comment ('get city name according to ip'); $table->timestamps(); }); DB:: statement ("alter table`browse _ logs`comment' browse record table'"); //Table Notes }

The code is as above. After editing, executing the command php artisan migrate will create all data tables that have not been migrated. as follows

Personally, laravel's default data type is debatable. For example, ipAddress (), whose data format is varchar(45), can actually be converted into int by using ip2long for storage. Timestamps () can also be stored with timestamps. Of course, laravel also provides accessors & modifiers for easy maintenance. You can choose from the actual projects.

Definition middleware

Define a global middleware, and every request will be executed. Execute PHP artisan make: middleware browse log, and create the app/http/middleware/browse log.php file. Add the created middleware to app/Http/Kernel.php, as follows.

recorded data

Finally, in the middleware, just record the data into the database, and the code is as follows.

/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $log = new \App\Models\BrowseLog(); $log->ip_addr = $request->getClientIp(); $log->request_url = $request->path(); $log->city_name = get_city_by_ip(); $log->save(); return $next($request); }

After visiting a few links, go to the database and have a look.

Data writing is normal, and this example ends here.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us