文章模块模型
ORM 模型
创建POST模型
使用下面命令创建一个Post模型
php artisan make:model Post
Tinker的使用
tinker是一个常驻进程,只记录启动那一刻的laravel 情况。
设置时区 /laravel54/config/app.php
// 'timezone' => 'UTC', 默认英国
'timezone' => 'Asia/Shanghai',
添加数据
ysqdeMacBook-Pro:laravel54 ysq$ php artisan make:model Post
Model created successfully.
ysqdeMacBook-Pro:laravel54 ysq$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.1.19 — cli) by Justin Hileman
>>> $post = new \app
PHP Fatal error: Class 'app' not found in Psy Shell code on line 1
>>> $post = new \app\Post();
PHP Fatal error: Class 'app/Post' not found in Psy Shell code on line 1
>>> $post = new \App\Post();
=> App\Post {#2852}
>>> $post->title ="this is post1";
=> "this is post1"
>>> $post->content ="this is content";
=> "this is content"
>>> $post->save();
=> true
>>> $post->title ="this is post2";
=> "this is post2"
>>> $post->content ="this is post2 content";
=> "this is post2 content"
>>> $post->save();
=> true
>>>
>>>
>>> $post = new \App\Post();
=> App\Post {#101}
>>> $post->title ="this is post1";
=> "this is post1"
>>> $post->content ="this is post1 content";
=> "this is post1 content"
>>> $post->save();
=> true
>>> ççç
[1]+ Stopped php artisan tinker
ysqdeMacBook-Pro:laravel54 ysq$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.1.19 — cli) by Justin Hileman
>>> $post = new \App\Post();
=> App\Post {#2854}
>>> $post->content ="this is post3 content";
=> "this is post3 content"
>>> $post->title ="this is post3";
=> "this is post3"
>>> $post->save();
=> true
查询数据
使用find(参数) 参数必须是主键
>>> \App\Post::find(2);
=> App\Post {#2859
id: 2,
title: "this is post1",
content: "this is post1 content",
user_id: 0,
created_at: "2019-11-19 07:20:24",
updated_at: "2019-11-19 07:20:24",
}
>>>
使用where(字段,值) ->first(), 只查一个,
>> \App\Post::where('title','this is post1')->first();
=> App\Post {#2859
id: 2,
title: "this is post1",
content: "this is post1 content",
user_id: 0,
created_at: "2019-11-19 07:20:24",
updated_at: "2019-11-19 07:20:24",
}
>>>
使用where(字段,值) ->get(), 返回数据对象
>> \App\Post::where('title','this is post1')->get();
=> Illuminate\Database\Eloquent\Collection {#2856
all: [
App\Post {#2852
id: 2,
title: "this is post1",
content: "this is post1 content",
user_id: 0,
created_at: "2019-11-19 07:20:24",
updated_at: "2019-11-19 07:20:24",
},
],
}
>>>
修改数据
ysqdeMacBook-Pro:laravel54 ysq$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.1.19 — cli) by Justin Hileman
>>> $post = \App\Post::find(2);
=> App\Post {#2849
id: 2,
title: "this is post1",
content: "this is post1 content",
user_id: 0,
created_at: "2019-11-19 07:20:24",
updated_at: "2019-11-19 07:20:24",
}
>>> $post->title ="this is post12"
=> "this is post12"
>>> $post->save();
=> true
删除数据
>>> $post = \App\Post::find(2);
=> App\Post {#2847
id: 2,
title: "this is post12",
content: "this is post1 content",
user_id: 0,
created_at: "2019-11-19 07:20:24",
updated_at: "2019-11-19 15:35:51",
}
>>> $post->delete();
=> true
>>>