Display Laravel Validation Errors? - laravel

I'm new to Laravel,
here I'm working with Laravel project. I need to display validation errors in form
<div class="col s12 m6">
<div class="row">
<div class="col s12 input-field">
<input name="newpassword" id="newpassword" type="password" class="validate">
<label for="newpassword"> New Password </label>
<small class="email_error"><div class="error">#error('newpassword'){{$message}}#enderror</div></small>
</div>
</div>
</div>
Controller
$this->validate($request, [
'newpassword' => 'required|min:8',
], [
'newpassword.required' => 'New Password is required field.',
'newpassword.min:8' => 'Enter Minimum 8 Characters',
]);
errors :

The easiest way would be to make a shared blade file, which you can use for any error handling (sessions, requests & etc..). The file would look something like this:
#if ( session()->has('success') )
<div class="row mb-3">
<div class="col-sm-12">
<div class="alert text-white" style="background-color: #47afc4; border-radius:5px;">
{!! session('success') !!}
</div>
</div>
</div>
#endif
#if ( session()->has('error') )
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
{!! session('error') !!}
</div>
</div>
</div>
#endif
#if (count($errors) > 0)
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
</div>
</div>
#endif
The session()->has('success') would catch any success session messages that you return in you controller:
return redirect('your route')->with('success', 'Success message');
The session()->has('error') would catch any error session messages that you return in you controller:
return redirect('your route')->with('error', 'Error message');
And $errors->all() will return any errors from the request validation.
You can then use this file in the pages you want, simply include it:
#inlcude('path.to.shared.errors.file')
If you need any more info, let me know.

You can use $errors->any() function
To find your answer look at this :
Displaying the Error Messages in Laravel after being Redirected from controller

Related

Missing required parameter for [Route: rolespermissions.update] [URI: rolespermissions/{rolespermission}] [Missing parameter: rolespermission]

Why am I getting the following error message:
Missing required parameter for [Route: rolespermissions.update] [URI:
rolespermissions/{rolespermission}] [Missing parameter:
rolespermission]. (View:
C:\laragon\www\idoc4\resources\views\rolespermissions\edit.blade.php)
The code for my controller is as follows:
public function edit(Role $role)
{
$permissions = Permission::all();
// return view('rolespermissions.edit',compact('role', 'permissions'));
return view('rolespermissions.edit',[ 'role' => $role, 'permisssions' => $permissions]);
}
My blade code is as follows:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2></h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('rolespermissions.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('rolespermissions.update',$role->id) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="card card-primary m-1" >
<div class="card-header">
Kemaskini Peranan Kebenaran
</div>
<div class="row">
{{ $role->name }}
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
#foreach ($permissions as $permission)
<input type="checkbox" name="permission[]" value="{{$permission->id}}" />{{$permission->name}}<br />
#endforeach
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
#endsection
Appreciate your help. Thank you.
I rewrote the controller as follows: Correct it was sending null before this
public function edit($id)
{
$role = Role::find($id);
// dd($role);
$permissions = Permission::all();
// return view('rolespermissions.edit',compact('role', 'permissions'));
return view('rolespermissions.edit',[ 'role' => $role, 'permissions' => $permissions]);
}

How to keep the modal open if the validation is failed in Laravel 8

I am trying to show the modal with the errors after the failure of the validation directly.
My code now shows the modal with the errors only if I clicked the button that shows it (to add a new user).
I have found some related answers but they did not work for me.
My controller
function add(Request $req){
$validated = $req->validate([
'name' => 'required|regex:/^[\pL\s\-]+$/u',
'email' => 'email|unique:users,email',
'password' => 'required|min:8',
]);
modal.blade
<form action="addUser" method="post" enctype="multipart/form-data">
#csrf
<div class="modal fade text-left" id="ModalCreate" tabindex="-1" role="dialog" aria-hidden="true" >
<div class="modal-dialog modal-lg" role="document" >
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">{{__('Add New User')}} </h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
...
</div>
</div>
</div>
</div>
</form>
The page that calls the modal
#extends('layouts.admin')
#section('content')
<div class="container">
<div class="float-right">
<a href="#" data-toggle="modal" data-target="#ModalCreate">
<button type="button" class="btn btn-dark"><i class="bi bi-person-plus"></i></button>
</a>
</div><br><br>
<table class="table">
...
</table>
</div>
#include('admin.Users.addUser')
#endsection

how to paginate read notifications in Laravel

I got an error related to the pagination of read notifications. Thank you for your help.
This is my controller code that gives this error when it runs
public function NotificationsRead(User $user)
{
$no = Auth::user()->readNotifications()->paginate(7)->latest()->get();
return view('user-profile.notifications-read', compact('user', 'no'));
}
And when I delete latest(), he gives an error to get()
I do not know how to paginate.
and A have two types notifications
You should do
$no = Auth::user()->readNotifications()->latest()->paginate(7);
In this way you are ordering descending before pagination.
Your code orders the result collection created by pagination.
https://laravel.com/docs/8.x/queries#latest-oldest
#jack this is my Blade
<div class="card-header text-black text-bold">
اعلان ها
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<div class="card">
<div class="card-body">
<h5 class="card-title">موضوع:</h5>
#foreach(auth()->user()->readnotifications->where('type','App\Notifications\NewReplySubmittedRoyal') as $notification)
<p class="card-text">{{$notification->data['name']}}
یک پاسخ برای پرسش شما با محتوای:
{{$notification->data['thread_title']}}
در {{$notification->data['time']}}<br>ثبت کرد.
</p>
<form>
<button type="submit" class=" btn btn-outline-success mb-2 ml-2">لینک پرسش</button>
</form>
<hr>
#endforeach
{{--*********** نوع دوم نوتیفیکیشن--}}
#foreach (auth()->user()->readNotifications->where('type','App\Notifications\FollowerNotifications') as $notification)
<p class="card-text">{{$notification->data['name']}} یک پرسش با محتوای:
{{$notification->data['thread_title']}}
ایجاد کرد.
</p>
لینک پرسش
<hr>
#endforeach
{{$no->render()}}
I have two types notifications

Laravel - Session flash did not display the content of the error message

I am using session flash in my Laravel-5.8 project.
Controller
<?php
namespace App\Http\Controllers\Appraisal;
use App\Http\Controllers\Controller;
use App\Models\Appraisal\AppraisalSkill;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Exception;
use Illuminate\Support\Facades\Validator;
use Session;
class AppraisalSkillsController extends Controller
{
public function create()
{
abort_unless(\Gate::allows('skill_create'), 403);
return view('appraisal.skills.create');
}
public function store(Request $request)
{
abort_unless(\Gate::allows('skill_create'), 403);
$this->validate($request, [
'skill_name' => 'required|unique:appraisal_skills,company_id',
]);
$skill = AppraisalSkill::create([
'skill_name' => $request->skill_name,
'description' => $request->description,
'company_id' => Auth::user()->company_id,
'created_by' => Auth::user()->id,
'created_at' => date("Y-m-d H:i:s"),
'is_active' => 1,
]);
Session::flash('success', 'Appraisal Skill is created successfully');
return redirect()->route('appraisal.skills.index');
}
}
view/partials/_messages.blade.php
#if (count($errors) > 0)
<div class="alert alert-danger alert-block" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Errors: </strong>
<ul>
#foreach ($errors as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Session::has('success'))
<div class="alert alert-success" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Success: </strong>{{Session::get('success')}}
</div>
#endif
view
<div class="container-fluid">
<div class="panel-heading clearfix">
<div class="float-right">
<div class="btn-group btn-group-sm" role="group">
<a href="{{ route("appraisal.skills.index") }}" class="btn bg-navy margin" title=" Back">
<span> Back to List</span>
</a>
</div>
</div>
</div>
<br>
#include('partials._messages')
<br>
<div class="card">
<div class="card-header">
Create Skill
</div>
<div class="card-body">
<form action="{{route('appraisal.skills.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Skill Name<span style="color:red;">*</span></label>
<div class="col-md-9 controls">
<input type="text" name="skill_name" placeholder="Enter skill name here" class="form-control" value="{{old('skill_name')}}">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Description</label>
<div class="col-md-9">
<textarea rows="2" name="description" class="form-control" placeholder="Enter Description here" value="{{old('description')}}"></textarea>
</div>
</div>
</div>
</div>
</div>
<div>
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.skills.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
</div>
</div>
</div>
When I click on save submit button, I expect that if there is any error it should display the detail of the error. But, rather it only display Error: without the details.
The success message is working, but the error message is not working as expected
How do I get this resolved?
Thank you.
There is a little mistake when looping through the errors. Change line #foreach ($errors as $error) to #foreach ($errors->all() as $error) . It should work now!

Undefined variable: entry Laravel 5.4 Passing data

At the moment I am trying to pass a value of a variable that is $entry from my EntryController to the view that allows me to view the entry depending on the person who is logged in. The database should only retrieve the entry of the user who is logged in. When I run the page, this error appears:
Undefined variable: entry
Here is part of the entry blade file (viewentry.blade.php):
<div class="row">
#foreach ($entry as $entries)
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image:</strong>
{{ $entries->image }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
{{ $entries->title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{{ $entries->description }}
</div>
</div>
#endforeach
The method to display the entry in the controller (EntryController.php):
public function showUpload(Entries $entry)
{
$entry = Entries::with('entrys')->where('user_id', '=', Auth::user()->id)->get();
return view('viewentry', compact('entry'));
}
I'm not sure why this error is coming up as I can't see anything wrong with the code above. Has anyone had this problem before?, If so how can it be solved?
I think you can try this:
<div class="row">
#if(isset($entry) && !empty($entry))
#foreach ($entry as $entries)
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image:</strong>
{{ $entries->image }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
{{ $entries->title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{{ $entries->description }}
</div>
</div>
#endforeach
#endif
Hope this work for you !!!

Resources