This article brings you a detailed introduction (code example) about laravel data migration and Eloquent ORM. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The 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, data migration is used to manage the database, which can be shared and edited with the team. For more information on both, please see the documentation below.
The following uses both as an example. The requirement is to record user browsing records. Please do not bring this example into actual projects, this article is only an example. The actual project is recorded according to the requirements, and the storage method is selected.
Create a data table
The first step is of course to create a data table. Using the artisan command can easily create models and migrate data. php artisan make:model Models/BrowseLog -m, the -m parameter also creates a data migration file when creating the model. After executing the above command, two new files, app/Models/BrowseLog.php and database/migrations/{now_date}_create_browse_logs_table.php, were added.
Next edit {now_date}_create_browse_logs_table.php to create the data table
/** * Run the migrations. * * @return void */ public function up() { Schema::create('browse_logs', function (Blueprint $table) { $table->increments('id'); $table->ipAddress('ip_addr')->comment('ip 地址'); $table->string('request_url', 20)->comment('请求 url'); $table->char('city_name', 10)->comment('根据 ip 获取城市名称'); $table->timestamps(); }); DB::statement("ALTER TABLE `browse_logs` comment'浏览记录表'"); // 表注释 }
The code is as above. After the editing is completed, execute the command php artisan migrate to create all data tables that have not been migrated. As follows
Personally, laravel’s default data type is questionable. For example, the data format of ipAddress() is varchar(45). In fact, you can use ip2long to convert it to int for storage. timestamps() can also use timestamps for storage. Of course, laravel also provides accessors & modifiers for easy maintenance. You can choose by yourself in the actual project.
Define middleware
Define a global middleware that will be executed on every request. Execute php artisan make:middleware BrowseLog to create the app/Http/Middleware/BrowseLog.php file.Add the created middleware to app/Http/Kernel.php as follows
Record data
Finally, in the middleware, just record the data to the database. 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); }
Data writing is normal, this example ends here.
The above is the detailed content of Detailed introduction to laravel data migration and Eloquent ORM (code example). For more information, please follow other related articles on the PHP Chinese website!