Blogger Information
Blog 37
fans 0
comment 0
visits 21057
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
综合实战第二课:laravel基础1-PHP培训九期线上班
渡劫小能手
Original
538 people have browsed it

模型的创建方式及简单使用

model的创建

可以使用artisan命令创建,默认在于 app/Http 平级的下面

  1. php artisan make:model Article

数据库连接

laravel默认使用model名称小写+s去数据库里面找相同名字的表

查询主键

findOrFail(1) ,查询主键为1的记录,laravel默认主键名为id

  1. $res = \App\Article::findOrFail(1);

直接拿出表里面的数据

  1. public function show()
  2. {
  3. echo '<pre>';
  4. $res = \App\Article::findOrFail(1)->toArray();
  5. print_r($res);
  6. }

指定表名

在model类中指定

  1. class Article extends Model
  2. {
  3. protected $table = 'article';
  4. }

指定id

在model类中指定

  1. class Article extends Model
  2. {
  3. protected $table = 'article';
  4. protected $primaryKey = 'aid';
  5. }

调用model中的方法

  1. lass Article extends Model
  2. {
  3. protected $table = 'article';
  4. // protected $primaryKey = 'aid';
  5. public function bbb(){
  6. $data = $this->findOrFail(1)->toArray();
  7. return $data;
  8. }
  9. }
  1. class Home extends Controller
  2. {
  3. public function show(Article $aaa)
  4. {
  5. echo '<pre>';
  6. // $res = \App\Article::findOrFail(1)->toArray();
  7. $res = $aaa->bbb();
  8. print_r($res);
  9. }
  10. }

视图

view的创建

只能手动创建,可以在views下建立文件夹,比如home文件夹,里面有lists.blade.php,访问就必须带上文件夹名字 return view('home.lists'); 来指定访问此视图

返回数据给view

  1. public function lists(){
  2. $res = \App\Article::findOrFail(1)->toArray();
  3. print_r($res['money']);
  4. return view('home.lists',['order'=>$res]);
  5. }
  1. <p>金额:{{$order['money']}}</p>

CSRF

在post请求时需要csrf,get请求不需要。
在页面任何地方写个@csrf,报错419
405,get/post方式错误

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <script type="text/javascript" src="/js/jquery.min.js"></script>
  6. </head>
  7. <body>
  8. <div>用户名:<input type="text" name="username"></div>
  9. <div>密码:<input type="password" name="pwd"></div>
  10. <div>重复密码:<input type="password" name="repwd"></div>
  11. <button type="button" onclick="doreg()">注册</button>
  12. @csrf
  13. <script type="text/javascript">
  14. function doreg() {
  15. var username = $.trim($('input[name="username"]').val());
  16. var pwd = $.trim($('input[name="pwd"]').val());
  17. var repwd = $.trim($('input[name="repwd"]').val());
  18. if (username == '') {
  19. return alert('请输入用户名');
  20. }
  21. $.post('/account/savereg', {
  22. username: username,
  23. pwd: pwd,
  24. _token: $('input[name="_token"]').val()
  25. }, function (res) {
  26. alert(res.msg);
  27. if (res.code == 0) {
  28. window.location.href = '/';
  29. }
  30. }, 'json');
  31. }
  32. </script>
  33. </body>
  34. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:作业看上去非常的整洁, 舒服 , 看来你对markdown语法的运用很熟悉了, laravel是一个最流行的框架, 如果你打算做web开发, 并选择php做为快速开发语言的话, laravel是必须要掌握的
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!