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?