What's the meaning of $errors in Laravel - php

I am new to Laravel and I wanted to add error validation messages to view and I wonder what is $errors variable and how it works behind the scenes. According my research the variable is an instance of Illuminate\Support\MessageBag but cannot find any further explanation and details about it. Please can you explain or at least give reference to websites that explain it.

According to the documentation,
Note: The $errors variable is available in every Laravel view where the web middleware is applied. It will simply be an empty instance of ViewErrorBag if no validation errors are present.
Also note that before now, we had to explicitly define it by calling Session::get('errors')->all().
Read this for more info:
https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors
Also, look for these files in your project for more insight.
/vendor/laravel/framework/src/Illuminate/Http/RedirectResponse.php - withErrors() method.
/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php - handle() method.
Note: I am using Laravel 5.6 as at the time of writing.

$errors in Laravel comes from validation, to further understand about valiation, kindly refer to documentation https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors.
To know how it show on every blade, kindly check your app/Http/Kernel.php, there is middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class and refer to this middleware, you will see these lines:
$this->view->share(
'errors', $request->session()->get('errors') ?: new ViewErrorBag
);
Hope this might help.

Related

how to use DD() method in Laravel projects?

I know that for some it might be stupid or funny question (but I am newbie) but I need to find know how to properly use DD() method in laravel projects.
For example - I have got tasks to debug some code and functionality in my project (PHP laravel). And it always takes me for ever to find the exact file or folder or code where the problem is.
My mentor says to use DD() method to find things faster (but for learning purposes he didn't explain me a lot about how to actually use it and said to find out my self), but said that I should start with Route (we use backpack as well for our project). So after finding Route (custom.php file) which controller connects to my required route what should I do next? How do I implement dd() method (or as my mentor says dd('call here') method) to fast find what I should be looking for to solve my problem and complete my task? Where should I write this dd() and how should I write it?
Thank you for the answer in advance!
for example I have a:
public function create(): View
{
return view('xxxxxx. \[
//
//
\]);
}
and if I put dd() anywhere in the code, I get error message in my URL :(
first of all ,in Laravel we use dd() before return in order to read any variable.
in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var).
notice:
if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).
dd stands for "Dump and Die."
Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.
Example:
dd($users,$variable1,$var2);
You can use dd() in blade
#foreach($users as $user)
#dd($user)
OR
{{dd($user)}}
#endforeach
#dd($var1)
You can read this article, the have more example and comparison
https://shouts.dev/articles/laravel-dd-vs-dump-vs-vardump-vs-printr-with-example
As Laravel is following model-view-controller or MVC design pattern. First go to the route and check which controller is called in the URL with the related URL.
Then go to the controller. **dd**() function is basically a dump and die. you also can do this by **print** or **echo** function too.
Lets assume that I have a controller name ProductController where I have method name index.From where I need to show a list of products in a table.
// in controller
public function index()
{
$products = Products::all();
// here you think ,I need to check whether I am getting the output or
not.
dd( $products );
//Or echo $products;
return view ('product.list',compact('products'));
}
let's suppose you are getting everything but in view when you loop through the products you declare the wrong variable name or mistakenly do some spelling mistakes. and want to see the result.
In view just do the dd() method by the following way:
{{ dd($products) }}

Laravel Storage::files() showing dotfiles when it shouldn't

I am trying to get a list of all the files in a folder using Laravel's Storage::files('folder') method, but it returns all the files, including .gitkeep
I want to find a way to exclude the dotFiles, and I know that the Storage::files() method has a secondary param that should do exactly that, because it is set by default, here:
public function files($directory, $hidden = false)
{
return iterator_to_array(
Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->depth(0)->sortByName(),
false
);
}
...but it doesn't seem to for for me!
Did anyone had this issue and can help a man out?
Thanks in advance!
By doing Storage::files(), you are not actually calling the method you mentioned.
Storage::files() calls Illuminate\Filesystem\FilesystemAdapter::files() instead. This confused me a lot before too.
What you want to do can actually be done using the File facade instead. This actually calls the method you mentioned.
File::files($path);

Controller check requirements to process action

I'm trying to figure out if there is possibility to check requirements before processing controller action. In Nette there are methods like checkRequirements, onStartup, beforeRender where I can check this.
I have api resource album/{albumId}/song/ and I would like to check if album with given id exists every time any action on my SongController is processed and return 404 status code if not.
So far I have found this article in Symfony documentation where I found there are no methods like preExecute and postExecute. However I guess there is bundle or something like that to add those methods. I think it does not make sense to create new class to use it only in one controller.
Are there any other options to do it?
ParamConverter does that. It looks for a entity using the id supplied from the route and throws an exception, returning a 404 if it doesn't find anything.
// paramConverter requires that you type-hint a class, which is a best practice anyway :)
public function getArtist(\Appbundle\Entity\Song $song)
{
//...
}

Using $input->all() instead of Input::all() Laravel-5

I'm trying to use $input->all() as opposed to Input::all() in Laravel-5, however it doesn't seem to like it, even though I am passing the Input reference to the function, like so:
/**
* Search for a specified resource.
*
* #return Response
*/
public function search(Booking $booking, Input $input)
{
dd($input->all()); // this doesn't work
dd(Input::all()); // this DOES work
}
The error I get is:
Call to undefined method Illuminate\Support\Facades\Input::all()
Does anyone have a solution to this problem?
I don't think you're supposed to inject Facades into your Controllers. Input is a facade for Illuminate\Http\Request and it's service container binding is request. So according to the documentation, in Laravel 5 you can do Request::all() and in Laravel 5.1 you can do $request->all()
http://laravel.com/docs/5.0/requests#retrieving-input
http://laravel.com/docs/5.1/requests#retrieving-input
EDIT: This post gives some more in-depth information: https://stackoverflow.com/a/29961400/2433843
EDIT3: I think it would be great if someone could explain WHY exactly you can't inject Facades into your Controllers. I understand DI and Facades are two different things entirely, and L5+ is pushing the developers towards DI. I just don't exactly understand why injecting a facade wouldn't work, since it points towards another class, and it works when you do not inject it. Not to forget Facades and Aliases are two seperate things too. I hope someone can elaborate on this.
One more important thing about using Request or Input to access the User Input is the version of Laravel that you are using.
In the Laravel 4.2 and prior, you could have access the Input::all(), Input::get() but from Laravel 5 onwards, it's been suggested to use the Input via Request facade
Ref: https://laravel.com/docs/5.2/requests
In case if you want to make use of Input in Laravel 5.0 and onwards, then you need to add this facade in the config/app.php file under the aliases section as 'Input' => Illuminate\Support\Facades\Input::class
Once you add the facade under alias, you should start using the 'Input::all()'
Hope this helps some others, who are having the confusion as whether to use 'Input' or 'Request' for Laravel 5.0 onwards.

Adding a field specific error from the controller in symfony2

I have some complex validation going on with my symfony form, and I need to be able to assign an error to a specific field from my controller. Right now, I have global errors working like this:
$error = new formerror("There is an error with the form");
$form->addError($error);
But that creates a global error, not one bound to a specific field.
Is there a way to throw an error on a specific field from my controller?
Thanks to some help over IRC (thanks #fkrauthan!) I came up with an answer.
Every field in SF2 is actually an instance of form. What you need to do is access the form object of the field, and add then error onto it. Thankfully, symfony provides a method to get an embedded form/field.
Heres my code:
$error = new FormError("There is an error with the field");
$form->get('field')->addError($error);
As some people have pointed out, you will need to include the FormError class at the top of your file:
use Symfony\Component\Form\FormError;

Categories