尝试从数据库填充DropDown时出现Laravel错误未定义变量

时间:2019-03-11 14:59:55

标签: php laravel

我是Laravel的新用户,尝试从数据库中将用户类型添加到注册刀片页面中,但每次出现相同错误时,它都会给我  未定义的变量:uTypes(视图:/Applications/MAMP/htdocs/creativeapp/resources/views/auth/register.blade.php)

路线代码web.php:

Route::get('/auth/register', 'UsersController@index');

控制器代码UsersController.php:

 <?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class UsersController extends Controller {
    public function index() {
        $uTypes = DB::table('userstype')->pluck("type_name", "id");
        return view('auth.register', ['uTypes' => $uTypes]);
    }
}

查看代码register.blade.php:

<div class="form-group row">
                        <label for="usertype" class="col-md-4 col-form-label text-md-right">{{ __('User Type') }}</label>

                        <div class="col-md-6">


                                <select name="userType" class="form-control">
                                    <option value="">--Select Type--</option>
                                    @foreach ($uTypes as $ut => $value)
                                    <option value="{{ $ut }}"> {{ $value }}</option>
                                    @endforeach
                                </select>

                        </div>
                    </div>

Route web.php

error

register.blade.php

usercontroller

1 个答案:

答案 0 :(得分:0)

您不需要重写Route,使用auth本机路由。

您需要将status方法覆盖到groups中。

app / Http / Controllers / Auth / RegisterController.php

const data = [
  {
    otherProperty: "string",
    otherPropertyTwo: "string",
    personId: "1269",
    peopleGroups: [
      { group: "SENIORS", groupStatus: "paid" },
      { group: "Infants", groupStatus: "not_paid" }
    ]
  }
];

const statusArrayUpdate = data.map(({ personId, peopleGroups }) => ({
  personId,
  groups: peopleGroups.map(({ group, groupStatus }) => ({
    group,
    status: groupStatus
  }))
}));

console.log(statusArrayUpdate)

然后,别忘了更新验证器,迁移和创建方法