Example of data table operation using UUID in PHP framework Laravel

  • 2021-10-13 06:54:04
  • OfStack

In this paper, an example of PHP framework Laravel using UUID to achieve data table operation. Share it for your reference, as follows:

UUID

UUID refers to a number generated on one machine, which is guaranteed to be unique to all machines in the same space-time.

To put it simply, it is a unique identification information generated by a rule (such as business identification number + year, month and day + self-increasing number formatting on the same day). Used to correlate our 1 amount of data and information.

Instances

I used this thing when I was doing a project before, and now I will write a simple demo with Laravel framework

Front-end form form


<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <form action="/addMysql" method="post">
      {!!csrf_field()!!}
      <table>
        <tr>
          <th style="colspan:2"> Registration </th>
        </tr>
        <tr>
          <td> Account number </td>
          <td>
            <input type="text" name="uname" value="" />
          </td>
        </tr>
        <tr>
          <td> Password </td>
          <td>
            <input type="password" name="pwd" value="" />
          </td>
        </tr>
        <tr>
          <td> Gender </td>
          <td>
            <input type="radio" name="sex" value="1" />: Male 
            <input type="radio" name="sex" value="0" />: Female 
          </td>
        </tr>
        <tr>
          <td> Age </td>
          <td>
            <input type="text" name="age" value="" />
          </td>
        </tr>
        <tr>
          <td style="colspan:2">
            <input type="submit" value=" Submit " />
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

Establish database and data tables (only 1 index table and 8 sub-tables for storing detailed information)

Principle: Through UUID's only 1 characteristic, the detailed information data of 1 data is stored in other tables, which means that it is randomly allocated through UUID, and only UUID and key fields are stored in the index table

Table Prefix 1 Prefix----mall_
Tables: mall_index_user Index Tables-----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

Submit data to the database by form form through routing and controller

Routing:


//form Form page routing 
Route::get('Mysql',function(){
  return view('home/Mysql');
});
// Data insertion commit route 
Route::post('/addMysql','findMoreController@addMysql');

Controller:


// Insert data into the 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]);
  $result1 = DB::table('user_'.$uid)->insert(['uuid'=>$uuid,'uname'=>$uname,'sex'=>$sex,'age'=>$age]);
  if($result1){
    return '1';
  }else{
    return '0';
  }
}

Solution: The above $uid is the representative number of the table to insert into which 1 detail table is obtained by UUID

For example: $uid=3 then insert details into user_3

Insert successfully after the query, first through uname query UUID, through UUID know the details stored in which sub-table

Routing:


// Query page 
Route::get('findMysql',function(){
  return view('home/findMysql');
});
// Query route 
Route::post('/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>
<html>
  <head></head>
  <body>
    <form action="" method="post">
      <table>
        <tr>
          <th style="colspan:2"> Traversal </th>
        </tr>
        @foreach($userInfos as $userInfo)
        <tr>
          <td> Gender </td>
          <td>
            <input type="text" name="" value="{{$userInfo->sex}}" />
          </td>
        </tr>
        <tr>
          <td> Age </td>
          <td>
            <input type="text" name="" value="{{$userInfo->age}}" />
          </td>
        </tr>
        @endforeach
      </table>
    </form>
  </body>
</html>

At this point, a simple example of using UUID sub-table to process data is completed.

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: