Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel从外键检索值_Php_Mysql_Laravel_Laravel Eloquent - Fatal编程技术网

Php Laravel从外键检索值

Php Laravel从外键检索值,php,mysql,laravel,laravel-eloquent,Php,Mysql,Laravel,Laravel Eloquent,我有这些模型及其相关的数据库表,目前我可以检索所有需求 Requirement::all() 但我只有一个外国密钥列表(目的地id、应用程序id、文档id)。如何直接检索连接到foreigns键的行 class Requirement extends Model { protected $fillable = [ 'required', 'destination_id', 'applier_id', 'doc_id'

我有这些模型及其相关的数据库表,目前我可以检索所有需求

Requirement::all()
但我只有一个外国密钥列表(目的地id、应用程序id、文档id)。如何直接检索连接到foreigns键的行

class Requirement extends Model
  {
    protected $fillable = [
        'required',
        'destination_id',
        'applier_id',
        'doc_id'
    ];

  public function destination()
  {
      return $this->belongsTo(Destination::class);
  }

  public function applier()
  {
     return $this->belongsTo(Applier::class);
  }

  public function doc()
  {
     return $this->belongsTo(Doc::class);
  }
}



class Doc extends Model
{
  protected $fillable = [
        'type',
        'description',
        'note'
  ];

    public function requirements()
    {
      return $this->hasMany(Requirement::class);
    }
 }



class Destination extends Model
{
    protected $fillable = [
        'country',
        'passying_country',
        'transfer_conditions',
        'passing_conditions'
     ];

   public function requirements()
   {
      return $this->hasMany(Requirement::class);
   }
}
您可以使用()调用
,而不是调用
all()
。因此,如果您尝试以下方法:

$requirements =  Requirement::with('destination', 'applier', 'doc')->get();
将其设置为
dd($requirements)
并查看输出


希望它能起作用

很好的解决方案。它返回我想要的,但也返回了外键列表本身,我正尝试使用pulk()避免这种情况,但不起作用如果您认为我的答案对您有帮助,请将其作为一个答案。谢谢您。