Laravel 5.2--改变数据库字段值,编辑时候,默认选中
模型
<?php namespace App\Models; use App\Helpers\ImageHelper; use App\Libraries\Nestedset\NodeTrait; class Popular extends Model { use NodeTrait; protected $table = 'populars'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'status', 'title', 'image', 'provider', 'parent_id', 'resource', 'descript', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'status', 'orderby', '_lft', '_rgt', 'provider_value', 'created_at_millisecond', 'created_at', 'updated_at_millisecond', 'updated_at' ]; /** * get provider attribute */ public function getProviderAttribute($value) { switch ($value) { case '1': $provider = 'category'; break; case '2': $provider = 'brand'; break; case '3': $provider = 'product'; break; default: if (! $this->attributes['is_click']) { $provider = 'unclick'; } else { $provider = 'undefine'; } break; } return $provider; } /** * get provider value */ public function getProviderValueAttribute() { switch ($this->provider) { case 'category': $value = 1; break; case 'brand': $value = 2; break; case 'product': $value = 3; break; default: $value = 0; break; } return $value; }
视图
@include('admin.widget.select', [
'colsm' => '12',
'collg' => '6',
'id' => 'provider',
'name' => 'provider',
'title' => '跳转方式',
'selected' => isset($showPopular->provider_value) ? $showPopular->provider_value : '',
'values' => [
'商品详情' => '1',
'分类商品列表' => '2',
'品牌商品列表' => '3',
]
])