Laravel 命令行 tinker

Laravel 包含一个强大的 REPL(Read Eval Print Loop),称为 tinker,由 Justin Hileman 编写的 PsySH 控制台驱动。tinker 允许您从交互式 shell 中的命令行与 Laravel 应用程序进行交互。

tinker 曾经是 laravel/framework 软件包的一部分,但随着 Laravel 5.4 的发布被提取到独立的软件包中。

进入 tinker

如果修改了配置信息,需要退出 tinker 命令行重新进入。

1
2
$ php artisan tinker
Psy Shell v0.9.12 (PHP 7.2.1 — cli) by Justin Hileman

添加文章数据

1
2
3
4
5
6
7
8
9
>>> $post = new \App\Post();
=> App\Post {#2856}
>>> $post->title = "this is post1";
=> "this is post1"
>>> $post->content = "this is post1 content";
=> "this is post1 content"
>>> $post->save();
=> true
>>>

根据主键获取文章数据

1
2
3
4
5
6
7
8
9
10
>>> \App\Post::find(2);
=> App\Post {#2861
id: 2,
title: "this is post2",
content: "this is post2 content",
user_id: 0,
created_at: "2020-02-14 10:56:51",
updated_at: "2020-02-14 10:56:51",
}
>>>

根据where条件获取文章数据

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> \App\Post::where('title', 'this is post1')->get();
=> Illuminate\Database\Eloquent\Collection {#2851
all: [
App\Post {#2855
id: 1,
title: "this is post1",
content: "this is post1 content",
user_id: 0,
created_at: "2020-02-14 10:52:48",
updated_at: "2020-02-14 10:52:48",
},
],
}

根据主键获取的文章数据,修改文章数据

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> \App\Post::find(2);
=> App\Post {#2849
id: 2,
title: "this is post2",
content: "this is post2 content",
user_id: 0,
created_at: "2020-02-14 10:56:51",
updated_at: "2020-02-14 10:56:51",
}
>>> $post->title = 'this is post_2';
=> "this is post_2"
>>> $post->save();
=> true

根据主键获取的文章数据,删除文章数据

1
2
3
4
5
6
7
8
9
10
11
>>> \App\Post::find(2);
=> App\Post {#2860
id: 2,
title: "this is post2",
content: "this is post2 content",
user_id: 0,
created_at: "2020-02-14 10:56:51",
updated_at: "2020-02-14 10:56:51",
}
>>> $post->delete();
=> true