ThinkPHP 5 how to check data by cross-table one-to-many Has Many Through

you could have checked User, through Conuntry and then Post, through User. Now you can check Post; directly through Country
usually the relationships between Country and User,User and Post are established beforehand, and then you can use hasManyThrough;

at this time.

Table structure:
country
id-integer
name-string

user
id-integer
country_id-integer
name-string

post
id-integer
user_id-integer
title-string

namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
    public function posts()
    {
        return $this->hasManyThrough("Post", "User", "country_id", "user_id");
        
    }
}

call:

$country = Country::get(2);
$posts = $country->posts();

this allows you to get id=2, all of the country"s articles. But which high-ranking person knows how to tie back through an article which country id=100 belongs to?

Mar.03,2021
Menu