laravel5获取器,user表里的status状态
可以直接在视图里面写
@if($user->status == 1) 有效 @elseif($user->status == 0) 无效 @else 停用 @endif
laravel和thinkphp5,yii2都提供了状态获取的快捷方法,只需要在模型中定义
user.php
/** * status获取 * @param $value * @return mixed */ public function getStatusAttribute($value) { $status = ['0'=>'注销','1'=>'有效','2'=>'停用']; return $status[$value]; } /** * status 还原回去 * @param $value * @return array */ public function setStatusAttribute($value) { $status = ['0'=>'注销','1'=>'有效','2'=>'停用']; return array_keys($status,$value); }
在视图中直接输出
@if(!empty($userlists)) @foreach($userlists as $userlist) <tr role="row" @if($loop->iteration %2 ==0) class="old" @else class="even" @endif> <td>{{$loop->iteration}}</td> <td>{{$userlist->uname}}</td> <td>{{$userlist->name}}</td> <td>{{$userlist->email}}</td> <td>{{$userlist->mobile_phone}}</td> <td>{{$userlist->qq}}</td> <td>{{$userlist->status}}</td> <td>{{$userlist->description}}</td> <td>{{$userlist->created_at}}</td> <td>{{$userlist->updated_at}}</td> <td> <a style="margin:3px;" href="/admin/user/{{$userlist->id}}/edit" class="X-Small btn-xs text-success "><i class="fa fa-edit"></i> 编辑</a> <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> </td> </tr> @endforeach <tr > <td align="right" colspan="7" id="pagination"> {{$userlists->links()}} </td> </tr> @endif
效果如下