01/10/2018, 17:12
Vấn đề về Route API trong Laravel (lỗi Undefined variable: errors)
Mình gặp vấn đề về Route trong laravel.
Khi controller gọi view chứa form update thông tin người dùng, thì vấn đề xảy ra. Nếu mình gọi route trong file api.php như sau thì xảy ra lỗi Undefined variable: errors :
Route::get('/', 'APIUserController@index');
Route::apiResource('user', 'APIUserController', [ 'except' => [ 'index' ] ]);
File usercontroller :
namespace AppHttpControllersAPI;
use IlluminateHttpRequest;
use AppHttpControllersController;
use AppUser ;
use Auth;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
return view('welcome') ;
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
return view('user.edit') ;
}
/**
* Display the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function show($name)
{
$user = User::where('username',$name)->first() ;
return view('user.display',compact('user')) ;
}
public function edit($name) {
$user = User::where('username',$name)->first() ;
return view('user.edit',compact('user')) ;
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'hobby' => 'required',
'objective' => 'required',
'quote' => 'required'
]);
$user = User::find($id);
$user->hobby = $request->input('hobby') ;
$user->objective = $request->input('objective') ;
$user->quote = $request->input('quote') ;
$user->save() ;
return redirect()->route('user.show',$user->username) ;
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
//
}
}
view form update của mình:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('user.update', $user)}}" >
{{ method_field('PUT') }}
@csrf
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control{{ $errors->has('username') ? ' is-invalid' : ' }}" name="username" value="{{ Auth::user()->username }}" required autofocus disabled="">
@if ($errors->has('username'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : ' }}" name="email" value="{{ Auth::user()->email }}" required disabled="">
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="hobby" class="col-md-4 col-form-label text-md-right">{{ __('Hobby') }}</label>
<div class="col-md-6">
<pre><textarea class="form-control" name="hobby" required="">{{ Auth::user()->hobby }}</textarea></pre>
@if ($errors->has('hobby'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('hobby') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="objective" class="col-md-4 col-form-label text-md-right">{{ __('Object') }}</label>
<div class="col-md-6">
<pre><textarea class="form-control" name="objective" rows="4" required="">{{ Auth::user()->objective }}</textarea></pre>
@if ($errors->has('objective'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('objective') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="quote" class="col-md-4 col-form-label text-md-right">{{ __('Quote') }}</label>
<div class="col-md-6">
<pre><textarea class="form-control" name="quote" required="">{{ Auth::user()->quote }}</textarea></pre>
@if ($errors->has('quote'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('quote') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-outline-primary">Confirm</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
màn hình báo lỗi:
Mình cám ơn mọi người trước.
Bài liên quan
Thằng $errors chưa được khởi tạo. Không thể gọi các hàm của nó.