一、创建设置
修改 app/User.php
protected $casts = [
'setting' => 'json'
];
public function setting()
{
return new Setting($this);
}
新建文件:app/Setting.php
namespace App;
class Setting
{
protected $allowed = ['city','bio'];
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function merge(array $attribute)
{
$setting = array_merge($this->user->setting, array_only($attribute,$this->allowed));
return $this->user->update(['setting'=>$setting]);
}
}
二、创建控制器
创建设置控制器
php artisan make:controller SettingController
修改 app/Http/Controllers/SettingController.php
namespace App\Http\Controllers;
class SettingController extends Controller
{
public function index()
{
return view('users.setting');
}
public function store()
{
user()->setting()->merge(request()->all());
return back();
}
}
注册路由
Route::get('setting', 'SettingController@index');
Route::post('setting', 'SettingController@store');
三、创建视图
创建文件:resources/views/users/setting.blade.php
访问:http://127.0.0.1:8000/setting 进行测试