dqw7121 2018-05-17 12:40
浏览 38

laravel更新注册用户

I have recently started working in Laravel, my first Framework. I have experience with PHP but I am a little lost now.

I used the default auth that comes with laravel. But I am trying to make a function to edit the users that have registered.

I dont quite see how the data gets to the database from the form.

My form: (didn't put all inputs)

@isset($user)
<form method="POST" action="{{ WHAT GOES HERE?? }}">
@csrf
{{--name--}}
<div class="form-group row">
    <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

    <div class="col-md-6">
        <input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ $user->name }}" required autofocus>
    </div>
</div>

{--last name--}}
<div class="form-group row">
    <label for="lastname"
           class="col-md-4 col-form-label text-md-right">{{ __('Last Name') }}</label>

    <div class="col-md-6">
        <input id="lastname" type="text"
               class="form-control{{ $errors->has('lastname') ? ' is-invalid' : '' }}" name="lastname" value="{{ $user->lastname }}" required autofocus>
    </div>
</div>
<div class="form-group row mb-0">
    <div class="col-md-6 offset-md-4">
        <button type="submit" class="btn btn-primary">
            {{ __('Edit user') }}
        </button>
    </div>
</div>

I did not make a route yet, as I do not know how exactly. Or if this is even needed.

My controller:

public function edit_profile($userId, Request $request)
    {
        $this->isAllowed($userId, true);
        $userProfile = User::findOrFail((int)$userId);


        //TODO validation
        //TODO update user model + save to db

        $userProfile->update();

        return view('user/edit_profile', [
            /* $ */
            'user' => $userProfile,
        ]);
    }

As stated; I do know what needs to happen. I just don't know and understand how.

And last, my user.php

protected $fillable = [
'name',
'lastname',
'email',
'password',
'mobile_number',
'date_of_birth',
];

My next step would be to make a route, to get the data to the controller. But I don't see how this route would look.

  • 写回答

1条回答 默认 最新

  • dousonghs58612 2018-05-17 13:04
    关注

    You need to create a Route for your action. Go to routes->web.php(laravel 5.3 or above) and add your route structure.

    Route::http-verb('url name',"Controller name@Controller Method name");
    

    This is the route you have to add in your web.php file

    Route::post('edit-profile', 'ProfileController@edit_profile');
    

    I am assuming that you have a controller with name as ProfileController and inside you have edit_profile method.

    {{ url('/edit-profile') }}
    

    add above code to action attribute.

    If you are using laravel 5.2 or old version the routes configuration resides inside app/Http/routes.php.

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗