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

davy_yg's avatar
Level 27

php tinker

I wonder what is the function of php artisan tinker?

Can you test each line of laravel using php artisan tinker?

What are the requirements of testable code using tinker? Does it has to be an independent code without any dependency?

0 likes
7 replies
Sinnbeck's avatar

My best advise is try it. Just imagine a controller method where you get output from each line. So you can just call get on a model and see the output at once.

Tray2's avatar

Tinker is just a php shell that has access to your laravel application.

In it you can try your code or tinker with it without making changes in your source code.

For example you can try out Eloquent queries to see if you get the expected result.

You can also test your eloquent relationship.

It's kinda like the developer tool console in your browser where you can run javascript on your page but for php.

1 like
davy_yg's avatar
Level 27

Check this out?

D:\xampp72\htdocs\aws_admin_test>php artisan tinker

Psy Shell v0.9.9 (PHP 7.2.11 — cli) by Justin Hileman
>>> type: "nfs"
=> "nfs"
>>> HomeLatestProjModel::find(1);
PHP Fatal error:  Class 'HomeLatestProjModel' not found in Psy Shell code on line 1
>>> HomeLatestProjModel::find('1');
PHP Fatal error:  Class 'HomeLatestProjModel' not found in Psy Shell code on line 1
>>> HomeLatestProjModel::find('1');
PHP Fatal error:  Class 'HomeLatestProjModel' not found in Psy Shell code on line 1
>>> HomeLatestProjModel::all();
PHP Fatal error:  Class 'HomeLatestProjModel' not found in Psy Shell code on line 1

Why do I get these errors in tinker?

Sinnbeck's avatar

Use the full namespace or use resolve :)

App\HomeLatestProjModel::find(1);

resolve('HomeLatestProjModel')->find(1);
1 like
laracasts2020's avatar

if your Model exists with in a directory then you must to give full path and name

e.g.

in Laravel 8 Models exists within the "Model" Directory so you must have to call like that

App\Models\Post::all();

Regards,

Snapey's avatar

hey @laracasts2020 welcome to the discussions. Please try answering questions that are recent, not 1 year old.

somay55's avatar

in Laravel 8 you need full namespace including: eg: $user= App\Models\User

------ for users model.

Please or to participate in this conversation.