简体   繁体   English

如何获取三级model数据laravel

[英]How to obtain three level model data laravel

Updated更新

User model用户 model

class User extends Authenticatable
{
    use HasFactory, Notifiable, HasApiTokens, HasRoles;

    const MALE = 'male';
    const FEMALE = 'female';

    protected $guard_name = 'sanctum';


    public function educationalBackgrounds()
    {
        return $this->hasMany("App\Models\Users\EducationalBackground", "user_id");
    }

    public function seminars()
    {
        return $this->hasMany("App\Models\Users\Seminar", "user_id");
    }
}

I have child table EducationalBackground which is related to User table我有与用户表相关的子表EducationalBackground

class EducationalBackground extends Model
{
    use HasFactory;

    protected $table = 'users.educational_backgrounds';
    protected $fillable = [
        'user_id',
        'studies_type',
        'year',
        'course',
    ];

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function educationalAwards()
    {
        return $this->hasMany("App\Models\Users\EducationalAward", "educational_background_id");
    }
}

And a third table that i want to access the award field还有我想访问award字段的第三张表

class EducationalAward extends Model
{
    use HasFactory;

    protected $table = 'users.educational_awards';
    protected $fillable = [
        'educational_background_id',
        'award',
        'photo',
    ];

    public function educationalBackground()
    {
        return $this->belongsTo('App\Models\Users\EducationalBackground', 'educational_background_id');
    }
}

I have api get route here我有 api 在这里获取路线

Route::get('/educational-background/{id}', [UserProfileController::class, 'getEducationalBackground']);

Here is my api method it works fine.这是我的 api 方法,它工作正常。 But i want to go deeper and access the data of third table.但我想深入 go 并访问第三个表的数据。

public function getEducationalBackground($id) 
    {
        $educationalBackground = EducationalBackground::with('user')->where('user_id', $id)->get();

        return response()->json($educationalBackground, 200);
    }

It looks like you're not really grasping the concept of relations yet.看起来你还没有真正掌握关系的概念。 Also, I'd advise you to look into route model binding:) What you basically want to be doing is:另外,我建议您查看路由 model 绑定:) 您基本上想要做的是:

public function getEducationalBackground($id) 
{
    $user = User::find($id);
    return $user->educationalBackgrounds()->with('educationalAwards')->get();
}

Also, when you're pretty sure that whenever you want to use backgrounds, you also want to use the awards, you can add the with(...) to the model definition like so:此外,当您非常确定无论何时要使用背景时,您也想使用奖励,您可以将with(...)添加到 model 定义中,如下所示:

class EducationalBackground extends Model
{
    ...
    protected $with = ['educationalAwards'];
}

That way, you can simplify your controller method to:这样,您可以将 controller 方法简化为:

public function getEducationalBackground($id) 
{
    $user = User::find($id);
    return $user->educationalBackgrounds;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM