Zak Dyer

Zak Dyer

1567562373

What's New in Laravel 6.0?

Originally published by Holly Lloyd at https://auth0.com

Laravel 6.0 is here!

Taylor Otwell, the creator of Laravel, has been teasing the 6.0 release for a few months now and today it has finally officially dropped. Let's take a look at what's changing and how you can start using it right away.

I plan to show some of the best goodies from Laravel 6.0 in my talk at @LaraconEU. Is there anything else you would like to see me cover on stage? 📣
— Taylor Otwell 🏝 (@taylorotwell) August 26, 2019

What's new?

Semantic versioning

Although the jump from 5.8 to 6.0 is a major release, Otwell has mentioned that in terms of new features and changes, this update will feel more like moving from 5.8 to 5.9.

Laravel has had huge updates with every major version release, which is typical in a young framework. All the updates throughout 5.x laid the groundwork for a stable and robust framework. Going forward with 6.0, you won't have to worry as much about huge breaking changes and rewrites with every new major release. Because of this new stability, the Laravel framework package is officially adopting the more traditional semantic versioning.

Job middleware

Instead of manually adding extra boilerplate code around your jobs, you can now extract it into middleware and then just attach it to the job!

This example shows how you could add rate limiting to a job prior to 6.0. You would add it to the job's handle() method for each job that requires it.

// app/Jobs/JobToBeThrottled.php

// Execute the job

public function handle() {

  // allow one job to process every 5 seconds

  Redis::throttle(‘key’)->block(0)->allow(1)->every(5)->then(function () {

    // Handle the job

  }, function () {

    // Could not obtain lock

    return $this->release(5);

  });

}

With this new release, we can extract that code out of handle() and move it into a job-specific middleware. You can create a directory in app/Jobs/Middleware (or wherever you want, Laravel isn’t opinionated about this) and add a file for the middleware in that folder.

// app/Jobs/Middleware/RateLimited.php

namespace App\Jobs\Middleware;
use Illuminate\Support\Facades\Redis;
 
class RateLimited {
  // Process the queued job.
  public function handle($job, $next) {
    Redis::throttle(‘key’)
      ->block(0)->allow(1)->every(5)
      ->then(function () use ($job, $next) {
        // Lock obtained
        $next($job);
      }, function () use ($job) {
        // Could not obtain lock
        $job->release(5);
      });
  }
}

Now you can attach this middleware to the job and it will run through the middleware before processing. If you’re familiar with route middleware, this process is very similar.

// app/Jobs/JobToBeThrottled.php
use App\Jobs\Middleware\RateLimited;

// get the middleware that the job will use
public function middleware() {
 return [new RateLimited];
}

public function handle() {
 // handle the job
}

This will keep your job files small, easier to read, and focused on their specific task. It will also allow you to easily reuse middleware throughout jobs.

Lazy collections

Laravel 6.0 introduces lazy collections, which will allow you to keep memory usage low when dealing with heavy data.

If you’re working with eloquent models, you can choose to load one model into memory at a time instead of everything at once by using the cursor() method instead of all().

// This loads all eloquent models into memory at the same time
// This may be a very large number if you have thousands of posts
 
$posts = App\Post::all()->filter(function ($post) {
    return $post->id > 500;
});

By switching from all() to cursor(), only one eloquent model is loaded into memory at a time because cursor() uses the new LazyCollection instance.

$posts = App\Post::cursor()->filter(function ($post) {
  return $post->id > 500;
});
 
foreach ($posts as $post) {
  echo $post->id;
}

Eloquent subquery enhancements

Eloquent has always made it easier to run complex queries. This new release gives you even more help when it comes to running subqueries, or a query within a query, in a single call. This is useful when you need to select information from two tables with a relationship. In Laravel 5.x you were sometimes limited with what you could do in a subquery and would often end up being forced to chain a query using DB::raw().

The addSelect method has been added to subqueries, which should eliminate a lot of that pain! Eloquent subqueries will also now have access to orderBy.

For this example, imagine you have 2 tables: hotels and reservations. You want to know the most recent room type that was booked for a specific hotel. Instead of doing two separate eloquent queries, you can now do this:

use App\Reservation;
use App\Hotel;
 
return Hotel::addSelect([‘last_booked_room’ => Reservation::select(‘room_type’)
  ->whereColumn(‘hotel_id’, ‘hotels.id’)
  ->orderBy(‘created_at’, ‘desc’)
  ->latest()
  ->limit(1) 
])->get();

Improved custom authorization responses

If you’re using Laravel’s out-of-the-box authentication with gates for authorization, Laravel 6.0 introduces a new method Gate::inspect. This makes it easier to provide custom messages to users during authorization requests, such as a specific error message if their request was denied.

For example, say you have the method below that determines if a user is authorized to edit a post. Before 6.0, it was difficult to retrieve this message and display it back to the user.

With the introduction of the Gate::inspect() method, you can now easily retrieve the full custom response to send back to the user.

If you want to limit the editing of posts to only users who have the role admin, you may have created a gate definition for editing posts.

// App/Providers/AuthServiceProvider.php

public function boot()
{
   $this->registerPolicies();

   // Define the gate that determines who can edit posts
   Gate::define(‘edit’, function ($user) {
       return $user->isAdmin
         ? Response::allow()
         : Response::deny(‘You must be an administrator to edit posts.’);
   });
}

Normally you would just use the allows or denies method to see if that user should be authorized to perform an action. The response depends on the boolean response from the Gate::define method defined above. The following example will not give you the custom response, just the boolean.

if (Gate::allows(‘edit’)) {
 // let the user edit
}

if (Gate::denies(‘edit’)) {
 // let the user edit
}

If you want to access the more detailed custom denial response, you can now use Gate::inspect.

// get the full authorization response returned by the gate
$response = Gate::inspect(‘edit’);
 
if ($response->allowed()) {
  // Let the user edit the post
} else {
  // Display the denial message
  // ‘You must be an administrator to edit posts.’
  echo $response->message();
}
“Laravel 6.0 is here! Check out what’s new and what’s changing!”

Tweet This

Laravel UI composer package

Another update to be aware of is the extraction of the laravel/ui package. Front-end scaffolding will still exist, it’s just not included by default. This means in a new Laravel 6.0 application, you won’t see any Vue or Bootstrap scaffolding.

The package now includes presets for React, Vue, and Bootstrap. In a tweet back in May, Otwell mentions that he’s considering adding a Tailwind option as well!

I’ve created https://t.co/IPvq4PdlrV to experiment with extracting Laravel’s front-end scaffolding. Give it a look! Welcome any improvements and will probably explore a @tailwindcss option. 🔥 “artisan ui vue —auth” … “artisan ui react —auth”
— Taylor Otwell 🏝 (@taylorotwell) May 31, 2019

If you’d still like it included, you can bring it back in with composer require laravel/ui.

New branding

With every major release, there has also been new branding that comes along with it, and 6.0 is no exception!

In addition to the new logo above, you can also find a fresh new redesign of the Laravel.com website.

Laravel Vapor

And last but certainly not least, the release of Laravel 6.0 also includes the launch of a much-anticipated product in the Laravel ecosystem - Laravel Vapor.

Before this release, most people used Laravel Forge to provision and deploy their apps. With Forge, you could connect your chosen server (Digital Ocean, AWS, etc.) and Forge would provision it specifically for your Laravel application. This is awesome of course, but you still had to manage updates for yourself.

Laravel Vapor does all of this and more. Instead of managing and updating servers for your Laravel application on your own, Vapor is completely serverless!

This doesn’t mean there are no servers involved, it just means you don’t have to deal with them. Another benefit is that you only pay for what you use. Instead of paying a flat monthly rate, you’re only charged when a request is made to your application. This also means you don’t have to worry about scaling, as it will automatically be done for you.

Here are some more awesome features that Vapor provides:

  • On-demand scaling - Can execute jobs immediately as they come in
  • Powered by AWS
  • Ready for bursts of traffic
  • Zero downtime during deployment
  • Extremely fast
  • Multiple environments with free vanity URL for testing (https://snowy-hurricane-12349834324432.vapor.build)
  • Maintenance mode
  • Searchable logs
  • Create and scale your databases from Vapor
  • Backup and restore databases
  • Metrics and monitoring with notifications
  • Purchase domains and manage DNS from Vapor dashboard
  • Automatically added SSL certificates
  • Jobs, workers, PHP updates, and more are handled behind the scenes
  • Deployment easily configured with a simple vapor.yaml file

The cost for all of this is $39 per month or $399 per year with unlimited team members and projects. Keep in mind, you still have to pay for your own AWS costs beyond that.

Upgrading to 6.0

The Laravel documentation estimates it should take about one hour to upgrade from 5.8 to 6.0. Your project may have other dependencies and external packages that can increase that time, so keep that in mind when you decide to upgrade.

How to upgrade to Laravel 6.0

In your composer.json file, change the Laravel framework dependency from 5.8. to ^6.0. If you’re on a version earlier than 5.8, it’s recommended to upgrade up to 5.8 before making the switch to 6.0.

// In composer.json
“laravel/framework”: “^6.0”, 

Then in your terminal run:

composer update

Laravel Shift

Another awesome way to manage your upgrades, especially for bigger applications, is to use Laravel Shift. Shift is an automated tool that will do your upgrade for you.

It works like this:

  • Grant Shift access to your Laravel repository
  • Choose what version of Laravel you’re on and what you want to upgrade to
  • Shift creates a new branch and modifies what needs to be changed in your code
  • Shift creates a pull request so that you can test and decide if it should be merged

The process is very simple and a great option if simply changing the package version in your composer.json didn’t work for you. The service isn’t free, but the one-time fee is very low.

You can find the upgrade instructions for 6.0 here.

Do I need to Upgrade?

Laravel 6.0 is the latest Long Term Support release, which means bug fixes will be guaranteed for two years and security fixes for three years. The previous LTS release with these guarantees was Laravel 5.5.

This table from the Laravel website outlines the current support schedule:

With bug fixes for the previous LTS release ending very soon, it may be worth it to upgrade to 6.0 so that you know your project will be supported for a couple more years.

Aside: Securing Laravel APIs with Auth0

Securing Laravel APIs with Auth0 is very easy and brings a lot of great features to the table. With Auth0, we only have to write a few lines of code to get:

Sign Up for Auth0

You’ll need an Auth0 account to manage authentication. You can sign up for a free account here. Next, set up an Auth0 API.

Set Up an API

Go to APIs in your Auth0 dashboard and click on the “Create API” button. Enter a name for the API. Set the Identifier to a URL(existent or non-existent URL). The Signing Algorithm should be RS256.

Create API on Auth0 dashboard

We’re now ready to implement Auth0 authentication on our Laravel backend API.

Dependencies and Setup

Install the laravel-auth0 package via composer like so:

composer require auth0/login:“~5.0”

Generate the laravel-auth0 package config file like so:

php artisan vendor:publish

After the file is generated, it will be located at config/laravel-auth0.php. Ensure you replace the placeholder values with the authentic values from the Auth0 Admin Dashboard. Double check your values with laravel-auth0.

Your .env file should have the AUTH0DOMAIN, AUTH0CLIENT_ID, AUTH0_CLIENT_SECRET and AUTH0_CALLBACK_URL values like so:

AUTH0_DOMAIN=kabiyesi.auth0.com
AUTH0_CLIENT_ID=xxxxxxxxxxxxxxxxxx
AUTH0_CLIENT_SECRET=xxxxxxxxxxxxxxxxx
AUTH0_AUDIENCE=http://mylaravelapi.com
AUTH0_CALLBACK_URL=null

Activate Provider and Facade

The laravel-auth0 package comes with a provder called LoginServiceProvider. Add this to the list of application providers.

// config/app.php
‘providers’ => array(
    // …
    \Auth0\Login\LoginServiceProvider::class,
);

If you would like to use the Auth0 Facade, add it to the list of aliases.

// config/app.php
‘aliases’ => array(
    // …
    ‘Auth0’ => \Auth0\Login\Facade\Auth0::class,
);

The user information can now be accessed with Auth0::getUser(). Finally, you need to bind a class that provides a user (your app model user) each time the user is logged in or an access_token is decoded. You can use the Auth0UserRepository provided by this package or you can build your own class.

To use Auth0UserRepository, add the following lines to your app’s AppServiceProvider:

// app/Providers/AppServiceProvider.php
public function register()
{
    $this->app->bind(
      \Auth0\Login\Contract\Auth0UserRepository::class,
      \Auth0\Login\Repository\Auth0UserRepository::class
    );
}

Configure Authentication Driver

The laravel-auth0 package comes with an authentication driver called auth0. This driver defines a user structure that wraps the normalized user profile defined by Auth0. It doesn’t actually persist the object but rather simply stores it in the session for future calls.

This is adequate for basic testing or if you don’t have a requirement to persist the user. At any point you can call Auth::check() to determine if there is a user logged in and Auth::user() to retreive the wrapper with the user information.

Configure the driver in config/auth.php to use auth0.

// app/config/auth.php
// …
‘providers’ => [
    ‘users’ => [
        ‘driver’ => ‘auth0’
    ],
],

Secure API Routes

Your API routes are defined in routes/api.php for Laravel 5.3+ apps.

// routes/api.php
 
<?php
 
use Illuminate\Http\Request;
 
/

|--------------------------------------------------------------------------

API Routes
Here is where you can register API routes for your application. These
routes are loaded by the RouteServiceProvider within a group which
is assigned the “api” middleware group. Enjoy building your API!
*/
 
Route::get(‘/public’, function (Request $request) {
  return response()->json([“message” => “Hello from a public endpoint! You don’t need any token to access this URL…Yaaaay!”]);
});
 
Route::get(‘/wakanda’, function (Request $request) {
  return response()->json([“message” => “Access token is valid. Welcome to this private endpoint. You need elevated scopes to access Vibranium.”]);
})->middleware(‘auth:api’);

Now, you can send a request to your protected endpoint which includes an access_token.

curl --request GET \
  --url http://localhost:8000/api/wakanda \
  --header ‘authorization: Bearer <ACCESS TOKEN>’

Once a user hits the api/wakanda endpoint, a valid JWT access_token will be required before the resource can be released. With this in place, private routes can be secured.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook

#laravel #php #web-development

What is GEEK

Buddha Community

What's New in Laravel 6.0?

I am Developer

1599536794

Laravel 8 New Features | Release Notes - Tuts Make

In this post, i will show you what’s new in laravel 8 version.

#What’s new in Laravel 8?

  • 1 - Change Path Of Default Models Directory
  • 2 - Removed Controllers Namespace Prefix
  • 3 - Enhancements on php artisan serve
  • 4 - Enhanced Rate Limiting
  • 5 - Enhanced on Route Caching
  • 6 - Update on Pagination Design
  • 8 - Dynamic Blade Componenets
  • 7 - Update Syntax for Closure Based Event Listeners
  • 8 - Queueable Model Event Listeners
  • 9 - Maintenance mode: secret access
  • 10 - Maintenance mode: pre-rendered page
  • 11 - Queued job batching
  • 12 - Queue backoff()
  • 13 - Laravel Factory

https://www.tutsmake.com/laravel-8-new-features-release-notes/

#laravel 8 features #laravel 8 release date #laravel 8 tutorial #news - laravel 8 new features #what's new in laravel 8 #laravel 8 release notes

Seamus  Quitzon

Seamus Quitzon

1595201363

Php how to delete multiple rows through checkbox using ajax in laravel

First thing, we will need a table and i am creating products table for this example. So run the following query to create table.

CREATE TABLE `products` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Next, we will need to insert some dummy records in this table that will be deleted.

INSERT INTO `products` (`name`, `description`) VALUES

('Test product 1', 'Product description example1'),

('Test product 2', 'Product description example2'),

('Test product 3', 'Product description example3'),

('Test product 4', 'Product description example4'),

('Test product 5', 'Product description example5');

Now we are redy to create a model corresponding to this products table. Here we will create Product model. So let’s create a model file Product.php file under app directory and put the code below.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'name','description'
    ];
}

Step 2: Create Route

Now, in this second step we will create some routes to handle the request for this example. So opeen routes/web.php file and copy the routes as given below.

routes/web.php

Route::get('product', 'ProductController@index');
Route::delete('product/{id}', ['as'=>'product.destroy','uses'=>'ProductController@destroy']);
Route::delete('delete-multiple-product', ['as'=>'product.multiple-delete','uses'=>'ProductController@deleteMultiple']);

#laravel #delete multiple rows in laravel using ajax #laravel ajax delete #laravel ajax multiple checkbox delete #laravel delete multiple rows #laravel delete records using ajax #laravel multiple checkbox delete rows #laravel multiple delete

I am Developer

1597559012

Multiple File Upload in Laravel 7, 6

in this post, i will show you easy steps for multiple file upload in laravel 7, 6.

As well as how to validate file type, size before uploading to database in laravel.

Laravel 7/6 Multiple File Upload

You can easily upload multiple file with validation in laravel application using the following steps:

  1. Download Laravel Fresh New Setup
  2. Setup Database Credentials
  3. Generate Migration & Model For File
  4. Make Route For File uploading
  5. Create File Controller & Methods
  6. Create Multiple File Blade View
  7. Run Development Server

https://www.tutsmake.com/laravel-6-multiple-file-upload-with-validation-example/

#laravel multiple file upload validation #multiple file upload in laravel 7 #multiple file upload in laravel 6 #upload multiple files laravel 7 #upload multiple files in laravel 6 #upload multiple files php laravel

I am Developer

1597727551

Laravel 7 Crud using Datatables

yajra datatables crud with ajax in laravel 7. In this post, i will show you how to create crud using datatable in laravel with ajax and model.

Now, i am going to show you how to install and use datatables in laravel 7 application. i will use jquery ajax crud with modals using datatables js in laravel 7. i will write easy code of jquery ajax request for crud with yajra datatable.

Laravel 7 DataTable CRUD Example

Use the below steps and create yajra DataTables crud with ajax in laravel:

Step 1: Install Laravel App For DataTable Crud
Step 2: Configuration .evn file
Step 3: Run Migration
Step 4: Install Yajra DataTables Package
Step 5: Add Fake Data into Database table
Step 6: Add Datatable Ajax Route
Stpe 7: Create DataTableController
Step 8: Create Ajax Datatable Blade View
Step 9: Start Development Server

https://www.tutsmake.com/laravel-7-6-install-yajra-datatables-example-tutorial/

#laravel 6 yajra datatables #yajra datatables laravel 6 example #laravel-datatables crud #yajra datatables laravel 7 #laravel 7 datatables #yajra datatables laravel

I am Developer

1597565398

Laravel 7/6 Image Validation

In this image validation in laravel 7/6, i will share with you how validate image and image file mime type like like jpeg, png, bmp, gif, svg, or webp before uploading image into database and server folder in laravel app.

https://www.tutsmake.com/image-validation-in-laravel/

#laravel image validation #image validation in laravel 7 #laravel image size validation #laravel image upload #laravel image validation max #laravel 6 image validation