从关系 laravel 中的另一个 table 获取 ID
Getting id from another table in a relationship laravel
我想从 pdf_Files table 获取数据,它只会显示基于它的文件名 user_id。我真的不知道如何解释它,但也许如果你看到我的代码,你就会明白。我已经完成这2 table之间的关系了
我希望它是这样的:
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 函数 downfunc(请求 $request){
$downloads=DB::table('pdf_files')->get();
return view('download.viewfile',compact('downloads'));
}
pdf文件模型:
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');
}
}
您是要一次搜索多个用户 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
我想从 pdf_Files table 获取数据,它只会显示基于它的文件名 user_id。我真的不知道如何解释它,但也许如果你看到我的代码,你就会明白。我已经完成这2 table之间的关系了
我希望它是这样的:
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 函数 downfunc(请求 $request){
$downloads=DB::table('pdf_files')->get();
return view('download.viewfile',compact('downloads'));
}
pdf文件模型:
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');
}
}
您是要一次搜索多个用户 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