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

路由

必选参数

route中定义的{id}要和控制器的方法中传参的$id一致

  1. Route::get('/product/item/{id}.html','Product@detail');
  1. public function detail($id){
  2. $res = array('id'=>$id,'title'=>'荣耀10,'price'=>2000);
  3. return view('product.detail',['product'=>$res]);
  4. }

可选参数

在控制器的方法中要有个默认值

  1. Route::get('/product/item/{id?}','Product@detail');
  1. public function detail($id=2){
  2. $res = array('id'=>$id,'title'=>'荣耀10,'price'=>2000);
  3. return view('product.detail',['product'=>$res]);
  4. }

命名路由

  1. Route::get('/product/item/{id?}','Product@detail')->name('product_item');

视图blade引擎流程控制

@foreach

  1. public function detail($id=2){
  2. $res[0] = array('id'=>2,'title'=>'荣耀10,'price'=>2000);
  3. $res[1] = array('id'=>5,'title'=>'iphone11,'price'=>8000);
  4. $res[2] = array('id'=>8,'title'=>'iphone12,'price'=>18000);
  5. return view('product.detail',['product'=>$res]);
  6. }
  1. @foreach($product as $item)
  2. 商品名称:{{$item['title']}}
  3. @endforeach

@if

  1. @foreach($product as $item)
  2. @if($item['price']<3000)
  3. 商品名称:{{$item['title']}}
  4. @endif
  5. @endforeach

@else

  1. @foreach($product as $item)
  2. @if($item['price']<3000)
  3. 商品名称:{{$item['title']}}
  4. @else
  5. {{$item['title']}}买不起
  6. @endif
  7. @endforeach

@elseif

  1. @foreach($product as $item)
  2. @if($item['price']<3000)
  3. 商品名称:{{$item['title']}}
  4. @elseif($item['price']>3000 && $item['price']<10000)
  5. 买{{$item['title']}}有压力
  6. @else
  7. {{$item['title']}}买不起
  8. @endif
  9. @endforeach

@while

  1. <?php
  2. $i = 0;
  3. $count = count($product);
  4. ?>
  5. @while($i<$count)
  6. {{$product[$i]['title']}}
  7. <?php $i++;?>
  8. $endwhile

原生数据库操作

select

注意应用DB类,查询出来的是一个二维数组,返回结果集
use Illuminate\Support\Facades\DB;

  1. public function detail(){
  2. $res = DB::select('select * from product where id>?',[2]);
  3. //$res = DB::select("select * from product where id>:id",['id'=>2]);
  4. echo '<pre>';
  5. print_r($res);
  6. return view('product.detail');
  7. }

insert

返回bool值

  1. $res = DB::insert('insert into product(`pname`,`price`) values ("iphone12",20000)');

update

返回受该语句影响的行数

  1. $res = DB::update('update product set pname="荣耀10",price=3000 where id=10');

delete

返回受该语句影响的行数

  1. $res = DB::delete('delete from product where pname="iphone12"');

查询构造器

查询

只要查询的结果集是 Collection 类型,就可以在后面加toArray()方法。返回的每个数组是一个对象,从对象中取值用->

  1. echo '<pre>';
  2. $res = DB::table('product')->get()->toArray();
  3. foreach($res as $item){
  4. echo $item->pname.'<br>';
  5. }

where条件

在获取结果集之前(get()),在table之后,在这2者之间
where中2个参数默认是 =

  1. echo '<pre>';
  2. $res = DB::table('product')->where('id',2)->get()->toArray();
  3. // $res = DB::table('product')->where('id','>',2)->get()->toArray();
  4. var_dump($res);
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:数据表的操作还是非常简单的吧
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!