Things I learned from reading Laravel: Up and running
Matt Stauffer is currently putting the final touches on his new book called Laravel: Up And Running. It aims to be a good guide for newcomers to the framework. But even if you've got some experience with Laravel, it should be worth your time to read it. Even Matt himself picked up a lot of cool stuff while writing it.
During my holiday I read an advance copy of the book containing all chapters expect the ones that touch on Laravel 5.3 specific functionality. I can say that it is very well written and it complements the official documentation well. Here are some of the things I've learned from reading this book.
Changing the route key for an Eloquent model
Consider this route:
Route::get('/newsItem/{newsItem}', 'NewsItemController@index');
By default when using route model binding, Laravel will use the id
of a model to fetch it. So a url like /newsItem/1
will work. Did you know the used route key can be changed by adding this code to the newsItem
model?
public function getRouteKeyName() {
return 'slug';
}
Given that the slug
column of one of your records contains my-slug
this url will work: /newsItem/my-slug
.
Sending a download response
Laravel makes it easy to respond with a download. You can even set the name the user will see in the browser.
Response::download($pathToFile, $nameOfDownload);
Using a default value in a Blade view
Whenever you use a variable in a Blade and you want to set a default value in case the variable is not set you can use or
.
//inside a blade view
{{ $myVariable or "This variable was not set" }}
Sharing a variable with all Blade views
You can share a variable globally in all Blade views with share
.
view()->share('currentUser', auth()->user());
You could put this code in a service provider.
Using closure based commands.
Instead of creating a separate class for an Artisan command you could define it using a closure inside the commands
method of App/Console/Kernel.php
// App/Console/Kernel.php
protected function commands()
{
$this->command(
'command:run {myArgument} {--myOption}',
function ($myArgument, $myOption) {
// do something useful here...
});
}
Getting records in a random order
The query builder has a method inRandomOrder()
to sort the retrieve models in a random order.
$randomUser = DB::table('users')
->inRandomOrder()
->first();
Getting a single value from a record
If you just need a single value from the first row you can use the value
method.
$newestNewsItemTitle = DB::table('news_items')
->orderBy('created_at', 'desc')
->value('title');
Adding a uuid column
Inside migrations you can use the uuid
method to add a column that'll hold a uuid.
//inside a migration
$table->uuid('id');
For MySQL this will create a VARCHAR(36)
column.
Eager load the number of related records
If you only need the count of the number of some related records you can use withCount
.
$newsItems = NewsItem::withCount('comments')->get();
These are just a few of the bits I picked up from reading the book. I can highly recommend the book to anyone using the framework (That's my honest opinion, I'm not being paid to say this :-)). I don't know on which date the book will be released publicly, but I guess it's rather soonish. Keep an eye on the official site of the book.
What are your thoughts on "Things I learned from reading Laravel: Up and running"?