How to get ID from a relationship table, Laravel

1.7k views Asked by At

I have 4 table : Users, CompanyRegister, VoucherDetails, Addvoucher. So the Authenticate Users Id will be submit as user_id in companyRegister table,and then companyRegister ID will be submit as company_id in Voucherdetails table, and lastly voucherDetails Id will be submit in addVoucher table as voucher_ID. I am new to using eloquent and also laravel, I cant understand why I cant get the id from voucherdetails and submit in addvoucher but I can get id from companyregister and submit in company_id in voucherdetails. I'm using the same method to get id but not work, I hope can get solution and explanation here,Thank you in advance!!

My users model

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;
}

my companyregister model

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

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

my voucherdetails model

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


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

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

my addvoucher model

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

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

my 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');
        
    }

my 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');

}
1

There are 1 answers

6
Wesley Smith On BEST ANSWER

This code works because companyregisters is a hasOne relationship for which the docs say:

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

However, this code fails because voucherdetails is a hasMany relationship for which the docs say:

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.

In summary, either your relationship is defined incorrectly (hasMany vs hasOne) or, you'll need to loop over the related records to get the id from each.