Home > PHP Framework > Laravel > body text

What is Laravel Tinker? how to use?

藏色散人
Release: 2021-06-16 09:02:28
forward
4140 people have browsed it

The following tutorial column will introduce you to the use of Laravel Tinker from laravel. I hope it will be helpful to friends in need!

Usage of Laravel Tinker

Introduction to Laravel Tinker

In native PHP, you can use the php -a command Use interactive Shell:

# php -a
php > $msg = "Hello world!";
php > print $msg;
Hello world!
php > $num = array_sum([1, 2, 3]);
php > print $num;
6
Copy after login

In addition, you can also use PsySH . Compared with the native php -a, PsySH has more advanced features and is more powerful.

Install globally through Composer:

composer g require psy/psysh:@stable
Copy after login

Then execute psysh on the command line to enter the interactive shell (you need to ensure that the bin of global Composer is directory in the system path), you can write various codes in PsySH, print statements, calculate expressions, write functions, etc.

Laravel Tinker is implemented based on PsySH. Through Tinker, we can realize various interactions with Laravel applications on the command line, including additions, deletions, modifications and queries of the database.

You can enter the interactive shell of Laravel Tinker through php artisan tinker on the command line.

View help documentation

In Laravel Tinker, the doc command can be used to view the help documentation for a certain function or method, for example, view the helper functionconfig( ) How to use:

doc config

show config
Copy after login

Run Artisan command

Runphp artisan tinker is equivalent to starting the Laravel application from the console, before running the interactive command , the tinker command will add some commands to the Shell, these commands are defined in the $commandWhitelist attribute of Laravel\Tinker\Console\TinkerCommand:

protected $commandWhitelist = [
    'clear-compiled', 'down', 'env', 'inspire', 'migrate', 'optimize', 'up',
];
Copy after login

So, these commands can be run directly in the Tinker Shell:

>>> inspire
>>> env
Copy after login

Testing Laravel Code

You can use the console to create a new model, save it to the database, and then query this Records.

php artisan tinker
>>> migrate
>>> use App\User;
>>> $user = new User();
>>> $user->name = "test";
>>> $user->email = "test@163.com";
>>> $user->password = bcrypt('123456');
>>> $user->save();
>>> $user = User::where('email', 'test@163.com')->first();
>>> factory(User::class, 3)->create();
>>> 
>>> $user = App\User::find(1);
>>> $user->delete();
>>> 
>>> $log = app('log');
>>> $log->info('test');
Copy after login

Related recommendations: The latest five Laravel video tutorials

The above is the detailed content of What is Laravel Tinker? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!