我提交用戶名時出現Method [getById] does not exist
錯誤。Controller.php中的BadMethodCallException行107:方法[getById]不存在
用戶名控制器:
use Auth;
use MostWanted\Models\User;
use Illuminate\Http\Request;
class UsernameController extends Controller
{
public function getUsername()
{
return view('dashboard.getusername');
}
public function postUsername(Request $request)
{
$id = Auth::id();
$user = $this->getById($id);
$this->validate($request, [
'username' => 'required|unique:users|regex:/^[A-Za-z0-9.]+$/|max:50',
]);
$user->username = $request->input('username');
return view('home');
}
}
路線:
Route::group(['middleware' => ['web']], function() {
/**
*Entering Username
*/
Route::get('/choose_username', [
'uses' => '\MostWanted\Http\Controllers\[email protected]',
'as' => 'dashboard.getusername',
]);
Route::post('/choose_username', [
'uses' => '\MostWanted\Http\Controllers\[email protected]',
]);
});
getusername.blade.php:
@extends('templates.default')
@section('title')
MostWanted | Enter your username
@endsection
@section('content')
<div style="min-width: 800px; margin: 0 auto; position: relative; top: 75px;">
<div class="row" style="width: 600px; margin: 0 auto; border: 2px solid #000; padding: 40px;">
<div class="col-lg-6" style="width: 600px; color: #999; padding: 44.5px 0;">
<h3 style="color: #000; margin-top: 0; margin-bottom: 10px;">Enter a username</h3>
<form class="form-vertical" role="form" method="post" action="{{ route('dashboard.getusername') }}">
<div>
<div>Your public username is the same as your Profile address:
<div>
<ul>
<li>
<div>mostwanted.com/<span id="display_name">
<script>
$('#username').keyup(function() {
$('#display_name').text($(this).val());
});
</script>
</span></div>
</li>
</ul>
</div>
</div>
<div class="form-group {{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="control-label">Choose a username</label>
<input style="width: 456px;" placeholder="e.g. {{ Auth::user()->getUsernameOrName() }}" type="text" name="username" class="form-control" id="username" value="">
@if ($errors->has('username'))
<span class="help-block" style="font-size: 12px; margin-bottom: 0;">{{ $errors->first('username') }}</span>
@endif
</div>
<div>Note: Your username cannot be changed and should include your authentic name </div>
</div>
</div>
<div class="form-group" style="margin-top: 15px;">
<button style="float: right;" type="submit" class="btn btn-primary">Save username</button>
</div>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
</div>
</div>
@stop
難道我定義$id
錯了還是怎麼的? getusername.blade.php是爲第一次登錄用戶輸入用戶名的表單,條件起作用,並且我通過MySQL Workbench將用戶名輸入到數據庫中,但是當我嘗試輸入用戶名時,會出現錯誤。我想確保輸入的用戶名將與登錄用戶輸入的詳細信息對齊。上次我這樣做時,輸入的用戶名每次都會創建新的記憶令牌。
我將'$ id'更改爲'$ id = User :: id()後調用了未定義的方法Illuminate \ Database \ Query \ Builder :: id );''和'$ user = $ this-> find($ id);' – maftyycs