douzhixun8393 2018-06-18 20:14
浏览 97

Laravel Eloquent:在null上调用成员函数connection()

In Laravel I have a Controller like this:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\Dienstreise;

class DienstreiseController extends Controller
{
  public static function store()
    {
     $dienstreise = new Dienstreise;
     $dienstreise->user_id=1;
     $dienstreise->start_ort = "Hamburg";
     $dienstreise->ziel_ort = "Kassel";
     $dienstreise->save();
     return "OK";
  }
}

I have a route for that function, that works totally fine. (So the database-settings are 100% correct)

Route::get('/dienstreise', 'DienstreiseController@store');

But now I have a file in a folder in my public-folder, which needs to access that function in the controller too. (I know that sounds strange, but the file will get a POST Request from Dialogflow and so the file must be located in the public folder, otherwise they can't access it)

The file looks like this:

<?php
require __DIR__ . '/../../vendor/autoload.php';

use App\Http\Controllers\DienstreiseController;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\Dienstreise;

$dr = DienstreiseController::store();
echo $dr;
?>

But I get a 500-HTTP-Error? The log:

PHP Fatal error: Uncaught Error: Call to a member function connection() on null in /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1140

If I comment all the lines with $dienstreise... in the controller, I get the "OK", so the mistake must be there... But why does it work with the route? I'm confused. I read about uncommenting "$app->withEloquent();" in bootstrap/app.php, but there is nothing like this commented?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?