我在按下表格行中的按钮后尝试更新数据库中的“付费”记录,但按下按钮时显示此错误:
0db55b8fee884912074e1a4700061051aeb18bcc.php行中的ErrorException 15:未定义的变量:users(查看: /Users/mauricio/code/cnr/resources/views/pages/users.blade.php)
我不知道为什么在我之前在刀片文件中使用users变量时它会给我这个错误。
这是我的注册管理员:
public function usersAdmin()
{
$users = DB::table('users')->get();
return view('pages.users', compact('users'));
}
欢迎控制器:
// for admin only
Route::get('/users', 'WelcomeController@usersAdmin');
Route::post('/users/{id}', 'RegistrationController@updatePay');
路由文件:
@extends('layouts.master')
@section('content')
<table class="highlight">
<thead>
<tr>
<th data-field="id" hidden="">ID</th>
<th data-field="name">Nombre</th>
<th data-field="last_name">Apellidos</th>
<th style="text-align: right;">Acciones</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->last_name}}</td>
<td style="text-align: right; width: 30em">
@if( $user->isAdmin )
{{"ADMINISTRADOR"}}
@elseif( $user->pay )
<a class="waves-effect waves-light btn yellow darken-2"><i class="material-icons left" style="margin:0">create</i></a>
<a class="waves-effect waves-light btn red darken-1"><i class="material-icons left" style="margin:0">delete</i></a>
@else
<form method="POST" action="/users/{{$user->id}}">
{{ csrf_field() }}
<button type="submit" class="waves-effect waves-light btn green lighten-1"><i class="material-icons left" style="margin:0">done</i></button>
</form>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
这是我的观点
insert . . . exec
答案 0 :(得分:1)
您需要检索用户并将其发送到类似的视图:
<强>控制器:强>
public function methodName()
{
$users = User::all();
return view('path.to.view')->with(compact('users'));
}
此外,您有两种选择:
<强>第一强>
public function updatePay($id)
<强>第二强>
public function updatePay(User $user)
{
DB::table('users')->where('id', $user->id)->update(['pay' => 1]);
而不是return view('pages.users');
在return redirect()->url('users');
方法中使用updatePay
。