Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel small standalone one-off script without artisan command?

Tags:

I need to check something small in Laravel, so I just want to make a small script to check it.

I know that I can do it with

php artisan make:console ...

But It will add a file to the App/Console/Command, and I will need to update app/Console/Kernel.php. It means that I will have do commit it to source control, which is really not needed.

Is there a way to have a standalone laravel script which will give me access to the Laravel Components?

I am using Laravel 5.2, (make:command doesn't exists, only make:console)

Just an example for what I tried:

<?php

use App\User;
use DB;

require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/..//bootstrap/app.php';

echo "hello world\n";
$res=User::where('id',5)->first();
echo "end!\n";
?>

But I am getting an error:

PHP Fatal error:  Uncaught Error: Call to a member function connection() on null in /var/www/html/dpriceit/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:3314
Stack trace:
#0 /var/www/html/dpriceit/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3280): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#1 /var/www/html/dpriceit/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1880): Illuminate\Database\Eloquent\Model->getConnection()
#2 /var/www/html/dpriceit/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1853): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#3 /var/www/html/dpriceit/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1823): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#4 /var/www/html/dpriceit/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3524): Illuminate\Database\Eloquent\Model->newQuery()

UPDATE

I tried creating a console command

php artisan make:console MyTempTest

But when I do php artisan list I don't see its signature in the list of available commands.

like image 690
justadev Avatar asked Sep 09 '19 15:09

justadev


People also ask

How do I stop artisan command in Laravel?

Press Ctrl + Shift + ESC. Locate the php process running artisan and kill it with right click -> kill process. Reopen the command-line and start back the server. Note that you should be able to kill the process just by sending it a kill signal with Ctrl + C.

Is there any CLI for Laravel?

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application.

How do I exit php artisan serve in CMD?

Simply use Ctrl + C . It will come out to prompt state.


1 Answers

To fix the error you're getting, boot up the application's kernel and handle the response like so

app\script.php

<?php

use App\User;

require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

echo "hello world\n";
$res = User::find(5)->name;
var_dump($res);
echo "end!\n";

Then from a terminal, run php app/script.php
Result:

~/Sites/laravel (master ✗) ✹ ★ ᐅ  php app/script.php 
hello world
string(11) "Khalid Bins"
end!
like image 133
Salim Djerbouh Avatar answered Sep 30 '22 07:09

Salim Djerbouh