laravel5访问器 & 修改器

laravel5获取器,user表里的status状态

可以直接在视图里面写

[php]  view plain  copy
  1. @if($user->status == 1) 有效 @elseif($user->status == 0) 无效 @else 停用 @endif  


laravel和thinkphp5,yii2都提供了状态获取的快捷方法,只需要在模型中定义

user.php

[php]  view plain  copy
  1. /** 
  2.  * status获取 
  3.  * @param $value 
  4.  * @return mixed 
  5.  */  
  6. public function getStatusAttribute($value)  
  7. {  
  8.     $status =  ['0'=>'注销','1'=>'有效','2'=>'停用'];  
  9.     return $status[$value];  
  10. }  
  11.   
  12. /** 
  13.  * status 还原回去 
  14.  * @param $value 
  15.  * @return array 
  16.  */  
  17. public function setStatusAttribute($value)  
  18. {  
  19.     $status =  ['0'=>'注销','1'=>'有效','2'=>'停用'];  
  20.     return array_keys($status,$value);  
  21. }  


在视图中直接输出

[php]  view plain  copy
  1. @if(!empty($userlists))  
  2.     @foreach($userlists as $userlist)  
  3.         <tr role="row" @if($loop->iteration %2 ==0) class="old" @else class="even" @endif>  
  4.             <td>{{$loop->iteration}}</td>  
  5.             <td>{{$userlist->uname}}</td>  
  6.             <td>{{$userlist->name}}</td>  
  7.             <td>{{$userlist->email}}</td>  
  8.             <td>{{$userlist->mobile_phone}}</td>  
  9.             <td>{{$userlist->qq}}</td>  
  10.             <td>{{$userlist->status}}</td>  
  11.             <td>{{$userlist->description}}</td>  
  12.   
  13.             <td>{{$userlist->created_at}}</td>  
  14.             <td>{{$userlist->updated_at}}</td>  
  15.   
  16.             <td>  
  17.                 <a style="margin:3px;" href="/admin/user/{{$userlist->id}}/edit" class="X-Small btn-xs text-success "><i class="fa fa-edit"></i> 编辑</a>  
  18.                 <a style="margin:3px;" href="#"    data-toggle="modal" data-target="#modal-danger" class="delBtn X-Small btn-xs text-danger"><i class="fa fa-times-circle"></i> 删除</a>  
  19.             </td>  
  20.         </tr>  
  21.   
  22.     @endforeach  
  23.     <tr >  
  24.         <td align="right" colspan="7" id="pagination">  
  25.             {{$userlists->links()}}  
  26.         </td>  
  27.     </tr>  
  28. @endif  


效果如下

猜你喜欢

转载自blog.csdn.net/qq_16399991/article/details/80759917
今日推荐