如何从关系 table、Laravel 中获取 ID

How to get ID from a relationship table, Laravel

我有 4 个 table:用户、公司注册、凭证详细信息、添加凭证。 因此,Authenticate Users Id 将作为 user_id 在 companyRegister table 中提交,然后 companyRegister ID 将作为 company_id 在 Voucherdetails table 中提交,最后将提交 voucherDetails Id在 addVoucher table 中作为 voucher_ID。我是 eloquent 和 laravel 的新手,我不明白为什么我无法从 voucherdetails 获取 id 并在 addvoucher 中提交,但我可以从 companyregister 获取 id 并在 company_id 中提交到 voucherdetails .我正在使用相同的方法获取ID但不起作用,希望能在这里得到解决方案和解释,提前谢谢!!

我的用户模型

public function companyregisters()
    {
         return $this->hasOne('App\companyregisters');
    }

public function voucherdetails()
{
     return $this->hasMany('App\voucherdetails');
}

 public function addvoucher()
{
     return $this->hasMany('App\addvoucher');
}

public function roles()
{
    return $this->belongsToMany('App\role');
}

public function hasAnyRoles($roles)
{
    if($this->roles()->whereIn('name', $roles)->first()){
        return true;
    }
    return false;
}

  public function hasRole($role)
{
    if($this->roles()->where('name', $role)->first()){
        return true;
    }
    return false;
}

我公司注册型号

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

public function voucherdetails()
{
     return $this->hasMany('App\voucherdetails');
}

我的优惠券详情模型

  public function User(){
     return $this->belongsTo('User');
}


public function companyregisters(){
     return $this->belongsTo('App\companyregisters');
}

    public function addvoucher()
{
     return $this->hasOne('App\addvoucher');
}

我的 addvoucher 模型

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

public function voucherdetails(){
     return $this->belongsTo('App\voucherdetails');
}

我的voucherdetailsController

         public function store(Request $request){
    
    $voucherdetail = new voucherdetails();

    $voucherdetail->title = $request->input('title');
    $voucherdetail->description = $request->input('description');
    $voucherdetail->user_id = Auth::user()->id;

    $id = Auth::user()->id;
    $user      = User::find($id);
    $company   = $user->companyregisters;
    $companyId = $company->id;

    $voucherdetail->company_id = $companyId;

    $voucherdetail->save();
    
    return redirect()->to('addvoucher');
        
    }

我的 addvoucherController

        public function store(Request $request){

    $addvoucher = new addvoucher();

    $addvoucher->voucherTitle = $request->input('voucherTitle');
    $addvoucher->voucherCode = $request->input('voucherCode');
    $addvoucher->user_id = Auth::user()->id;

//Here(the voucherdetails id cant get to submit in voucher_id)

    $id = Auth::user()->id;
    $user      = User::find($id);
    $voucher   = $user->voucherdetails;
    $voucherID = $voucher->id;

    $addvoucher->voucher_id = $voucherID;

    $addvoucher->save();
    
    return redirect()->to('displayVouchers');

}

此代码有效,因为 companyregisters 是一个 hasOne 关系,文档说:

Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties.

public function companyregisters()
{
   return $this->hasOne('App\companyregisters');
}

$company   = $user->companyregisters; // ie this returns the single related record 
$companyId = $company->id;            // and it has an `id` property, all good here

但是,此代码失败,因为 voucherdetails 是一个 hasMany 关系,文档说:

Once the relationship has been defined, we can access the "collection" of comments by accessing the comments property.

More info on collections

public function voucherdetails()
{
     return $this->hasMany('App\voucherdetails');
}


$voucher   = $user->voucherdetails; // ie this returns a "collection" of related records
$voucherID = $voucher->id;          // this "collection" does NOT have an id property, but each record IN the collection does.

总而言之,您的关系定义不正确(hasMany 与 hasOne),或者您需要遍历相关记录以从每个记录中获取 ID。