php - Laravel One-To-Many insertion on a Pivot table -


i using laravel 5.3 ad having issues on saving through hasmany relationship.

i have tables employee , activity has one-to-many relation i.e each employee perform many activities.

when adding activities against employee using associate() send me error:

badmethodcallexception in builder.php line 2440: call undefined method illuminate\database\query\builder::associate() 

below db structure , code:

database structure:

employee (1st table) – employee_id – employee_name - created_at - updated_at  activity (2nd table) – activity_id – activity_name - created_at - updated_at  employee_activity (pivot table) – employee_activity_employee_id – employee_activity_activity_id 

model class employee:

class employee extends model {     protected $table = 'employee';     protected $primarykey = 'employee_id';      public function activity() {         return $this->hasmany('app\models\activity', 'employee_activity', 'employee_activity_employee_id', 'employee_id');     } } 

model class activity:

class activity extends model {     protected $table = 'activity';     protected $primarykey = 'activity_id';      public function employee() {         return $this->belongsto('app\models\employee', 'employee_activity', 'employee_activity_activity_id' ,'activity_id');     } } 

controller:

public function employeeactivityassociation() {     $employee_id = input::get('employee_id');     $activity_ids = input::get('activity_id'); // array of activities     $employee = employee::find($employee_id);     if (!empty($employee)) {          //adding activity under employee         foreach ($activity_ids $id) {             $activity = activity::find($id);             $employee->activity()->associate($activity);             $employee->save();         }         return 'activities asscoiated employee';     }        else {         return 'employee not found';     } } 

do have relationship defined incorrectly here?

your relation not defined correctly. seeing table structure should many-to-many relationship.

model class employee:

public function activity() {     return $this->belongstomany('app\models\activity', 'employee_activity', 'employee_activity_employee_id', 'employee_id'); } 

model class activity:

public function employee() {     return $this->belongstomany('app\models\employee', 'employee_activity', 'employee_activity_activity_id' ,'activity_id'); } 

so controller should as:

public function employeeactivityassociation() {     $employee_id = input::get('employee_id');     $activity_ids = input::get('activity_id'); // array of activities     $employee = employee::find($employee_id);     if (!empty($employee)) {         $employee->activity()->attach($activity_ids);       return 'activities asscoiated employee';     }        else {         return 'employee not found';     } } 

if want stick one-to-many relation have change table schemas as:

remove table employee_activity table , add column employee_id in activity table define relation according one-to-many.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -