To start:
"Model::remember()" is Gone
I had to update a number of repositories to fix this one. Taylor removed the ability to cache a database query, using the remember() method. So, this will no longer work:
Question::remember(1440)->get();
The same is true for the cacheTags method. Instead, just use Cache::remember(), and then wrap your Eloquent query within it.
Pagination
A couple things have changed with pagination. The big one is that, for the page links, $paginator->links() no longer works. Change all those calls to $paginator->render().
Controllers
All controllers in Laravel 5 are namespaced, by default (following PSR-4). This can be a pretty big pain, in terms of upgrading. I decided to go through with the manual process of updating all the classes, and ensuring that everything was imported properly. If you'd rather skip this, you can add the controllers directory to your composer.json file (in the classmap autoload section). Finally, go to your RouteServiceProvider, and set the namespace property to null.
That said, if you have the time, I recommend just updating everything to be namespaced. It's good for consistency.
Configuration
Configuration is pretty different in Laravel 5 (but better). For your Laravel 4 apps, you know how you have, for example, an app/config/local/mail.php file, which handles mail configuration for the local environment, right? Well it's not quite like that anymore. Instead, you'll just have one config/mail.php file, where you'll use environment variables to set the values.
As an example, your config/database.php section might have something like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_NAME', 'forge'),
'username' => env('DB_USER', 'forge'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
env is a little helper function that Laravel provides. It allows you to reference an environment variable, or use a default if one does not exist. Now, you'll create a .env file for each environment, and set these values, like so:
DB_HOST=localhost
DB_NAME=homestead
DB_USER=homestead
DB_PASSWORD=secret
So, for production, you'll manually create a .env file, and set those values to whatever is appropriate for your production db.
Helper Functions
Laravel 5 offers a number of convenient helper functions to handle common tasks, like View::make() or Redirect::to or Event::fire(). For those three examples, you can now use view(), redirect(), and event().
So, you can do a quick search and replace within your controllers directory to update these. So, to clarify:
return View::make('posts.index')
can be updated to:
return view('posts.index');
Next, if you fire any events from your controllers:
Event::fire('UserSignedUp', [$user]);
You can search and replace to:
event('UserSignedUp', [$user]);
And last, but not least (there's a number of them), for redirections, these...
return Redirect::to('articles');
return Redirect::route('articles_path');
return Redirect::back();
return Redirect::home();
...can be updated to:
return redirect('articles');
return redirect()->route('articles_path');
return back();
return redirect()->home();
One nice benefit to this is that you don't have to import all those facades for every namespaced controller.
Form and Html Facades are Gone
Taylor stripped the FormBuilder and HtmlBuilder classes from core. As a result, if you still want to do things, like:
{{ Form::open() }}
{{ Form::close() }}
Then you'll need to:
- Composer require
illuminate/html - Add
Illuminate\Html\HtmlServiceProviderto yourconfig/app.phpsection. - Add facade aliases to that same file at the bottom (
'Form' => 'Illuminate\Html\FormFacade')
Now, you're back to normal.
Blade Braces
On that note, when you load your updated app in the browser, you'll see all the HTML from things like {{ Form::open() }}. Laravel changed how these were treated, to help with security. Everything is escaped by default, now. If you want to override that, then you must use:
{!! Form::open() !!}
{!! Form::close() !!}
For a big app, this could be a very lengthy process, so, temporarily, you can bring back the old Blade defaults (until you have time to update everything). From the Laravel docs:
However, if you must use the old Blade syntax, add the following lines at the bottom of AppServiceProvider@register:
\Blade::setRawTags('{{', '}}');
\Blade::setContentTags('{{{', '}}}');
\Blade::setEscapedContentTags('{{{', '}}}');
Catching Exceptions
In Laravel 4, you might have caught exceptions from your global.php file, like so:
App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception)
{
return Redirect::to('foo')->with('flash_message', 'Had trouble loading that page. Sorry!');
});
You can't do that in version 5. Instead, you can store this sort of logic in app/Exceptions/Handler.php, within the render() method. Let's convert the snippet from above:
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
return redirect('foo')->with(
'flash_message',
'Had trouble loading that page. Sorry!'
);
}
return parent::render($request, $e);
}
Notice how we check the type of the instance. Also, on a related note, if you use a service, like Bugsnag (highly recommended), then you can add the necessary setup to the report method in that file. Maybe something like:
app('bugsnag')->setNotifyReleaseStages(['production']);
Views
Directories have been moved around in Laravel 5. Your views used to be stored in app/views. Move those to resources/views.
Artisan Commands
I had to move around my Artisan commands to get those to work. First, move all the commands from app/commands to the new app/Console/Commands. Don't forget to upgrade the namespaces in the process. Next, rather than registering your commands with something like Artisan::add() or Artisan::resolve(), instead update the $commands array within app/Console/Kernel.php. Yours may look something like:
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
'Laracasts\Console\Commands\ClearHistoryCommand',
'Laracasts\Console\Commands\SignupsReportCommand',
'Laracasts\Console\Commands\WelcomeUserCommand',
];
That should do it!