Tagged: laravel-5

Remove duplicate after union two collections in Laravel 5

Problem: Having two collections/MySQL query results and combine them using union():

$result1->union($result2);

then want to remove all duplicate on that union result.

Solution: using unique() to remove duplicates. You can add a parameter like unique(‘id’) to specifically remove which tag name. Example:

$result1 = $result1->unique()->values()->all();

Remember to assign it to a variable or else it will remain the same.

Change localhost to correct domain path in Laravel

Problem: when resetting password or go to other routes, the path should no longer be a localhost but my new domain name. Still, the domain from URL is localhost.

Update .env file to update Laravel domain name

Solution: Update .env file, from APP_URL=http://localhost to APP_URL=https://yourdomain.com

and remember to clean config setting by enter below command:

php artisan config:clear

Applying version design in Laravel

Problem: Current Laravel need to serve different companies and have different stage(production, test, etc.)

Using Route::group and create a different folder for different website/stage.

Solution: follow post on this and this:

  1. Create folders for a different purpose and put controller you want inside it
/app
  /controllers
    /Api
      /v1
        /UserController.php
      /v2
        /UserController.php

In the UserController.php, remember to change namespace accordingly.

For example for controller under v1 folder

namespace Api\v1;

for controller under v2 folder

namespace Api\v2;

In the web.php, using Route::group to determine using which controller, like:

Route::group(['prefix' => 'api/v1'], function () {
  Route::get('user',      'Api\v1\[email protected]');
  Route::get('user/{id}', 'Api\v1\[email protected]');
});

Route::group(['prefix' => 'api/v2'], function () {
  Route::get('user',      'Api\v2\[email protected]');
  Route::get('user/{id}', 'Api\v2\[email protected]');
});

for any prefix like api/v1, using the controller under v1 folder.

Route::group(['domain' => 'itdailydocumentation.com'], function () {
  Route::get('user',      'Api\v3\[email protected]');
  Route::get('user/{id}', 'Api\v3\[email protected]');
});

for domain itdailydocumentation.com, using controller under v3 folder.

As for the version design for layout, you can also copy the view blade to a different folder:

views/
├── v1/
│   ├── master
|   |   ├── master-public.blade.php
|   |   ├── master-admin.blade.php
|   |   ├── master-user.blade.php
|   ├── views
|   |   ├── home
|   |   ├── chat
|   |   ├── order
|   |   ├── reports
├── v2/
└── v3/

Laravel does not load asset via HTTPS

Problem: After turn on HTTPS, Laravel load the assets via HTTP and it cause Google Chrome inspector to pop up a message “…was loaded over HTTPS, but requested an insecure…

Although you access the page via HTTPS but HTTPS page is asked to load a resource over HTTP. In general, simply change the setting in order to load assets via HTTPS.

Solution: for Laravel check out this post, below changes work for me.

Find AppServiceProvider.php and change boot()

    public function boot()
    {
        //
        If (env('APP_ENV') != 'local') {
            $this->app['request']->server->set('HTTPS', true);
        }

    }

If you are just for testing (local environment), simply change APP_ENV checking:

If (env('APP_ENV') == 'local') {

Laravel composer update failed with carbon extension

Problem: Run composer update failed with a message “providing support and update for 1.x versions, please upgrade to Carbon 2.”

This is because of carbon version 1 is deprecated, you have to use Carbon 2, but Carbon requires Laravel 5.8

Solution: According to the official Carbon website, you can just update composer.json with below content:

{
    "require": {
        "nesbot/carbon": "2.25.0 as 1.39.0"
        "kylekatarnls/laravel-carbon-2": "^1.0.0"
    }
}

In this case, you can still be using other versions of Laravel

Change Timezone in Laravel 5

Problem: In Laravel, the created_at and updated_at attribute in database value are incorrect when users trying to create and update data.

Solution: Change the timezone value in /config/app.php. The timezone list can be found at https://www.w3schools.com/php/php_ref_timezones.asp

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Taipei',  

419 error in laravel 5.6 when post route for testing

Problem: https://laracasts.com/discuss/channels/laravel/post-request-in-laravel-57-error-419-sorry-your-session-has-expired or just POST data to route. Having Error -- 419 Sorry, your session has expired.

Solution: If user just want to test the route using POST and don’t want to send csrf token, user can an exception for specific route in VerifyCsrfToken

VerifyCsrfToken .php

 
protected $except = [
'foo', // for route 'foo', this route can post without csrf token
'getReplytest', // for route 'getReplytest', this route can post without csrf token
];