Example analysis of one-to-one relationship implementation and application method of laravel 5.1 framework model

Time:2021-10-14

This paper describes the implementation and use of one-to-one relationship of larave5.1 framework model. Share with you for your reference, as follows:

This article mainly records the one-to-one relationship of the model. Association relationship is a very convenient function of the model.

1. Realize one-to-one relationship

1.1 preparation

First, we need to create two tables and two corresponding models. The first model is the user table and the second model is the account table.

Our logic here is: a user can only have one account under the information, and an account can only be owned by one user. This is a one-to-one relationship.

1.1.1 user information table

Generate model and migration files:


php artisan make:model UserInfo -m

Write migration file (table specification):

public function up()
  {
    Schema::create('user_infos', function (Blueprint $table) {
      $table->increments('id');
      $table->string('name');
      $table->string('phone_number');     //  cell-phone number
      $table->timestamps();
    });
  }

Write model factory:


// user_info
$factory->define(App\UserInfo::class, function (Faker\Generator $faker) {
  return [
    'name' => $faker->name,
    'phone_number' => $faker->phoneNumber,
  ];
});

Using seeder:


php artisan make:seeder UserInfoSeeder


  public function run()
  {
    factory(\App\UserInfo::class, 10)->create();
  }
  public function run()
  {
    Model::unguard();
    $this->call('UserInfoSeeder');
    Model::reguard();
  }

Generate data and fill in the data with seeder:


php artisan migrate:seeder 

1.1.2 account number table

Write migration file (table specification):

public function up()
  {
    Schema::create('accounts', function (Blueprint $table) {
      $table->increments('id');
      $table->string('user_name');    //  user name
      $table->integer('user_info_id');  //  Association key
      $table->timestamps();
    });
  }

2. Preparation relationship

The first is the account table. Write the following code under the model:


  public function user()
  {
    return $this->belongsTo(UserInfo::class, 'user_info_id', 'id');
  }

Code interpretation: the second and third parameters of belongsto can be omitted. If the second and third parameters are omitted, laravel will automatically fill in according to the method name. The second parameter will be filled with “user_id” and the third parameter will be filled with “Id”, but we must write this now, because the foreign key I customize is user_ info_ ID if laravel is automatically populated, the foreign key cannot be found, so we need to add it manually.

The second parameter of belongsto is account. This model uses’ user ‘_ info_ ID ‘foreign key to associate userinfo model.

The third parameter of belongsto is account. This model is associated with the primary key name of userinfo.

Then there is the user information table:


  public function account()
  {
    return $this->hasOne(Account::class);
  }

Code interpretation: the second and third parameters of hasone can also be omitted. If the second and third parameters are omitted, laravel will automatically fill in the second parameter with “user_info_id” and the third parameter with “Id”.

use:


//  $account = App\Account::find(1);
//  dd($account->user);
  $userInfo = App\UserInfo::find(1);
  dd($userInfo->account);

For more information about laravel, readers who are interested can see the special topics on this site: introduction and advanced tutorial of laravel framework, summary of PHP excellent development framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation, and summary of common database operation skills for PHP

I hope this article will be helpful to your PHP Programming Based on laravel framework.