Gate errors on Laravel 5.5 - php

I'm following a tutorial about Authorization in laravel, but it seems it got some errors in the code like this:
"Type error: Argument 2 passed to App\Providers\AuthServiceProvider::App\Providers\{closure}() must be an instance of App\Providers\Post, instance of App\Post given, called in E:\xampp\htdocs\cms\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php on line 323 ◀"
I got those error code when I'm accessing http://cms.dev/posts/edit/2. here's the code that i've put on AuthServicesProfider:
public function registerPostPolicies()
{
Gate::define('create-post', function ($user) {
return $user->hasAccess(['create-post']);
});
Gate::define('update-post', function ($user, Post $post) {
return $user->hasAccess(['update-post']) or $user->id == $post->user_id;
});
Gate::define('publish-post', function ($user) {
return $user->hasAccess(['publish-post']);
});
Gate::define('see-all-drafts', function ($user) {
return $user->inRole('editor');
});
}
and here's the code for the update.blade:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Update Post</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('update_post', ['post' => $post->id]) }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<label for="title" class="col-md-4 control-label">Title</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control" name="title" value="{{ old('title', $post->title) }}" required autofocus>
#if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('body') ? ' has-error' : '' }}">
<label for="body" class="col-md-4 control-label">Body</label>
<div class="col-md-6">
<textarea name="body" id="body" cols="30" rows="10" class="form-control" required>{{ old('body', $post->body) }}</textarea>
#if ($errors->has('body'))
<span class="help-block">
<strong>{{ $errors->first('body') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Update
</button>
#can('publish-post')
<a href="{{ route('publish_post', ['post' => $post->id]) }}" class="btn btn-primary">
Publish
</a>
#endcan
<a href="{{ route('list_posts') }}" class="btn btn-primary">
Cancel
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I really have no idea about how to fix it since i really don't understand about authorization in Laravel. Thanks.

You're missing a use statement for the Post class, therefore PHP assumes the gate takes the Post class from the current namespace, hence the App\Providers\Post in the error message.
Add the following in the provider class:
use App\Post;

Related

Can't send $id parameter to controller function Laravel 8

I'm trying to use a custom function in my controller to do a soft delete in my database, but I'm getting the following error.
Too few arguments to function
App\Http\Controllers\EspecialidadesController::borrarEspecialidad(), 0
passed in
D:\xampp\htdocs\SistemaHNF\vendor\laravel\framework\src\Illuminate\Routing\Controller.php
on line 54 and exactly 1 expected
It looks that the $id is not reaching the controller function. I'm going to post the code of the controller, view, and routes.
View
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Editar Especialidad</h1>
<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('especialidades.borrarEspecialidad', $especialidad) }}">
#csrf
#method('PUT')
<div class="form-group row">
<label for="nombre" class="col-md-4 col-form-label text-md-right">{{ __('Nombre') }}</label>
<div class="col-md-6">
<input id="nombre" type="text" readonly class="form-control-plaintext" class="form-control #error('nombre') is-invalid #enderror" name="nombre" value="{{ $especialidad->nombre }}" required autocomplete="nombre" autofocus>
#error('nombre')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-danger">
{{ __('Borrar') }}
</button>
<a class=" button btn btn-primary"href="{{route('especialidades.index')}}">Cancelar</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Controller
public function borrarEspecialidad($id)
{
var_dump($id);
$especialidad = Especialidades::findOrFail($id);
$especialidad->estado = false;
if ($especialidad->save()) {
return redirect()->route('especialidades.index');
} else {
return redirect()->route('especialidades.borrar');
}
}
Routes
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/especialidades/borrar/{nombre}',[EspecialidadesController::class,'borrar'])->name('especialidades.borrar');
Route::put('/especialidades/delete',[EspecialidadesController::class,'borrarEspecialidad'])->name('especialidades.borrarEspecialidad');
Route::resource('/especialidades', EspecialidadesController::class);
Route::get('/gestionarMedicos', [PersonaController::class,'mostrarMedicos'])->name('personaMostrarMedicos');
Route::get('editarMedico',[PersonaController::class])->name('editarMedicos');
I'm new to Laravel, and also English isn't my main language, and I haven't found an answer to my problem on the internet.
For Route try this
Route::post('/especialidades/delete',[EspecialidadesController::class,'borrarEspecialidad'])->name('especialidades.borrarEspecialidad');
In your blade
<form method="POST" action="{{ route('especialidades.borrarEspecialidad') }}">
#csrf
<input name="id" value="{{$yourModel->id ?? 0}}" hidden>
</form>
Controller
public function borrarEspecialidad(Request $request)
{
$id = $request['id'];
$especialidad = Especialidades::findOrFail($id);
$especialidad->estado = false;
if ($especialidad->save()) {
return redirect()->route('especialidades.index');
} else {
return redirect()->route('especialidades.borrar');
}
}

Laravel Login route not working - submit button doesn't work

I am implementing authentication in my Laravel application, the register and logout routes are working perfectly fine, but the login form is not working. I'm not getting any error, but when I click the logi button it doesn't go anywhere.
Here is my login.blade.php :
#extends('layouts.app')
#section('title', 'Login')
#section('content')
<form class="form-horizontal form-material" id="loginform" method="POST" action="{{ route('login') }}>
#csrf
<h3 class="text-center m-b-20">Sign In</h3>
<div class="form-group ">
<div class="col-xs-12">
<input required="" id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" placeholder="Enter your Email" name="email" value="{{ old('email') }}" required autofocus> </div>
#if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
<div class="form-group">
<div class="col-xs-12">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" placeholder="Enter your Password" required>
#if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<div class="d-flex no-block align-items-center">
<div class="custom-control custom-checkbox">
<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 class="ml-auto">
<i class="fas fa-lock m-r-5"></i>{{ __('Forgot Your Password?') }}
</div>
</div>
</div>
</div>
<div class="form-group text-center">
<div class="col-xs-12 p-b-20">
<button class="btn btn-block btn-info btn-rounded" type="submit">{{ __('Login') }}</button>
</div>
</div>
<div class="form-group m-b-0">
<div class="col-sm-12 text-center">
Don't have an account? <b>Sign Up</b>
</div>
</div>
</form>
#endsection
These are my login routes:
$this->get('admin/login', 'Auth\LoginController#showLoginForm')->name('login');
$this->post('admin/login', 'Auth\LoginController#login');
Does anyone know where I messed up?
You can try implementing the standard auth routing wrapped in a prefix to give you want you want:
Route::group(['prefix' => 'admin'], function () {
Auth::routes();
});
That way you do not have to worry about the route names being different from the docs :)

How to send login errors back without any redirect ? Laravel 5.4

In the Laravel code: AuthenticatesUsers.php there's this function that returns error messages back to the user, for example if they didn't enter a registered email address:
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
}
My issue is that I'm using a Modal box so when this function redirects to the same page the modal disappears and the user can only see the errors when he clicks to see the modal again.
How can I send the data back without using redirect() or any refresh ?
This is the AJAX call I'm using:
<script type="text/javascript">
$(function(){
$('#login').click(function(e) {
e.preventDefault();
$('#myModal').modal();
});
$(document).on('submit', '#formLogin', function(e) {
e.preventDefault();
$.ajax({
method: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: "json"
})
.done(function(data) {
console.log(data);
})
.fail(function(data) {
console.log(data);
});
});
})
</script>
For completion, this is the entire Modal with both the registration and login forms:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">recharge.ae | Welcome!</h4>
</div>
<div class="modal-body">
<div id="reg-div">
<h4>New customer? Register below.</h4>
<form class="form-horizontal" method="POST" action="{{ route('register') }}" id="formRegister">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
{{-- <small class="help-block"></small> --}}
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email-reg" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
{{-- <small class="help-block"></small> --}}
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
{{-- <small class="help-block"></small> --}}
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register and Log Me In
</button>
</div>
</div>
</form>
</div><!--#reg-div-->
<div id="login-div">
<h4>Returning customer? Login below.</h4>
<form class="form-horizontal" method="POST" action="{{ route('login') }}" id="loginForm">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div><!-- #login-div -->
</div>
</div>
</div>
</div>
Well, return a JSON response rather than redirecting the page. Here's how to do it. Rather than this
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
write this
return json_encode([ 'errorr' => $$errors ]);
and via above line, you get the data in the done function of the ajax. There you just need to parse that JSON like this
data = JSON.parse(data);
and then you can access the errors like this data.errors but here you need to keep one thing in mind you will need to attach these errors in the html yourself
P.S If there is anything that you don't understand feel free to ask

Laravel can't render view with form and token

I'm trying to render the view with the form reset password and laravel can't do it. Don't have errors but view is all white. If i change the view to other with a simple h1 laravel render it.
So, I guess the problem is with the token.
The route is this:
Route::get('/password/reset/{token}','Auth\AdminResetPasswordController#showResetForm')->name('admin.password.reset');
The controller function this:
public function showResetForm(Request $request, $token = null)
{
return view('auth.passwords.reset-admin')->with(
['token' => $token, 'email' => $request->email]
);
}
And the view file is at
/resources/views/auth/passwords/reset-admin.blade.php
Thanks a lot.
At the end of the render process execute this function:
public function handleShutdown()
{
if (! is_null($error = error_get_last()) && $this->isFatal($error['type'])) {
$this->handleException($this->fatalExceptionFromError($error, 0));
}
}
Blade content:
#extends('backend.public.includes.head')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">ADMIN Reset Password</div>
<div class="panel-body">
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ route('admin.password.request') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ $email or old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Extrainfo:
Take the scripts, but not render the form.
Error on error.loc
2017/07/14 11:19:35 [error] 5048#5048: *1 FastCGI sent in stderr: "PHP message: PHP Warning: require(/home/vagrant/Code/name/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /home/vagrant/Code/name/bootstrap/autoload.php on line 17
PHP message: PHP Stack trace:
PHP message: PHP 1. {main}() /home/vagrant/Code/name/public/index.php:0
PHP message: PHP 2. require() /home/vagrant/Code/name/public/index.php:22
If i put something like alskdaslkd on the view, it render it.
SOLVED
The problem is i use extend to head (where i have all the scripts) but in /layouts/default is where i define my "structure" with #yield('content') so if i put #extends('backend.public.layouts.default') on the top of the blade, i can use #section('content')

Registration Form is not submitting data into Database Laravel 5.2

I have installed laravel authentication(Scaffolding) to my Point of Sale application , registration form was working fine BUT I created an Admin middle ware and I restricted the /register to admin only , I can get the view from my admin panel when admin is logged in but when I submit data it sends nothing to database.
PS: If any thing else is need just mention in comments.
Registration View:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ old('name') }}">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Routes:
Route::group(['middleware' => 'web'], function () {
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get('/register', function(){
return view('auth.register');
})->middleware('isAdmin');
});
HomeController:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
Try using fillable in User.php
For Example:
If you want to store name, email and password, make sure you have
protected $fillable = ['name', 'email', 'password'];
in your User.php

Categories