博客列表 >laravel控制器调用数据库,视图层ifelse等

laravel控制器调用数据库,视图层ifelse等

张浩刚
张浩刚原创
2019年12月31日 17:39:511138浏览

控制器:Article.php

  1. namespace App\Http\Controllers\Article;
  2. use App\Http\Controllers\Controller;
  3. use Illuminate\Support\Facades\DB;
  4. class Article extends Controller
  5. {
  6. public function show()
  7. {
  8. //增加,2种方式皆可
  9. $arr = DB::insert('insert into article(`title`,`img`,`newstext`) values(?,?,?)', ['盗梦空间', 'http....jpg', '这是一个盗梦的故事']); // ?占位符
  10. $arr = DB::insert('insert into article(`title`,`img`,`newstext`) values(:title,:img,newstext)', ['盗梦空间', 'http....jpg', '这是一个盗梦的故事']);
  11. //修改
  12. $uppdate = DB::update('update article set `title`="喜乐街", `newstext`="这是一个在喜乐街的故事" where id=? ', [2]);
  13. //删除
  14. DB::delete('delete from article where id=?', [2]);
  15. //查询,2种方式皆可
  16. $res = DB::select('select * from article where id = ?', [1]); // ?为占位符
  17. $res = DB::select('select * from article where id= :id', ['id'=>1]);
  18. foreach($res as $val){
  19. //这里数组内是对象
  20. return view('index', ['index'=>$val]);
  21. }
  22. //获取全部
  23. $args = DB::table('users')->get();
  24. }
  25. }
  26. //
  27. DB类之查询
  28. DB::table("表名")->where("id","=",1)->get(); //这里=可省略
  29. DB::table("表名")->where('id',1)->get();//结果同上
  30. 满足条件的全部获取:DB::table("表名")->where("id",">","1")->get();
  31. 满足条件的第一列获取:DB::table("表名")->where("id",">","1")->first();
  32. 满足条件的全部字段:DB::table("表名")->where("id",">","1")->lists("title");

路由

  1. Route::get('/show', 'Article\Article@show');

模板引擎:index.blade.php

  1. <p>{{$index->title}}</p>
  2. <p><img src="{{$index->img}}"></p>
  3. <p>{{$index->newstext}}</p>

必填参数,此方法最多

  1. 控制器:Article.php
  2. class Article extends Controller
  3. {
  4. public function show($id)
  5. {
  6. $res = ['id'=>$id, 'title'=>'华为手机', 'price'=>5999];
  7. return view('index', ['index'=>$res]);
  8. }
  9. }
  10. 路由
  11. Route::get('/{id}.html', 'Article\Article@show'); //可实现类似京东的伪静态如:www.920930.com/2592652487.html

可选参数

  1. 控制器:Article.php
  2. class Article extends Controller
  3. {
  4. public function show($id=3)//这是设置一个默认值,无值时默认为3
  5. {
  6. $res = ['id'=>$id, 'title'=>'华为手机', 'price'=>5999];
  7. return view('index', ['index'=>$res]);
  8. }
  9. }
  10. 路由
  11. Route::get('/itme/{id?}.html', 'Article\Article@show');

路由命名

  1. Route::get('/{id}.html', 'Article\Article@show')->name('article-show');
  2. //只要引用这个路由名article-show,就可以调用这个路由了

模板渲染

  1. 1、数据内外均为数组时
  2. 控制器:Article.php
  3. class Article extends Controller
  4. {
  5. public function show()
  6. {
  7. //数据内外均为数组时
  8. $res = [
  9. ['id'=>1, 'title'=>'华为手机', 'price'=>5999],
  10. ['id'=>2, 'title'=>'小米手机', 'price'=>2999],
  11. ['id'=>3, 'title'=>'魅族手机', 'price'=>1999]
  12. ];
  13. return view('index', ['index'=>$res]);
  14. }
  15. }
  16. 模板渲染
  17. @foreach($index as $item)
  18. @if( $item['price'] < 2000 ) //注意这里的变量不能用{{}}包裹,否在报错
  19. <p>{{$item['title']}},买得起</p>
  20. @elseif( $item['price'] > 2000 && $item['price'] < 3000)
  21. <p>{{$item['title']}},有点贵</p>
  22. @else
  23. <p>{{$item['title']}},太贵了</p>
  24. @endif
  25. @endforeach
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议