PHP's db class library Eloquent uses the series alone (10) - remote one-to-many

My Eloquent series of articles using
php's db class library Eloquent alone series (1)
php's db class library Eloquent alone series (2) - paging
php's db class library Eloquent alone series (3) - sql log
php db class library Eloquent used alone series (4) - event monitoring
php's db class library Eloquent used alone series (5) - model to array
php's db class library Eloquent used alone series (6) - one-to-one association with
php's db class Library Eloquent used alone series (7) - one-to-many association
php's db class library Eloquent used alone series (8) - many-to-many association
php's db class library Eloquent used alone series (9) - many-to-many association - table association
The db class library Eloquent of its own php uses the series (10) - many-to-many association - remote one-to-many
php db class library Eloquent uses the series (11) - many-to-many association - adding model attributes
php's db class library Eloquent Single use series (12) - result set model to array - 2



The purpose of this series of articles is to use Eloquent out of the laravel environment, because it is easy to use .
All codes in this series of articles have passed the test. Eloquent Version: 5.4.27

The purpose of this article is to use remote one-to-many.
First build a table, there are multiple users in a country, and each user has multiple articles. Need to build 3 tables to meet the demand.
It is required to be able to find out which articles have been published in a country, which can be found through the user table.
create table countries (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(50) NOT NULL DEFAULT '' COMMENT 'Country name',
  PRIMARY KEY (id)
);


CREATE TABLE users (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(50) NOT NULL DEFAULT '' COMMENT 'User name',
  country_id int(11) NOT NULL DEFAULT '0' COMMENT '国家id',
  PRIMARY KEY (id)
);

create table posts (
  id int(11) NOT NULL AUTO_INCREMENT,
  user_id int  NOT NULL DEFAULT '0' COMMENT '用户id',
  title varchar(255) not null default '' comment 'Article title',
  PRIMARY KEY (id)
);


Source code
Country.php Country model class
<?php
namespace app\model\ill5;
use \Illuminate\Database\Eloquent\Model;

/**
 * Country model class
 */
class Country extends Model
{
    protected $table = 'countries';
    public $timestamps = false;
    
    /**
     * Get all articles in the specified country
     */
    public function posts()
    {
        // Usually Eloquent foreign key rules will be used when performing this kind of association query, if you want to customize the foreign key of the association,
        //They can be passed as the third and fourth parameters to the hasManyThrough method. The third parameter is the foreign key name of the intermediate model,
        //The fourth parameter is the foreign key name of the final model, and the fifth parameter is the local primary key.
        return $this->hasManyThrough('app\model\ill5\Post', 'app\model\ill5\User', 'country_id', 'user_id', 'id');
    }
}


User model class User.php
<?php
namespace app\model\ill5;
use \Illuminate\Database\Eloquent\Model;

/**
 * User model class
 */
class User extends Model
{
    protected $table = 'users';
    public $timestamps = false;
}


article model class
<?php
namespace app\model\ill5;
use \Illuminate\Database\Eloquent\Model;

/**
 * Post model class
 */
class Post extends Model
{
    protected $table = 'posts';
    public $timestamps = false;
}


main program
<?php
namespace app\control;

use Illuminate\Database\Capsule\Manager;
use app\model\ill5\User;
use app\model\ill5\Country;
use app\model\ill5\Post;

class Ill5
{
    /**
     * Main program.
     */
    public function index()
    {
        $db = new Manager ();
        $db->addConnection ( [
            'driver' => 'mysql',
            'host' => '127.0.0.1',
            'database' => 'test1',
            'username' => 'root',
            'password' => 'root',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => ''
        ] );
        $db->setAsGlobal ();
        $db->bootEloquent ();
      
        $db::delete('delete from users ');
        $db::delete('delete from posts ');
        $db::delete('delete from countries ');
        
        foreach (range(1,2) as $v) {
            $country = new Country();
            $country->name = 'country'.$v;
            $country->id = $v;
            $country->save (); // new model added
        }
        foreach (range(1,5) as $v) {
            $user = new User ();
            $user->name = 'user'.$v;
            $user->id = $v;
            if ($v< 4) {
                $user->country_id = 1;
            }else {
                $user->country_id = 2;
            }
            $user->save (); // new model added
        }
        foreach (range(1,10) as $v) {
            $post = new Post ();
            $post->title = 'Post'.$v;
            $post->id = $v;
            if ($v < 5) {
                $post->user_id = 1;
            }elseif ($v < 9){
                $post->user_id = 2;
            }else  {
                $post->user_id = 5;
            }
            $post->save (); // new model added
        }
        
        echo "<h2>All articles in country 1</h2>";
        echo Country::find(1)->posts->toJson(JSON_UNESCAPED_UNICODE);
        echo "<br>";
        echo "<h2>All articles in country 2</h2>";
        echo Country::find(2)->posts->toJson(JSON_UNESCAPED_UNICODE);
        echo "<br>";
        
        echo '<br>all ok!';
    }
}


Browser output


summary:
There is one more field country_id in the result set, so amazing!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327031165&siteId=291194637