Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

air_petr's avatar

Use tinker with code from file

Hi. Is there a way to pipe code from file to tinker? Something like this:

php artisan tinker < my-code.php
0 likes
12 replies
Braunson's avatar

Tinker isn't for running full files of code. Instead it's meant to be able to run a few lines at a time to interact with the application without having to spin up a local server.

If your code is a class you want to interact with, include it in your app (e.g. Autoload it in) and in Tinker you can call the class and do what you need there.

tldr:: Tinker is a tool to help interact with the app without needing to spin up a server. It's a simple feature you can use to test a couple of lines you'd normally delete from your project.

Cronix's avatar

You can run a php script from the cli though, just not tinker. Different tools. php -f my-code.php

air_petr's avatar

Yes, but it won't has Laravel instance (to use Models for example). Maybe there is a way to call Laravel inside my-code.php...

36864's avatar
36864
Best Answer
Level 13

You can pipe a file as input to tinker and it will happily accept it. On windows, more test.php | php artisan tinker works just fine.

//test.php

$a = 'Hello';
$b = 'World';
implode(' ', [$a, $b]);

(note there is no php opening tag on the file)

D:\projects\test>more test.php | php artisan tinker
Psy Shell v0.9.6 (PHP 7.2.1 — cli) by Justin Hileman
=> "Hello"
=> "World"
=> "Hello World"

Exit:  Ctrl+D
5 likes
mhmelshaaer's avatar

You can easily create any PHP file and run it inside tinker. Also, you can use models, helper methods, and classes in this file since it will run inside tinker.

For example:

// some-file.php

<?php

$user = \App\User::find(3);
$user->name = "New Name";
$user->save();

And then, run tinker and include this file providing the absolute path to it, like


>>>include('/home/myPCname/Desktop/some-file.php')
=> 1

Press enter and this user will be updated successfully. You can define a function also and call it whether inside the file itself or from tinker, like so:

// another-file.php

<?php
function foo($id) {
    return \App\User::findOrFail($id);
}

// Can be called here
foo(3);

Run tinker

>>> include('/home/myPCname/Desktop/another-file.php')
=> App\User {#3180
     id: 3,
     name: "name",
     created_at: "2020-06-10 13:05:20",
     updated_at: "2020-06-10 13:05:20",
   }

Or inside tinker

>>> include('/home/myPCname/Desktop/another-file.php')
=> 1
>>>foo(3)
=> App\User {#3180
     id: 3,
     name: "name",
     created_at: "2020-06-10 13:05:20",
     updated_at: "2020-06-10 13:05:20",
   }

4 likes
minasrc's avatar

@mhmelshaaer This is exactly what I was looking for, thank you! Now I can have a file that initializes all of my controllers and objects without having to enter them line by line.

cameronwilby's avatar

@minasrc Thanks! It shortens to: php artisan tinker --execute="include('tinker.php')" without aliases.

shofmann's avatar

I came across this thread and trying on Mac to load file to work with within tinker. When I try ./app/filename.php | php artisan tinker it returns zsh: permission denied: ./app/filename.php. It tried prefixing with sudo and entering password a get back sudo: ./app/filename.php: command not found. Not sure what I'm doing incorrectly and looking for any guidance.

vixtecnologia's avatar

If you just want run some piece of code, you can try autoload some function and execute it inside tinker. I think it's way better than just pipe every line inside tinker. "autoload-dev": { "files": { 'app/Helpers/test.php }, },

// test.php function run_test () { // Code Here } composer dump-autoload php artisan tinker

run_test()

mominsid's avatar

I just ran into this now. My favorite version of this is php artisan tinker --execute 'include("path/to/script.php");';

1 like

Please or to participate in this conversation.