为什么我的Eloquent all()还会回收垃圾?

Hello I have code like this http://pastie.org/10852864 why when I call routes, I got result with trash http://prnt.sc/b8jxhh.

But when I change to this http://pastie.org/10852865, It work's as expected without trash.

But the second one I need to write config(...) everywhere.

The problem here is that you don't run parent constructor so no special trait methods won't be applied - in this case bootSoftDeletes method won't be run.

All you need is to run parent constructor. instead of:

public function __construct()
{
    config(['database.connections.pos.database' => 'mydata']);
}

You should use:

public function __construct()
{
    config(['database.connections.pos.database' => 'mydata']);
    parent::__construct();
}

Let me explain what this line does:

config(['database.connections.pos.database' => 'mydata']);

It will actually go to config/database.php and look for connections array and set the pos.database to be mydata

The array looks like this:

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
    ],

],

As you can see, most of the values are taken from the .env file. You can set this in your .env and you don't have to set it using the lines you put.

The example I had, in this case, assuming I am using MySQL and the username and password as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=freshbook
DB_USERNAME=root
DB_PASSWORD=password

EDITED

So, if you need to have two connections, you can actually create two controllers for two different database.

class MysqlController extends Controller
{
    public function __construct() {
      config(['database.connections.mysql.database' => 'mydata']);
      parent::__construct();


    }

    public function getSomething() {

    }
}

In routes.php

Router::get('/get', function() {

   $user = App\User::find(1);

   if ($user-id == 1) {
      $ctrl = new App\Http\Controllers\MysqlController();

      return $ctrl->getSomething(); 

}

Hope you get the idea