2

I have an employees table and a roles table. On the from to create an employee I also wish to assign a role. However my code stores the data with NULL on the foreign key in employees table. How do I make it store the relation?

Employees model.php

 class employees extends Model
{
     protected $fillable = [
     'first_name',
     'surname',
     ];

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

Roles model.php

class Roles extends Model
{
    protected $fillable = [
    'role_name',
    ];

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

Controller to create employee

 public function store(EmployeesRequest $request)
{
     $employee = Employees::find($request); 
    Employees::create($request->all());
    Roles::create($request->all());
    $employee->roles()->save($roles);
    return redirect()->route('employees.index')->with('message','Employee has been added'); 

}

On the form I have first_name, surname and role_name. These are saved but with NULL on the role_id column in my employees table? Tried reading the laravel docs but struggling to use ->save method

Current error it is returning is Call to a member function roles() on null

1 Answer 1

0

You have to make the role_id fillable in your Employee Model:

protected $fillable = [
     'role_id', 
     'first_name',
     'surname',
     ];

In your Controller: You need use App\Roles;

public function create()
    {   
        $role = Role::lists('role_name', 'id'); 
        return view('roles.create', compact('role'));

    }

 public function store(EmployeesRequest $request)
    { 
        Employees::create($request->all());
        return redirect()->route('employees.index')->with('message','Employee has been added'); 
    }

Form:

<div class="form-group">
            {!! Form::label('role_id', 'Role:') !!}
            {!! Form::select('role_id', $role, null, ['class' => 'form-control']) !!}
        </div>

HTML-Form: Try something like this, I hope it works its not tested

<label>Role</label>
<select name="role_id">
  @foreach($roles as $role)
    <option value="{{ $role->id }}">{{ $role->role_name }}</option>
  @endforeach
</select

In your Controller:

public function create()
    {   
        $roles = Role::all();    
        return view('roles.create', compact('roles'));

    }
Sign up to request clarification or add additional context in comments.

5 Comments

I have added 'roles_id' as fillable in the employees model. The data for the employee is passing to the db however the role is no longer passing which is why I had 'Roles::create($request->all());' When i do it either way 'role_id' is not populating to show a relationship. In the controller is there a syntax I should include to make it associate the role_id? At the moment I am here but still no luck: $employee = new Employees(); Employees::create($request->all()); Roles::create($request->all()); $roles = $employee->roles()-> associate($employee); return redirect()->
Do you have the foreign key role_id field in the employee table? Do you have role_id in the Employeesrequest?
Yes employees table has 'roles_id' and no EmpoyeesRequest did. I have just tested it with it in and roles_id column is still NULL
thanks that's great. I am not using form collective. How do i render the above in proper html form?
I found a better answer in Stackoverflow to the form for you: stackoverflow.com/questions/29508297/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.