2. Directory Changes
• Route folder (api - web - console )
• Assets have a js Folder
• Folder events, policies , console and listeners was removed
• Web file now under middleware web
• Api file under prefix api and middleware api
3. New Query Builder
Now DB class return with collection rather than array
Example :
DB::table(‘user ’)->get() ;
4. New Global Cache Method
Like session(), the cache() global helper can perform three primary functions: get, put, or return an instance
of the backing service
For example:
● cache('abc', null) gets the cached value of abc, or an optional fallback of null
● cache(['abc' => 'def'], 5) sets the value of abc to def, for the duration of 5 minutes
● cache() returns an instance of the CacheManager
5. New Custom Pagination View
How it works:
• Just run
php artisan vendor:publish
• Edit resources/views/vendor/pagination/default.blade.php
6. New Mailble Class
To create new Class:
• Run command
php artisan make:mail ClassName
• Any variable defined as a public property will be available to the view
Example:
php artisan make:mail Reminder
Mail::to($user)->send(new Reminder);
7. <?php
namespace AppMail;
use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
class Reminder extends Mailable
{
use Queueable, SerializesModels;
public function __construct(){ }
public function build(){
return $this->from('hello@app.com', 'Your Application')
->subject('Your Reminder!')
->view('emails.reminder');
}
}
8. New $loop Variable at Every Loop
The $loop variable is a stdClass object that provides meta information about the loop you're currently
inside. Take a look at the properties it exposes:
● index: the 0-based index of the current item in the loop; 0 would mean "first item"
● iteration: the 1-based index of the current item in the loop; 1 would mean "first item"
● remaining: how many items remain in the loop; if current item is first of three, would return 2
● count: the count of items in the loop
● first: boolean; whether this is the first item in the loop
● last: boolean; whether this is the last item in the loop
● depth: integer; how many "levels" deep this loop is; returns 1 for a loop, 2 for a loop within a loop, etc.
● parent: if this loop is within another @foreach loop, returns a reference to the $loop variable for the parent loop item;
otherwise returns null
9. @foreach ($users as $user)
@if ($loop->first)
This is the first iteration.
@endif
<p>This is user {{ $user->id }}</p>
@if ($loop->last)
This is the last iteration.
@endif
@endforeach
10. Many to Many Relationships has New Toggle Method
• It gives us the ability to attache item in table or remove it if it exists
• toggle() method on all belongsToMany relationships.
• This solves the issue of needing to delete a pivot table record if it exists, or adding it if it doesn’t.
• As an example, consider needing to toggle a user’s "like" status for a post
11. New JSON-Column where() and update() Syntax
Let's assume we have a table with a JSON column:
class CreateContactsTable extends Migration
{
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->json('meta');
$table->timestamps();
});
}
12. We could imagine one contact (output to JSON for blog-post-readability) might look like this:
{"id": 1,
"name": "Alphonse",
"meta": {
"wants_newsletter": true,
"favorite_color": "red"
}}
So, let's get all of our users whose favorite color is red. As you can see below, we start with the column
(meta), followed by an arrow (->), followed by the key name of the JSON property (favorite_color).
$redLovers = DB::table('users')
->where('meta->favorite_color', 'red')
->get();
DB::table('users')
->where('id', 1)
->update(['meta->wants_newsletter' => false]);
13. Advanced Operations with Collection::where
If you want to filter a Laravel collection to only those records which meet particular criteria, you're most
likely going to reach for filter() or reject(). For a quick refresh, this is how you might use both:
$vips = $people->filter(function ($person) {
return $person->status === 'vip';
});
$nonVips = $people->reject(function ($person) {
return $person->status === 'vip';
});