[英]Add Profile Information in MyAccountController [backpack-laravel]
我想添加一個頁面,以更改用戶的個人資料:姓名,姓氏等。
文件:MyAccountController.php
函數:public function postAccountProfileForm(UpdateRequest $request) FormRequest,返回空
DB:用戶->個人資料
文件控制器: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 Framework 5.7.12 "php": "^7.1.3" "backpack/backupmanager": "^1.4" "backpack/crud": "^3.4" "backpack/langfilemanager": "^1.0" "backpack/logmanager" : "^2.3" "backpack/newscrud": "^2.1" "backpack/pagemanager": "^1.1" "backpack/permissionmanager": "^3.12" "backpack/settings": "^2.1" "barryvdh/laravel- elfinder": "^0.4.1" "fideloper/proxy": "^4.0" "laravel/framework": "5.7.*", "laravel/tinker": "^1.0", "laravelcollective/html": "^ 5.7", "mews/purifier": "^2.1", "tymon/jwt-auth": "^0.5.12"
您是否告訴過背包不要設置身份驗證路線,以便您可以設置自己的路線?
繼續到config/backpack/base.php
並相應地更改setup_auth_routes
和setup_my_account_routes
。
如果您緩存了路由,請不要忘記php artisan route:cache
最好的,佩德羅
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.