Appears on
Articles715
Laravel 5.1: Names for route groups
A small new feature in Laravel 5.1 - just for those who want to group their routes a little more properly. Now we can add a prefix not only to routes, but to the route groups as well.
Change date format with Accessors and Mutators
Pretty common situation - you have a date field in the MySQL database, which has a default format of YYYY-MM-DD, but the users need to see/edit dates in MM/DD/YYYY. How do you handle it properly in Laravel?
How to deal with possible empty Input variable?
Laravel is pretty strict about any kind of errors - if you try to use undefined variable or don't pass a necessary parameter you immediately see Whoops or another kind of error, depending how you handle them. There's a little trick with Input variables that might save you some time and lines of code.
How to configure PHPStorm for PSR-2
Just a quick tip - since Laravel 5.1 now is coming with PSR-2 support (most important change is spaces indent instead of tabs), it makes sense to configure your editor to support that by default, if you haven't done so already.
AND-OR-AND + brackets with Eloquent
Eloquent is a great thing - you can build your query step-by-step and then call get() method. But sometimes it gets a little tricky for more complicated queries - for example, if you have multiple AND-OR conditions and you want to put brackets, how to do it properly?
All about Redirects in Laravel 5
Laravel has a useful function redirect() to, well, redirect a user to a different page or action, with or without data. Let's discuss those various options in one place - maybe you will find out something new for yourself.
Don't forget the fillables!
Just a quick tip. I've noticed a really typical mistake by developers, which caused some headache for myself and for others - you add a new field into the database table, but for some reason it isn't automatically saved.
How to customize "artisan down" page?
Artisan has a convenient command artisan down for an occasion when you need to put your web-project down for a while - for the case of deployment, maintenance or anything like that. Question - how to customize that temporary error page?
Laravel 5.1: empty option in Form::select with lists()
There is one small upgrade in Laravel 5.1 which breaks apps with earlier versions - it is related to dropdown items. For a long time, Eloquent had a useful way of passing options to Form::select() - but adding an empty parameter doesn't work in Laravel 5.1 anymore. Here's what to do.
Did you know: affected rows after Eloquent update
Another quick tip - if you need to know, how many rows were actually affected if you launch Eloquent update() statement - it cannot be easier.
belongsTo() and withTrashed() - linking to deleted row
Let's say we have DB table products, which is linked to table categories with a field products.category_id = categories.id, Eloquent helps us to define a relation easily. But what if the category gets (soft) deleted, but we still need to have that relationship to be returned for history reasons? You can use withTrashed() method here.
There's no PUT/PATCH/DELETE method, or how to build a Laravel form manually
Laravel RESTful controller mechanism is a very convenient thing - we generate Controller, write {{ Form::xxxx() }} methods in Blade templates and it works like magic. But there's a little trick there which I want to talk about - it's PUT/PATCH/DELETE methods for updating the database entries.
CRUD: How to avoid building whole Form for Delete button
There is one annoying thing in Laravel RESTful approach - if you want to have Edit/Delete links in your tables, Edit is done easy with a link to URL, but for Delete you have to build the whole form. Is there a way to avoid it?
Why use $appends with Accessors in Eloquent?
Eloquent has a convenient feature called Accessors - you can define your own custom fields on top of existing in the database table. But then there's an Eloquent property $appends - should we use it or not? And what's the difference?
Did you know about view()->exists() function?
When you load a View from the Controller, there could be a situation when you don't actually know which view to load - the name could be dynamic. For example, you would use different Blade templates for sending emails. There is a neat function to assure your chosen View file actually exists.
How to customize error messages in Request Validation?
Laravel 5 has an awesome new function - Request Validation. It separates the logic of Validation into kind of a separate layer - Requests, which reside in the folder app/Http/Requests. After the form validation, it auto-magically shows error messages. But what if we want to customize them?
Eloquent: filter only rows which has related "children" rows
Simple use-case: you want to filter only those categories which have at least one product. Or course, you write Category::with('products')->... but how do you filter out those empty categories? Those with no product? There's an app function for that: has().
Schema Builder: changing table columns (only Laravel 5.0+)
While working with database migrations and schema builder, sometimes we do need to make changes to the columns that already exist. Can you do that and how?
Eloquent: How to make lists() work for Accessor fields?
A very convenient way to populate dropdown options with Eloquent is to use lists() function. But it doesn't work out of the box with "Append" fields, also called "Accessors". Here's a small trick to make it work.
Eloquent: incrementing columns without update() function
Eloquent mechanism isn't limited to just create/update/delete functions - that's why it's awesome. One of those helpers come to rescue when you need to increment a column, basically run update X set Y=Y+1 where id = Z - apparently, there's no need to run update() function for that.
