Example analysis of one to many relationship in the far layer of laravel 5.1 framework model

Time:2021-10-11

This paper illustrates the far layer one to many relationship of larave5.1 framework model. Share with you for your reference, as follows:

Far layer one to many, we can fully understand it through an example:

Every article must have and only one publisher. Publishers can have multiple articles, which is a one to many relationship. A publisher can come from one country, but a country can have multiple publishers, which is a one to many relationship. Then there is a far-reaching one to many relationship, which is the “relationship between country and article”. The country table can be associated to the article table through the publisher table.

1. Realize the far layer one to many relationship

1.1 article table structure


  public function up()
  {
    Schema::create('articles', function (Blueprint $table) {
      $table->increments('id');
      $table->string('title');
      $table->text('body');
      $table->integer('user_id');
      $table->timestamps();
    });
  }

1.2 add a column to the users table


  public function up()
  {
    Schema::table('users', function (Blueprint $table) {
      $table->integer('country_id');
    });
  }
  public function down()
  {
    Schema::table('users', function (Blueprint $table) {
      $table->dropColumn('country_id');
    });
  }

1.3 national table structure


  public function up()
  {
    Schema::create('countries', function (Blueprint $table) {
      $table->increments('id');
      $table->string('name');
      $table->timestamps();
    });
  }

1.4 write one to many relationships

First, the relationship between country and user:

Country model:


  public function users()
  {
    return $this->hasMany(User::class);
  }

User model:


  public function country()
  {
    return $this->belongsTo(Country::class);
  }

Then there is the relationship between user and article:

User model:


  public function articles()
  {
    return $this->hasMany(Article::class);
  }

Article model:


  public function user()
  {
    return $this->belongsTo(User::class);
  }


1.5 accessing remote one to many relationships

This is the main content of today. The article can be found in the far layer in the country:

public function articles()
  {
    /**
     *It is recommended that the first and second parameters be written in full, and the third and fourth parameters can be omitted to use the default (if the default is OK).
     */
    return $this->hasManyThrough(Article::class, User::class, 'country_id', 'user_id');
  }

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.