在null laravel上调用成员函数connection()

时间:2018-01-28 20:48:24

标签: php laravel

我在laravel测试了一些东西,但下面的代码给了我错误,我无法弄清楚问题是什么

代码:

    <?php
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$posts = \App\Models\Post::take(5)->get()->toArray();
file_put_contents('1.txt', print_r($posts, 1));
>

错误:

Fatal error: Uncaught Error: Call to a member function connection() on null in /home/vowave/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:3351 Stack trace: #0 /home/vowave/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3317): Illuminate\Database\Eloquent\Model::resolveConnection(NULL) #1 /home/vowave/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1861): Illuminate\Database\Eloquent\Model->getConnection() #2 /home/vowave/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1834): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder() #3 /home/vowave/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1804): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes() #4 /home/vowave/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3561): Illuminate\Database\Eloquent\Model->newQuery() #5 /home/vowave/public_html/vendor/laravel/framework/sr in /home/vowave/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 3351

1 个答案:

答案 0 :(得分:1)

手动启动laravel时,应调用bootstrap方法,如下所示:

 <?php
 $app = require __DIR__.'/bootstrap/app.php';
 $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
 var_dump(Task::all());