Sok Ma
Sok Ma

Reputation: 137

How to fill a select using Laravel?

I'm a beginner in laravel, I have a submit form add salary, table salary contains a foreign key to table function, in the form I want to fill select with function_id to choose the function of a salary, but it gives me error Undefined variable : functions.

create.blade.php

<select class="form-control" name="function_id">
    <option></option>
    @foreach($functions as $function)
        <option value="{{ $function->id }}">{{ $function->function}}</option>
    @endforeach
</select>

salaryController.php

<?php

namespace App\Http\Controllers;
use App\Salary;
use App\Function;
use Illuminate\Http\Request;

class SalaryController extends Controller
{
    public function getFunctions(Request $request)
    {
        $functions = Function::get();

        return view("salary.create", compact("functions"));
    }
}

web.php

Route::get('/getFunctions','SalaryController@getFunctions');

Function.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Function extends Model
{
    //
}

Upvotes: 0

Views: 236

Answers (1)

Vincent Decaux
Vincent Decaux

Reputation: 10714

I just the name of your variable is wrong in your controller :

class SalaryController extends Controller
{
    public function getFunctions(Request $request)
    {
        $functions = Function::get();

        // you put "fonctions"
        return view("salarie.create", compact("functions"));
    }
}

EDIT

You fix the variable name in your last edit ?

Upvotes: 1

Related Questions