Has many relationships help.

Published 3 weeks ago by nizam0786

Hi guys,

I am trying to setup a has many relationship but just a bit confused. Everything was working until I changed my database names and also in the model.

Before I would call the following $pass->process and I could loop through all the processes belonging to the pass now its returning an error exception 'Property [process] does not exist on this collection instance.'

Pass Model.

 protected $primaryKey = 'gtcPasses_ID';


 public function process()
    {
        return $this->hasMany('App\Models\PassProcess', 'gtcPassesProcess_gtcPasses_ID');
    }

PassProcess Model:


protected $primaryKey = 'gtcPassesProcess_ID';


 public function pass()
    {
        return $this->belongsTo('App\Models\Pass', 'gtcPassesProcess_gtcPasses_ID');
    }

the code above is the primary keys for each model and the relationship could someone please check it and help. Thanks

Best Answer (As Selected By nizam0786)
tisuchi

Your relationships declear should be like this-

In PassProcess.php

public function pass()
{
    return $this->belongsTo('App\Models\Pass', 'gtcPassesProcess_gtcPasses_ID', 'gtcPasses_ID');
}

In Pass.php

public function process()
{
    return $this->hasMany('App\Models\PassProcess', 'gtcPassesProcess_gtcPasses_ID', gtcPasses_ID'');
}

Note: Adjust your foreign and local key if you feel that I am wrong.

Ref: https://laravel.com/docs/5.5/eloquent-relationships#one-to-many

tisuchi
tisuchi
3 weeks ago (265,395 XP)

I can feel that you have issue with foreign key. Can you show your tables columns?

nizam0786

which columns would you like to see?

tisuchi
tisuchi
3 weeks ago (265,395 XP)

Can you show me Pass and PassProcess tables columns?

nizam0786

Here are the columns belonging to the PassProcess Model


'gtcPassesProcess_ID',
        'gtcPassesProcess_gtcPasses_ID',
        'gtcPassesProcess_Count',
        'gtcPassesProcess_DateQueued',
        'gtcPassesProcess_Reason_ID',
        'gtcPassesProcess_QueuerUsers_ID',
        'gtcPassesProcess_AuthorisorUsers_ID',
        'gtcPassesProcess_Authorised',
        'gtcPassesProcess_Status',
        'gtcPassesProcess_gtcPaymentMethods_ID',
        'gtcPassesProcess_Charge',
        'gtcPassesProcess_DateAuthorised',
        'gtcPassesProcess_PassSerialNumber',

Here are the columns belonging to the PassModel


'gtcPasses_ID',
        'gtcPasses_gtcAcademicYears_ID',
        'gtcPasses_gtcTraveler_ID',
        'gtcPasses_gtcSchools_ID',
        'gtcPasses_gtcSchoolGroups_ID',
        'gtcPasses_gtcBusRoutes_ID',
        'gtcPasses_gtcPassType_ID',
        'gtcPasses_BusPassCover',
        'gtcPasses_PassPrice',
        'gtcPasses_OptionsPrice',
        'gtcPasses_SiblingDiscount',
        'gtcPasses_Yr1113Discount',
        'gtcPasses_EarlyAppDiscount',
        'gtcPasses_OtherDiscount',
        'gtcPasses_Deleted',
        'gtcPasses_QueuedTotal',
        'gtcPasses_ProcessedTotal',
        'gtcPasses_ThirdPartyPayment',
        'gtcPasses_Photo',
        'gtcPasses_ThirdParty'

nizam0786

Any idea?

tisuchi
tisuchi
3 weeks ago (265,395 XP)

Your relationships declear should be like this-

In PassProcess.php

public function pass()
{
    return $this->belongsTo('App\Models\Pass', 'gtcPassesProcess_gtcPasses_ID', 'gtcPasses_ID');
}

In Pass.php

public function process()
{
    return $this->hasMany('App\Models\PassProcess', 'gtcPassesProcess_gtcPasses_ID', gtcPasses_ID'');
}

Note: Adjust your foreign and local key if you feel that I am wrong.

Ref: https://laravel.com/docs/5.5/eloquent-relationships#one-to-many

nizam0786

Thank you, will give it a go and let you know.

nizam0786

Still returning the same error?

tisuchi
tisuchi
3 weeks ago (265,395 XP)

Have you adjusted foreign key?

nizam0786

where and how would I adjust the foreign key?

nizam0786

I just added gtcPasses_ID as a third parameter.

nizam0786

apart from the foreign key in the above relationship I haven't adjusted anywhere else.... is there somewhere else where it needs adjusting?

nizam0786

????

Please sign in or create an account to participate in this conversation.