[英]`Undefined variable: roles` in Laravel blade template
I'm creating a file called edit.blade
to edit users. 我正在创建一个名为
edit.blade
的文件来编辑用户。
The problem is, when i'm trying to make a list with all roles, Laravel prints the following error: 问题是,当我尝试列出所有角色的列表时,Laravel显示以下错误:
Undefined variable: roles (View: /home/ubuntu/workspace/resources/views/user/edit.blade.php)
Here is my edit.blade
template: 这是我的
edit.blade
模板:
@extends ('adminlte::page')
@section('content')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<div class="container" style="margin-left:25%">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Editar datos del Usuario</h2>
</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('user.update', ['user' => $user->id]) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT"/>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">
Nombre
</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ $user->name }}" autofocus style=" ">
@if ($errors->has('name'))
<span class="help-block">
<strong style=" ">{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">
Email
</label>
<div class="col-md-6">
<input id="email" type="text" class="form-control" name="email" value="{{ $user->email }}" autofocus style=" ">
@if ($errors->has('email'))
<span class="help-block">
<strong style=" ">{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="club" class="col-md-4 control-label">
Club
</label>
<div class="col-md-6">
<select name="id_club" >
@foreach($clubs as $club)
<option value="{{$club->id_club}}" class="form-control">{{$club->nombre}}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="roles" class="col-md-4 control-label">
Rol
</label>
<div class="col-md-6">
<select name="role_id" >
@foreach($roles as $role)
<option value="{{$role->id}}" class="form-control">{{$role->name}}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Guardar</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
And my this is my UserController
that calls the blade template with the relevant variables: 这是我的
UserController
,它使用相关变量调用刀片服务器模板:
public function edit(User $user)
{
$roles = Role::all();
$clubs = Club::all();
return view('user.edit', ['user' => $user], ['clubs' => $clubs], ['roles' => $roles]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, User $user)
{
$user->save();
$user->roles()->sync([$request->input('role_id')]);
//$user->update($request->all());
return redirect()->route('user.index');
}
I'm including use App\\Role;
我包括
use App\\Role;
at the top of this file. 在此文件的顶部。
Change this: 更改此:
return view('user.edit', ['user' => $user], ['clubs' => $clubs], ['roles' => $roles]);
to this: 对此:
return view('user.edit', ['user' => $user, 'clubs' => $clubs, 'roles' => $roles]);
The view()
accepts the data you want to send to the view as an array as a second param. view()
以数组的形式接受要发送到视图的数据作为第二个参数。
You can also use view(...)->with(compact('user', 'clubs', 'roles'))
您还可以使用
view(...)->with(compact('user', 'clubs', 'roles'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.