search
HomePHP FrameworkLaravelWhat is Laravel Tinker? how to use?

What is Laravel Tinker? how to use?

Jun 04, 2021 am 11:39 AM
lavarelphp

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

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

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

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',
];

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

>>> inspire
>>> env

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');

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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Using Laravel: Streamlining Web Development with PHPUsing Laravel: Streamlining Web Development with PHPApr 19, 2025 am 12:18 AM

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

Laravel: An Introduction to the PHP Web FrameworkLaravel: An Introduction to the PHP Web FrameworkApr 19, 2025 am 12:15 AM

Laravel is a modern PHP framework that provides a powerful tool set, simplifies development processes and improves maintainability and scalability of code. 1) EloquentORM simplifies database operations; 2) Blade template engine makes front-end development intuitive; 3) Artisan command line tools improve development efficiency; 4) Performance optimization includes using EagerLoading, caching mechanism, following MVC architecture, queue processing and writing test cases.

Laravel: MVC Architecture and Best PracticesLaravel: MVC Architecture and Best PracticesApr 19, 2025 am 12:13 AM

Laravel's MVC architecture improves the structure and maintainability of the code through models, views, and controllers for separation of data logic, presentation and business processing. 1) The model processes data, 2) The view is responsible for display, 3) The controller processes user input and business logic. This architecture allows developers to focus on business logic and avoid falling into the quagmire of code.

Laravel: Key Features and Advantages ExplainedLaravel: Key Features and Advantages ExplainedApr 19, 2025 am 12:12 AM

Laravel is a PHP framework based on MVC architecture, with concise syntax, powerful command line tools, convenient data operation and flexible template engine. 1. Elegant syntax and easy-to-use API make development quick and easy to use. 2. Artisan command line tool simplifies code generation and database management. 3.EloquentORM makes data operation intuitive and simple. 4. The Blade template engine supports advanced view logic.

Building Backend with Laravel: A GuideBuilding Backend with Laravel: A GuideApr 19, 2025 am 12:02 AM

Laravel is suitable for building backend services because it provides elegant syntax, rich functionality and strong community support. 1) Laravel is based on the MVC architecture, simplifying the development process. 2) It contains EloquentORM, optimizes database operations. 3) Laravel's ecosystem provides tools such as Artisan, Blade and routing systems to improve development efficiency.

Laravel framework skills sharingLaravel framework skills sharingApr 18, 2025 pm 01:12 PM

In this era of continuous technological advancement, mastering advanced frameworks is crucial for modern programmers. This article will help you improve your development skills by sharing little-known techniques in the Laravel framework. Known for its elegant syntax and a wide range of features, this article will dig into its powerful features and provide practical tips and tricks to help you create efficient and maintainable web applications.

The difference between laravel and thinkphpThe difference between laravel and thinkphpApr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Laravel user login function listLaravel user login function listApr 18, 2025 pm 01:06 PM

Building user login capabilities in Laravel is a crucial task and this article will provide a comprehensive overview covering every critical step from user registration to login verification. We will dive into the power of Laravel’s built-in verification capabilities and guide you through customizing and extending the login process to suit specific needs. By following these step-by-step instructions, you can create a secure and reliable login system that provides a seamless access experience for users of your Laravel application.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment