Reputation: 179
I have laravel application! I use original laravel authentication from
php artisan make:auth
My question is how to check if name already exist in database and if exist to return error message!
My user table structure is:-
user:
id - UNIQUE
name
email
password
remembertoken
timestamps
Upvotes: 0
Views: 4896
Reputation: 66
You can use auth()->user()->name in blade file.
<?php $name=auth()->user()->name; ?>
Upvotes: 0
Reputation: 1439
You actually have two solutions:
See the code below
$user = User::where("name", $nameToTestAgainst)->first();
if($user!= null) {
$v->errors()->add('Duplicate', 'Duplicate user found!');
return redirect('to-your-view')
->withErrors($v)
->withInput();
}
Upvotes: 0
Reputation: 1855
Following is quoted from there:
Use the "unique" rule for name.
$request->validate([
'name' => 'required|unique:users'
]);
And display error like this:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div> @endif
Upvotes: 3