从关系laravel中的另一个表中获取id

时间:2017-10-27 01:57:27

标签: php mysql database laravel one-to-many

我想从pdf_Files表中获取数据,它只显示基于user_id的文件名。我真的不知道如何解释它,但也许如果你看到我的代码,你会理解。我已经完成了这两个表之间的关系

我希望它像:

DB::table('pdf_files')->where(table:personal_infos->id = pdf_files->user_id)->get();  --> I want it to be something like that but I do not know how to get it

这是我的代码: 控制器:

public function downfunc(Request $ request){

$downloads=DB::table('pdf_files')->get();
return view('download.viewfile',compact('downloads'));

}

pdfFile模型:

   protected $fillable = array('file_name','file_size','user_id');

public function personal_infos() {
    return $this->belongsTo('App\personal_info', 'user_id', 'id');
}

personal_info模特:

class personal_info extends Eloquent
{
    protected $fillable = array('Email', 'Name');
    protected $table = 'personal_infos';
    protected $primaryKey = 'id';

    public function pdfFiles() {
    return $this->hasMany('App\pdfFile','user_id');
}

}

1 个答案:

答案 0 :(得分:0)

您是要一次搜索多个用户ID还是从请求中获取ID?

如果您从请求中获取信息,您可能希望执行以下操作:

use App\pdfFile; //Put this above the class

pdfFile::where('user_id', $request->user_id)->get(); //If you are checking to see if something equals something else in eloquent, you do not have to use equals