在 MyAccountController [backpack-laravel] 中添加个人资料信息
Posted
技术标签:
【中文标题】在 MyAccountController [backpack-laravel] 中添加个人资料信息【英文标题】:Add Profile Information in MyAccountController [backpack-laravel] 【发布时间】:2019-04-23 12:48:11 【问题描述】:我考虑过添加一个页面,以更改用户的个人资料:姓名、姓氏等。
错误报告
文件:MyAccountController.php
函数:公共函数 postAccountProfileForm(UpdateRequest $request) FormRequest,返回空
我做了什么:
数据库: 用户->个人资料
文件控制器:App\Http\Controllers\Auth\MyAccountController
namespace App\Http\Controllers\Auth;
use Backpack\Base\app\Http\Controllers\Auth\MyAccountController as BaseMyAccountController;
use App\Http\Requests\Auth\Account_profileRequest as StoreRequest;
use App\Http\Requests\Auth\Account_profileRequest as UpdateRequest;
use Auth;
use App\Models\Auth\Account_profile;
class MyAccountController extends BaseMyAccountController
/**
* Show the user a form to change his personal information.
*/
public function getAccountProfileForm()
$user = Auth::user();
$this->data['title'] = trans('backpack::base.my_account');
$this->data['profile'] = Account_profile::getAccount_profilebyId($user->id);
return view('backpack::auth.account.update_profile', $this->data);
/**
* Save the modified personal information for a user.
*/
public function postAccountProfileForm(UpdateRequest $request)
//var_dump($request);
//die();
//$result = $this->guard()->user()->update($request->except(['_token']));
$result = Account_profile::getAccount_profilebyId($this->guard()->user()['id'])->update($request->except(['_token']));
if ($result)
Alert::success(trans('backpack::base.account_updated'))->flash();
else
Alert::error(trans('backpack::base.error_saving'))->flash();
return redirect()->back();
文件请求:App\Http\Requests\Auth\Account_profileRequest
namespace App\Http\Requests\Auth;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class Account_profileRequest extends FormRequest
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
// only allow updates if the user is logged in
return backpack_auth()->check();
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
return [
// 'name' => 'required|min:5|max:255'
'nome' => 'required|min:5|max:45',
'cognome' => 'required|min:5|max:45',
'sesso' => 'required|min:5|max:45',
'countrys_id' => 'required|numeric',
'regions_id' => 'required|numeric',
//'provinces_id' => 'required|numeric',
//'citys_id' => 'required|numeric',
'users_id' => 'required|numeric',
//'attivo' => 'required|boolean',
//'accetto_condizionigenerali' => 'required|boolean',
//'accetto_marketing' => 'required|boolean',
//'attivo_notifiche' => 'required|boolean'
//continue_13
];
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
return [
//
];
/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
return [
//
];
文件模型:App\Models\Auth\Account_profile
namespace App\Models\Auth;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use App\Models\Profile;
class Account_profile extends Model
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
public static function getAccount_profilebyId($id)
return Profile::find($id);
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| ACCESORS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
文件路径:Routes\backpack\Custom
// --------------------------
// Custom Backpack Routes
// --------------------------
// This route file is loaded automatically by Backpack\Base.
// Routes you generate using Backpack\Generators will be placed here.
Route::group([
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
'namespace' => 'App\Http\Controllers\Admin',
], function () // custom admin routes
); // this should be the absolute last line of this file
Route::group(
[
'namespace' => 'App\Http\Controllers\Auth',
'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
'prefix' => config('backpack.base.route_prefix'),
],
function ()
// if not otherwise configured, setup the auth routes
if (config('backpack.base.setup_auth_routes'))
// Authentication Routes...
// if not otherwise configured, setup the dashboard routes
if (config('backpack.base.setup_dashboard_routes'))
Route::get('dashboard', 'AdminController@dashboard')->name('backpack.dashboard');
Route::get('/', 'AdminController@redirect')->name('backpack');
// if not otherwise configured, setup the "my account" routes
if (config('backpack.base.setup_my_account_routes'))
Route::get('edit-account-profile', 'MyAccountController@getAccountProfileForm')->name('backpack.account.profile');
Route::post('edit-account-profile', 'MyAccountController@postAccountProfileForm');
);
文件刀片:resources\views\vendor\backpack\base\auth\account\update_profile.blade.php
@extends('backpack::layout')
@section('after_styles')
<style media="screen">
.backpack-profile-form .required::after
content: ' *';
color: red;
</style>
@endsection
@section('header')
<section class="content-header">
<h1>
trans('backpack::base.my_account')
</h1>
<ol class="breadcrumb">
<li>
<a href=" backpack_url() "> config('backpack.base.project_name') </a>
</li>
<li>
<a href=" route('backpack.account.info') "> trans('backpack::base.my_account') </a>
</li>
<li class="active">
trans('backpack::base.update_account_info') Profile
</li>
</ol>
</section>
@endsection
@section('content')
<div class="row">
<div class="col-md-3">
@include('backpack::auth.account.sidemenu')
</div>
<div class="col-md-6">
<div class="box">
<div class="box-body backpack-profile-form">
!! Form::open(array('route' => 'backpack.account.profile', 'method' => 'post')) !!
<div class="form-group">
@php
$label = 'Nome';
$field = 'nome';
@endphp
!! Form::label($field, $label, ['class' => 'required']) !!
!! Form::text($field, old($field) ? old($field) : $profile->$field, ['class' => 'form-control', 'required']) !!
</div>
<div class="clearfix"></div>
<div class="form-group">
@php
$label = 'Cognome';
$field = 'cognome';
@endphp
!! Form::label($field, $label, ['class' => 'required']) !!
!! Form::text($field, old($field) ? old($field) : $profile->$field, ['class' => 'form-control', 'required']) !!
</div>
<div class="clearfix"></div>
<div class="form-group">
@php
$label = 'Sex';
$field = 'sesso';
@endphp
!! Form::label($field, $label, ['class' => 'required']) !!
!! Form::select($field, array('M' => 'Male', 'F' => 'Female'), old($field) ? old($field) : $profile->$field, ['class' => 'form-control', 'required']) !!
</div>
<div class="clearfix"></div>
<div class="box-footer">
@php
$field = 'id';
$label = '<span class="ladda-label"><i class="fa fa-save"></i>'.trans('backpack::base.save').'</span>';
@endphp
!! Form::hidden($field, old($field) ? old($field) : $profile->$field) !!
!! Form::button($label, ['class' => 'btn btn-success', 'type' => 'submit']) !!
<a href=" backpack_url() " class="btn btn-default"><span class="ladda-label"> trans('backpack::base.cancel') </span></a>
</div>
!! Form::close() !!
</div>
</div>
</div>
</div>
@endsection
我预期会发生什么:
我希望表单已经过验证
发生了什么:
当我提交时,请求返回给我为空
我已经尝试过修复它:
背包、Laravel、PHP、DB版:
Laravel 框架 5.7.12 “php”:“^7.1.3” “背包/备份管理器”:“^1.4” “背包/crud”:“^3.4” “背包/langfilemanager”:“^1.0” “背包/日志管理器”:“^2.3” “背包/新闻报刊”:“^2.1” “背包/页面管理器”:“^1.1” “背包/权限管理器”:“^3.12” “背包/设置”:“^2.1” “barryvdh/laravel-elfinder”:“^0.4.1” “fideloper/代理”:“^4.0” “laravel/框架”:“5.7.*”, “laravel/修补匠”:“^1.0”, "laravelcollective/html": "^5.7", “喵喵/净化器”:“^2.1”, “tymon/jwt-auth”:“^0.5.12”
【问题讨论】:
如果您发现了一个错误,那么向开发人员报告它可能是您拥有的最佳选择。根据their documentation 的说法,他们通常处理Backpack for Laravel's Github 上的错误报告。如果还有时间的话,还可以阅读 *** 的“What topics can I ask about here?”。 【参考方案1】:您是否告诉背包不要设置身份验证路由以便您自己设置?
转到config/backpack/base.php
并相应地更改setup_auth_routes
和setup_my_account_routes
。
如果您缓存了路线,请不要忘记php artisan route:cache
最好, 佩德罗
【讨论】:
嗨 Pedro 我解决了,一旦我迁移到背包的第 4 版,我就会将其公开,希望它对某人有用。以上是关于在 MyAccountController [backpack-laravel] 中添加个人资料信息的主要内容,如果未能解决你的问题,请参考以下文章