Using UUID in PHP framework laravel for data table operation

Source: Internet
Author: User
Tags php framework
This article mainly introduces the use of UUID in PHP framework laravel data table operation, combined with the case of the form of a more detailed analysis of the Laravel framework based on UUID data table related procedures, implementation techniques and operational considerations, the need for friends can refer to the next

The examples in this paper describe the use of UUID in PHP framework laravel for data table operation. Share to everyone for your reference, as follows:

Uuid

A UUID is a number generated on a machine that guarantees that all machines in the same time and space are unique.

Simply put, it is a unique identifying information generated by a rule (e.g. Business identification number + month-date + day-to-date self-increment number format). Used to correlate some of our amount of data and information.

Instance

I used this thing in a project before, and now I'm writing a simple demo with the Laravel framework.

Front-end form forms

<!      DOCTYPE html>

Establish database and data tables (only 1 index tables and 8 sub-tables for storage details)

Principle: Through the unique characteristics of the UUID, the detail data of one data exists in other tables, which is randomly assigned by the UUID, only the UUID and key fields are stored in the index table.

Table prefix uniform prefix-------mall_ table:    mall_index_user       Index Table    -------uuid,uname table 0:    mall_user_0           Uuid,uname,sex, Age Table 1:    mall_user_1 table 2:    mall_user_2 table 3:    mall_user_3 table 4:    mall_user_4 table 5:    mall_user_5 table 6:    Mall_user_6 Table 7:    mall_user_7

Insert data into a database by Routing and Controller for form form submission data

Routing:

Form form page Routing route::get (' Mysql ', function () {  return view (' Home/mysql ');}); /Data Insert Submit Routing route::p ost ('/addmysql ', ' findmorecontroller@addmysql ');

Controller:

Insert data to database Public function Addmysql (Request $request) {  $uuid =md5 (Uniqid (Mt_rand (), true));  $uid =hexdec (substr ($uuid, 0,1))% 8;  $sex = $request->input (' sex ');  $age = $request->input (' age ');  DD ($UUID);  $uname = $request->input (' uname ');  $result = db::table (' Index_user ')->insert ([' uuid ' = ' + $uuid, ' uname ' + $uname]);  $result 1 = db::table (' user_ '. $uid)->insert ([' uuid ' = + $uuid, ' uname ' = = $uname, ' sex ' + $sex, ' age ' = $age ]);  if ($result 1) {    return ' 1 ';  } else{    return ' 0 ';  }}

Solution: The $uid above is a representation of the table to which the detail table will be inserted through the UUID

For example: $uid = 3 Then insert the details into the user_3

After the insert succeeds, the query is made by querying the UUID through uname, and the UUID knows where the details are stored in the Zhang Zihe table. Then query

Routing:

Query page route::get (' Findmysql ', function () {  return view (' Home/findmysql ');}); /Query Routing route::p ost ('/findmysql ', ' findmorecontroller@findmysql ');

Controller:

Query public Function findmysql (Request $request) {    //dd ($request);    $uname = $request->input (' uname ');    $uuid =db::table (' Index_user ')->where (' uname ', ' = ', $uname)->value (' uuid ');    $uid =hexdec (substr ($uuid, 0,1))% 8;    $userInfos =db::table (' User_ '. $uid)->get ();    if ($userInfos) {      return view (' Home/selectmysql ', [' userinfos ' = ' = $userInfos]);    } else{      return view (' Home/findmysql ');}    }

Front End Display

<! DOCTYPE html>

At this point, a simple example of using the UUID table to process data is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.