"Undefined index: password" laravel 5.6 - laravel

i have three roles Hr,staff and admin... i use 3 middleware guards to guard each role.... but don't know why the staff(default web guard) won't allow me login... giving undefined index password error.
// controller function
public function logged(Request $request)
{
$this->validate($request,[
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('web')->attempt(['email'=>$request->email,
'password' => $request->password]))
{
return redirect()->intended(url('/home'));
}
Session::flash('message','Invalid Login details');
return redirect()->back()->withInput($request->only('email','remember'));
}
//my view
<div class="m-t-40 card-box">
<div class="panel-body">
<div class="login-form">
<h4><center>Login</center></h4>
<form action="{{route('login.submit')}}" method="POST" data-parsley-validate >
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
<input type="text" name="email" placeholder="Email address" value="{{old('email')}}" required>
<input type="password" name="password" class="pass" placeholder="Password" required>
<span class="check-left"><input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me</span>
<span class="check-right">Forgot password?</span>
<div class="clearfix"></div>
<button class="btn btn-info btn-block" type="submit">Sign in</button>
<p class="center-block mg-t mg-b">Dont have and account?
Register here.
</p>
</form>
</div>
</div>
</div
// config/auth
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin'=> [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
'hr' => [
'driver' => 'session',
'provider' => 'hrs',
],
'hr-api' =>[
'driver' => 'token',
'provider' => 'hrs'
],
],
// screenshot of the error

Related

Always show [The post image must be a file of type: jpg, jpeg, png, webp.] laravel

It always shows error in validation. I tried uploading png jpeg jpg and all showed error.
View
<form action="{{ route('post.store') }}" method="POST" enctype="multipart/form-data">
<div id="post_image_container">
<input type="file" name="post_image[]" id="post_image" class="bg bg-light border rounded text-dark font w-100 p-3 form-control" placeholder="Add Images" accept="image/*" multiple>
#if($errors->first('post_image'))
<small class="form-text d-block text-danger fw-bold">{{ $errors->first('post_image') }}
</small>
#endif
</div>
Controller - Validation
$this->validate($request, [
'post_title' => ['required', 'min:5'],
'post_tags' => ['required', 'min:5'],
'post_content' => ['required', 'min:50'],
'post_image' => ['required', 'mimes:jpg,jpeg,png,webp']
]);
dd($request->all());
You are uploading multiple images, therefore, you need to state that in your validating by writing this:
$this->validate($request, [
'post_title' => ['required', 'min:5'],
'post_tags' => ['required', 'min:5'],
'post_content' => ['required', 'min:50'],
'post_image' => ['required', 'array'],
'post_image.*' => ['required', 'mimes:jpg,jpeg,png,webp'],
]);

Laravel authentication problem with multiple guards

im new to laravel and im trying to create login function with multiple guards. However when i insert the correct credentials in the form, it does not authenticate properly. I cant seem to find the problem with my login controller, any ideas?
This is my AdminLoginController
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminLoginController extends Controller
{
public function __construct()
{
$this->middleware('guest:admin');
}
public function showLoginForm()
{
return view('auth.admin-login');
}
public function login(Request $request)
{
// validate the form data
$this->validate($request, [
'admin_email ' => 'required|email',
'password' => 'required|min:8'
]);
//attempt to log the user in
if( Auth::guard('admin')->attempt(['admin_email'=>$request->admin_email, 'admin_password'=>$request->admin_password], $request->remember))
{
//if successful then send to their location
return redirect()->route('admin.dashboard');
}
//if fail redirect back to the login with the data
return redirect()->back()->withInput($request->only('admin_email','remember'));
}
}
This one is my form
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Admin-Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.login.submit') }}">
#csrf
<div class="row mb-3">
<label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label>
<div class="col-md-6">
<input id="admin_email" type="email" class="form-control #error('admin_email') is-invalid #enderror" name="admin_email" value="{{ old('admin_email') }}" required autocomplete="admin_email" autofocus>
#error('admin_email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-3">
<label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="admin_password" type="password" class="form-control #error('admin_password') is-invalid #enderror" name="admin_password" required autocomplete="current-admin_password">
#error('admin_password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-3">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
#if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
#endif
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
This one is the Model
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class Admin extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $guard = 'admin';
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* #var array<int, string>
*/
protected $fillable = [
'admin_email',
'admin_password',
];
/**
* The attributes that should be hidden for serialization.
*
* #var array<int, string>
*/
protected $hidden = [
'admin_password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* #var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Edited: This is my route
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::prefix('admin')->group(function (){
Route::get('/login', [App\Http\Controllers\Auth\AdminLoginController::class, 'showLoginForm'])->name('admin.login');
Route::post('/login', [App\Http\Controllers\Auth\AdminLoginController::class, 'login'])->name('admin.login.submit');
Route::get('/', [App\Http\Controllers\adminController::class, 'index'])->name('home')->name('admin.dashboard');
});
my dd($request)
Illuminate\Http\Request {#43 ▼
#json: null
#convertedFiles: null
#userResolver: Closure($guard = null) {#260 ▶}
#routeResolver: Closure() {#268 ▶}
+attributes: Symfony\Component\HttpFoundation\ParameterBag {#45 ▶}
+request: Symfony\Component\HttpFoundation\InputBag {#44 ▶}
+query: Symfony\Component\HttpFoundation\InputBag {#51 ▶}
+server: Symfony\Component\HttpFoundation\ServerBag {#47 ▶}
+files: Symfony\Component\HttpFoundation\FileBag {#48 ▶}
+cookies: Symfony\Component\HttpFoundation\InputBag {#46 ▶}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#49 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/admin/login"
#requestUri: "/admin/login"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: Illuminate\Session\Store {#300 ▶}
#locale: null
#defaultLocale: "en"
-preferredFormat: null
-isHostValid: true
-isForwardedValid: true
-isSafeContentPreferred: null
basePath: ""
format: "html"
}
This is my config/auth file
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'resident' => [
'driver' => 'session',
'provider' => 'residents',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
'residents' => [
'driver' => 'eloquent',
'model' => App\Models\Resident::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
'residents' => [
'provider' => 'residents',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
'admin' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],

Laravel 5.7.17 file validation issue

The example is based on Laravel's registration.
I have added following to register.blade.php:
<div class="form-group row">
<label for="file" class="col-md-4 col-form-label text-md-right">{{ __('Files') }}</label>
<div class="col-md-6">
<input type="file" id="files" name="files[]" multiple>
</div>
</div>
The method in RegisterController looks like this:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'files.*' => ['required', 'file'],
]);
dd($validator->errors());
}
I'm trying to upload a PDF and a DOC file.:
MessageBag {#236 ▼
#messages: array:2 [▼
"files.0" => array:1 [▼
0 => "The files.0 must be a file."
]
"files.1" => array:1 [▼
0 => "The files.1 must be a file."
]
]
#format: ":message"
}
Must be a file? These are files...
just add enctype="multipart/form-data" to your form:
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
Try this :
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'files.*' => ['required', 'mimes:doc,docx,pdf,txt'],
]);
dd($validator->errors());
}

why not update the picture? laravel 5.4

I can upload a picture, but when I try to update it, then there is no error. The file name changes in the database to the name of the new picture. And in the public folder the picture remains old and does not appear new.
I use: Intervention Image
What I did not understand, help please.
Controller: UploadController
use Illuminate\Http\Request;
use App\Post;
use Image;
use Storage;
use Faker\Provider\File;
public function update (Request $request, $id)
{
//validate
$this->validate($request, [
'title' => 'required|max:255',
'author' => 'required',
'text' => 'required',
'desc' => 'required',
'image' => 'required',
]);
$posts = Post::find($id);
$posts->title = $request->input('title');
$posts->author = $request->input('author');
$posts->text = $request->input('text');
$posts->desc = $request->input('desc');
$posts->image = $request->input('image');
//update image
if ($request->hasFile('image'))
{
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/'. $filename);
Image::make($image)->resize(800, 400)->save($location);
$oldFilename = $posts->image;
//update db
$posts->image = $filename;
//delete old image
Storage::delete($oldFilename);
}
$posts->save();
return redirect('/');
}
View: edit.blade.php
<div class="container">
<form method="POST" action="{{ route('goUpdate', [$posts->id]) }}">
{{ csrf_field() }}
{!! method_field('patch') !!}
#if($posts)
<div class="form-group">
<br>
<label>title</label>
<input name="title" type="text" class="form-control" value="{{ $posts->title }}">
</div>
<div class="form-group">
<label>author</label>
<input name="author" type="text" class="form-control" value="{{ $posts->author }}">
</div>
<div class="form-group">
<label>text</label>
<textarea name="text" class="form-control" rows="7">{{ $posts->text }}</textarea>
</div>
<div class="form-group">
<label>desc</label>
<textarea name="desc" class="form-control" rows="5">{{ $posts->desc }}</textarea>
</div>
<div class="form-group">
<label>image</label>
<input type="file" name="image" class="form-control-file" value="" >
</div>
<div>
<input name="submit" type="submit" class="btn btn-primary" value="update"/>
back
</div>
#endif
</form>
<br>
filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
Try using this
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalName();
$path = public_path('events/' . $filename);
Image::make($image->getRealPath())->resize(300, 300)->save($path);
$event->image = 'events/' . $filename;
Hope this can help you

Laravel validation not showing the default error messages

I am working on a laravel project and I am having some issues with the validation.
The default error messages dont appear instead I get to see the validation requirements like this: http://gyazo.com/681e9d8e2e176a29d90db041354f7177
this is my code:
routes.php (I put all the code in here for now)
Route::filter('checkLogin', function()
{
if(Input::GET('email') != ""){ //register
$rules =
array(
'username' => 'required|max:64|min:3|unique:users',
'password' => 'required|max:64|min:6',
'fname' => 'required|max:255|alpha',
'lname' => 'required|max:255|alpha',
'email' => 'required|max:255|email',
'phone' => 'max:24|min:9',
'zip' => 'required',
'street' => 'required|max:255|alpha',
'housenumber' => 'required|max:6|numeric',
'country' => 'required',
'avatar' => 'max:32'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('/')->withInput()->withErrors($rules);
}
}
});
this is how the code from the view:
<div class="fields">
<div class="field">
<i class="fa fa-user"></i>
{{ Form::text('username', null, ['placeholder' => 'Username', 'tabindex' => 1]) }}
{{ $errors->first('username') }}
</div>
<div class="field">
<i class="fa fa-lock"></i>
{{ Form::password('password', ['placeholder' => 'Password', 'tabindex' => 2]) }}
{{ $errors->first('password') }}
</div>
</div>
When validation fails, you are returning your $rules as the errors. Change this line:
return Redirect::to('/')->withInput()->withErrors($rules);
to this:
return Redirect::to('/')->withInput()->withErrors($validator);

Resources