Undefined variable: errors Lumen 5.4 - php

I got this error Undefined variable: error in Lumen 5.4
web.php
$app->get('/', 'SubscribersController#index');
$app->post('/', 'SubscribersController#store');
controller
public function store(Request $request)
{
$this->validate($request, [
'email' => 'required|email|unique:subscribers,email'
]);
app('db')->table('subscribers')->insert([
'email' => $request->input('email'),
]);
return redirect('/');
}
index.blade.php
{{dd($errors)}}
So, how i can get my error? I can get error only in json format, but i want put this error on index.blade.php how this is possible?
{
"email": [
"The email must be a valid email address."
]
}
I want see this error in index.blade.php

#if($errors->has())
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
#endif

The lumen documentation states that Lumen does not have the $error variable for views and that you should catch the error $this->validate throws. This way you can add the variables to your view manualy.

Related

Laravel validation error not showing in view, only showing in 422 post error

I am have trouble figuring out why laravel validation error message are not showing in my current blade view file when I try to send a post request without an title input.
I would get a 422 post error.
upload.blade.php
#if (count($errors) > 0)
<div class = "alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<input type="text" name="title" id="title" placeholder="enter post title" />
uploadcontroller.php
public function store(Request $request)
{
$photos = $request->file('file');
$title = $request->input('title');
$this->validate($request, [
'title' => 'required|max:120',
]);
}
When I inspect the 422 POST error, only then it shows the error message
If you are using a standard form, you need to return back to the view in order to see the error. Something like:
return \Redirect::back() // can send errors, or with() or whatever
If you are using ajax to send your info, you'll need to handle the response via the success or error method, depending on how you wish to handle the error. Your store() method would then send some kind of text back to ajax in this case.
Bottom line, you need to return something from your store() method to get back to the view you were on, else you won't see anything unless you inspect it.

Is there anything available to display error code in luman laravel while file uploading to validated the file size and file type?

Am new for luman laravel framework and i worked with file uploading concept with file type and file size validation.
$this->validate($request, [
'profile_picture' => 'required|mimes:jpeg,png,jpg,gif,svg|max:100',
'pancard_image' => 'required|mimes:pdf,jpeg,png,jpg|max:100',
'national_id_image' => 'required|mimes:pdf,jpeg,png,jpg|max:100',
'income_proof_image' => 'required|mimes:pdf,jpeg,png,jpg,doc,docs,xlsx|max:100',
'address_proof_image' => 'required|mimes:pdf,jpeg,png,jpg|max:100'
]);
Am using the validation with above code and my question is how can i display error message with error code like core php file uploading concept.suggestions welcome.
in the controller
$validator = Validator::make($data, $rules);
if ($validator->fails())
return Redirect::back()->withInput()->withErrors($validator);
in the blade:
#foreach ($errors->all() as $message)
<li>{{ $message }}</li>
#endforeach

how to fix Undefined variable in Laravel 5.2

I need update some record and this is form action in my blade
<form class="form-vertical" role="form" method="post" action="/projects/{{ $project->id }}/collaborators/{{ $collaborator->user()->first()->id }}">
My controller
public function update(Request $request, $projectId, $collaboratorId)
{
$this->validate($request, [
'status' => 'required',
]);
DB::table('permissions')
->where('project_id', $projectId)
->where('collaborator_id', $collaboratorId)
->update(['status' => $request->input('status')]);
return redirect()->back()->with('info','Your Permission has been updated successfully');
}
routes
Route::put('projects/{projects}/collaborators/{id}',['uses'=>'ProjectCollaboratorsController#update',]);
when click update button generate following error
Undefined variable: project (View: C:\Users\Nalaka\Desktop\c\resources\views\collaborators\permissionedit.blade.php)
how to fix this?
With your route Route::put('projects/{projects}/collaborators/{id}',['uses'=>'ProjectCollaboratorsController#update',]); you need to send a PUT request. Your form send a POST request.
Change your route by Route::post or use some javascript to send a PUT request with ajax.
Try that and let's see if there are other error after.
Also, if there are "undefined variable", show the code around this variable.

Laravel 5: not able to use pagination, I get error "Call to undefined method Illuminate\Database\Eloquent\Collection::render()"

I am quite new to Laravel and started to play around with L5 and tried to paginate my data (from my HomeController method):
someMethod(){
...
$messages = Message::paginate(50)->sortByDesc('timestamp');
return view('home', ['messages' => $messages]);
}
in my home.blade view:
...
<div class="list-group">
#foreach($messages as $message)
#if(!$message->processed)
...
#endif
#endforeach
</div>
{!! $messages->render() !!}
...
I get the error below:
FatalErrorException in b706d42af4b0adc08aee8abb2fdf4ba9 line 105:
Call to undefined method Illuminate\Database\Eloquent\Collection::render()
in b706d42af4b0adc08aee8abb2fdf4ba9 line 105
I followed the logic from the Pagination doc page : https://laravel.com/docs/5.1/pagination#basic-usage and https://laravel.com/docs/5.1/pagination#displaying-results-in-a-view
You have an error in your code. Try this:
$messages = Message::orderBy('timestamp', 'desc')->paginate(50);

Laravel session Flash Message Not working

Session message is not working i tried this code and many fix available online
Here id my store function `
public function store(Request $request)
{
// dd($request->all());
$this->validate($request, [
'name' =>'required',
'username' =>'required',
'email' =>'required',
'address' =>'required',
'likes' =>'required',
'gender' =>'required'
]);
$input = $request->all();
Contacts::create($input);
Session::put('flash_message', 'Task successfully added!');
return redirect()->back();
}
And Retrieving by this code
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
I resolved issue with laravel 6.x
As soon as I moved \Illuminate\Session\Middleware\StartSession::class and \Illuminate\View\Middleware\ShareErrorsFromSession::class from web $middlewareGroups to $middleware in app\Http\Kernel.php everything started working as expected.
I Resolved the Issue with laravel 5.2.
I was having route like this
Route::group(['middleware' => [ 'web','auth']], function () {
.......
}
So Removed the web middle ware
Route::group(['middleware' => ['auth']], function () {
.......
}
and its start working
Analysis: By default Laravel Add web Middleware.
check by php artisan route:list it shows web, web,auth .
so by defining it again redirect two time for the web middleware.
I RESOLVED the issue with laravel 5.2.
I was having all the routes inside this:
Route::group(['middleware' => 'web'], function() {
I remove it because when I used the command php artisan route:list in the Name Middleware column the "web" shows to times: web, web.
If you have AUTH replace by:
Route::group(['middleware' => 'auth'], function() {
Also I delete a duplicate route (in routes.php). Now I just have:
Route::resource('/publicaciones', 'PublicacionesController');
My controller:
return redirect()->back()->with('success', 'Saved!');
My view:
#if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
#endif
have you include the following namespace
use Session;
instead of the following code
Session::put('flash_message', 'Task successfully added!');
use
Session::flash('flash_message', 'Task successfully added!');
in instead to
return redirect()->back();
try using
return redirect()->route('your route');
When the validation fails, no further code is executed and the previous page is loaded again. This is the reason why session message is not working. In order to check for any validation errors use the following code snippet at the top of your blade file.
#if ($errors->any())
#foreach ($errors->all() as $error)
<div class="alert alert-danger alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $error }}</strong>
</div>
#endforeach
#endif
A bit late for this forum. I encounter this problem, I've been different sites searching for the right solution but none works. Here's my case, I'm using v6.0, and I put the route inside routes\api.php.
I think there is difference of putting the route to the right place or file, can't explain more.
Here's how I solved, I transfer the route from routes\api.php to routes\web.php and thats it after so many researching I now successfully display the flash message.
Try this code
Session::flash('flash_message', 'Task successfully added!');
Session::save();
return redirect()->back();
This worked for me
This will work in case "Session" fails to display errors in your blade view.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

Categories