简体   繁体   中英

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
{
    //
}

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM