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

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!

Related

Laravel: how to customize login error messages through Fortify

I have the following blade view where I want the error messages printed to me:
<x-layout>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<section class="colorbody vh-100">
<div class="container py-5 h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div class="col-12 col-md-8 col-lg-6 col-xl-5">
<div class="card shadow-2-strong bordercard">
<div class="card-body p-5 text-center">
<form method="POST" action="{{route('login')}}">
#csrf
<div class="d-flex align-items-center mb-3 pb-1">
<i class="fa-brands fa-slack fa-2xl me-3"></i>
<span class="h1 fw-bold mb-0">Logo</span>
</div>
<h5 class="fw-normal mb-3 pb-3 letterspace">Enter in your account</h5>
<div class="form-outline mb-4">
<input type="email" class="form-control form-control-lg" name="email" value="{{ old('email') }}"/>
<label class="form-label" for="form2Example17">Email address</label>
</div>
<div class="form-outline mb-4">
<input type="password" class="form-control form-control-lg" name="password"/>
<label class="form-label" for="form2Example27">Password</label>
</div>
<div class="pt-1 mb-4">
<button class="btn btn-dark btn-lg btn-block" type="submit">Login</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
</x-layout>
Except that if I insert an invalid email and a password, only the error message (automatically obtained from auth.failed) of the following type is displayed:
These credentials do not match our records.
I would like, for example, to have the following error message:
'The email entered is incorrect'
'The password entered is incorrect'
How can I do this? Can anyone kindly help me?
Edit Authentication Language Lines in resources/lang/en/auth.php
return [
'failed' => 'These credentials do not match our records.',
'password' => 'The provided password is incorrect.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
];

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]);
}

Laravel - Error: 403 while trying to upload picture

In my Laravel-5.8, I want to upload picture
Http\Controllers\HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Hr\HrEmployee;
use App\Http\Requests\Hr\Employee\UploadPictureRequest;
class HomeController extends Controller
{
public function update_picture(UploadPictureRequest $request, $id)
{
DB::beginTransaction();
try{
$employee = HrEmployee::find($id);
if ($request->emp_image != "") {
$emp_image = $request->file('emp_image');
$new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
$emp_image->move(public_path('storage/employees/image'), $new_name);
$employee->emp_image = $new_name;
}
$employee->save();
DB::commit();
Session::flash('success', 'Picture Successfully Uploaded');
return redirect()->route('dashboard');
}
catch (Exception $exception)
{
DB::rollback();
Session::flash('error', 'Action failed!');
return redirect()->back();
}
}
}
view
<span data-toggle="tooltip" data-original-title="Click To Upload Picture">
<a class="btn btn-info btn-block text-white" data-toggle="modal" data-target="#upload-picture{{ $employee->id }}" data-original-title="Picture">
<b>Upload My Picture</b>
</a>
</span>
<div class="modal fade" id="upload-picture{{ $employee->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form">
{{ csrf_field() }}
<div class="modal-header">
Self-Review Comment
</div>
<div class="col-md-12">
<div class="text-center">
#if($employee->emp_image != '')
<input type="image" src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="165" disabled/>
<!--<input type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
<div class="row">
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<input type="file" name="emp_image" id="wizard-picture" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
</div>
#else
<input type="image" src="{{asset('theme/adminlte3/dist/img/default.png')}}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="150" disabled/>
<!--<input type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
<div class="row">
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<input type="file" name="emp_image" id="wizard-picture" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
</div>
#endif
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" id="upload_pic_btn-submit" class="btn btn-success btn-ok">Save</button>
</div>
</form>
</div>
</div>
</div>
view\dashboard
The form is a modal form inside a view called dashboard
route/web
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::post('update_picture/{id}', [
'uses' => 'HomeController#update_picture',
'as' => 'update_picture'
]);
When I submited the form, I got this error:
Error: 403 while trying to upload picture
Then when I did php artisan route:list, I got:
| POST | update_picture/{id} | update_picture | App\Http\Controllers\HomeController#update_picture | web,auth |
How do I resolve it?
Thank you.
Or simply you can try this
Route::put('update_picture/{id}''HomeController#update_picture')->name('update_picture');
please add this enctype="multipart/form-data" in form tag
<form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form" enctype="multipart/form-data">
and replace this line $new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
with this line $new_name = rand() . '.' . $request->emp_image->getClientOriginalExtension();

How to fix query in edit view?

i'm setting up a new project to perform multi language form but i'm stuck in edit form i don't know how to handle that
I created my controller and create view the only thing i need is edit view
so you can check my create view in bellow that work fine :
<div class="card-body text-center">
{!! Form::open(['route' => 'content.store', 'method' => 'Post']) !!}
<div class="card">
<div class="card-body">
<div class="form-group">
<label class="mx-4" for="my-input">{{ __('content/form.country_t') }}:</label>
<input id="my-input " type="text" name="country" placeholder="{{ __('content/form.country') }}">
<label class="mx-4" for="my-input">{{ __('content/form.city_t') }}:</label>
<input id="my-input" type="text" name="city" placeholder="{{ __('content/form.city') }}">
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="card col-xs-12 p-0">
<nav>
<div class="nav nav-pills nav-fill card-header" id="nav-tab" role="tablist">
#foreach (config('translatable.locales') as $la=>$desc)
<a class="nav-item nav-link" id="nav-home-tab" data-toggle="tab" href="#{{ $la }}" role="tab" aria-controls="nav-home" aria-selected="true">{{ $desc }}</a> #endforeach
</div>
<div class="tab-content py-3 px-3 px-sm-0 card-body" id="nav-tabContent">
#foreach (config('translatable.locales') as $la=>$desc)
<div class="tab-pane fade px-4" id="{{ $la }}" role="tabpanel" aria-labelledby="nav-home-tab">
<div class="form-group">
<label for="my-input" class="">{{ __('content/form.title') }}</label>
<input id="my-input" class="form-control" type="text" name="translations[{{ $la }}][title]">
</div>
<div class="form-group">
<label for="my-input" class="">{{ __('content/form.body') }}</label>
<input id="my-input" class="form-control" type="text" name="translations[{{ $la }}][body]">
</div>
</div>
#endforeach
</div>
</nav>
</div>
<button type="submit" class="row col-12 mt-2 mx-auto btn btn-primary">{{ __('content/form.submit') }}</button>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
and this is my controller :
public function store(Request $request)
{
$contents = new Content;
// $contents->fill($request->all());
$this->fillRequest($request,$contents);
$contents->User()->associate(\Auth::user());
$contents->saveOrFail();
return redirect()->route('content.index')->with('success','با موفقیت ساخته شد');
}
private function fillRequest(Request $request, Content $model)
{
//fill model on fillable variables
$model->fill($request->only($model->getFillable()));
$model->saveOrFail();
foreach ($request->translations as $la => $desc) {
//if title field is null ignore the translations
// in case of there is a translation... delete it
if (!$desc["title"]) {
if ($model->hasTranslation($la)) {
$model->deleteTranslations($la);
}
continue;
}
//create new translation if not exists
$model->translateOrNew($la)->fill($desc);
$model->saveOrFail();
}
return $model;
}
I need to know how can i create edit view exactly same as my create view above

Store and update method together using modal view laravel

I've a store and update method that I would like to use the same modal box to prompt, however I notice that the store method takes the syntax of
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>
whereas my update method,
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
Is there a way that I can specify which method to use depending on the option chosen, I have two buttons created, each respectively.
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<button class="btn btn-sm btn-warning" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
Is there a way I can call separately using the same modal box or I have to create two duplicate modal box, one for store, the other for update?
My partial code is shown below ..
blade.php
<div class="well col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
<div class="row user-row">
<div class="col-xs-2 col-sm-3 col-md-4 col-lg-4">
<h5 style="font-weight: bold">{{ $user->name }}</h5>
</div>
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 dropdown-user" data-for=".{{ $user->id }}">
<h5 class="glyphicon glyphicon-chevron-down text-muted pull-right"> </h5>
</div>
</div>
<div class="row user-infos {{ $user->id }}">
<div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">User Information</h2>
</div>
<div class="panel-body">
<div class="row">
<div class=" col-md-10 col-lg-10 hidden-xs hidden-sm">
<div class="col-xs-5">User level:</div><div class="col-xs-5"> {{ $user->role->role_description }}</div>
<div class="col-xs-5">Email:</div> <div class="col-xs-5"> {{ $user->email }}</div>
<div class="col-xs-5">Phone number: </div> <div class="col-xs-5"> {{ $user->mobile }} </div>
<div class="col-xs-5">Office extension: </div> <div class="col-xs-5"> [ TO IMPLEMENT ]</div>
</div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-sm btn-warning btn--edit" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
<span class="pull-right">
<button class="btn btn-sm btn-danger" type="button">Inactive <i class="glyphicon glyphicon-remove"></i></button>
</span>
</div>
</div>
</div>
</div>
<input type="hidden" name="user_id" value="{{ $user->id }}" />
#endforeach
</div>
#if(Session::has('flash_message'))
<div class="alert alert-success col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
{{ Session::get('flash_message') }}
</div>
#endif
<div class="col-sm-offset-1 col-sm-2">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<!-- Modal -->
<div id="form" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User Information</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-3" for="name">Username:</label>
<div class="col-sm-5 #if ($errors->has('name')) has-error #endif">
<input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password">Password:</label>
<div class="col-sm-5 #if ($errors->has('password')) has-error #endif">
<input type="password" class="form-control" type="hidden" id="password" name="password" placeholder="Enter login password">
#if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> #endif
</div>
</div>
...
controller.php
class ManageAccountsController extends Controller
{
public $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function index()
{
$users = User::orderBy('name')->get();
$roles = Role::all();
return view('manage_accounts', compact('users', 'roles'));
}
public function store(StoreUserRequest $request)
{
// validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
$this->userRepository->upsert($request);
Session::flash('flash_message', 'User successfully added!');
return redirect()->back();
}
public function update(StoreUserRequest $request, $id)
{
// validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
$this->userRepository->upsert($request, $id);
Session::flash('flash_message', 'User successfully updated!');
return redirect()->back();
}
}
class UserRepository {
public function upsert($data, $id)
{
// You will also need something like this
if(isset($id))
{
$user = User::find($id);
}
else {
$user = new User;
}
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = Hash::make($data['password']);
$user->mobile = $data['mobile'];
$user->role_id = $data['role_id'];
// save our user
$user->save();
return $user;
}
}

Resources