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

Eden WebStudio
  • 687
  • 2
  • 14
  • 34

1 Answers1

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

    }
Javier Gonzalez
  • 311
  • 2
  • 5
  • 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()-> – Eden WebStudio Sep 28 '16 at 16:16
  • Do you have the foreign key role_id field in the employee table? Do you have role_id in the Employeesrequest? – Javier Gonzalez Sep 28 '16 at 18:58
  • 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 – Eden WebStudio Sep 28 '16 at 19:12
  • thanks that's great. I am not using form collective. How do i render the above in proper html form? – Eden WebStudio Sep 29 '16 at 11:51
  • I found a better answer in Stackoverflow to the form for you: http://stackoverflow.com/questions/29508297/laravel-5-how-to-populate-select-box-from-database-with-id-value-and-name-value – Javier Gonzalez Sep 29 '16 at 14:40