Laravel Tinker Shell – Access a Laravel controller function through command line

Laravel Tinker Shell is an interactive command like tool that allows you to run a controller function directly via the command line.

Let's see an example below:

Say you have a controller as such: App\Http\Controllers\SomeController

Within it, there's a function call SomeFunction().

Now you want to access SomeFunction() via the Tinker command line:

php artisan tinker

$controller = app()->make('App\Http\Controllers\SomeController');

app()->call([$controller, 'SomeFunction'], []);
// the SomeFunction function is called
// the second param array [] is used to hold the parameters
// leave it empty if there's no parameter.

app()->call([$controller, 'SomeFunction'], [param1 => 321]);
// this is how the parameters are passed in

So you can see the outputs from the command line directly, this is very helpful for testing. However if  you want something permanent you probably may like to make a custom artisan command for long term use.

Laravel Tinker Docs

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.