在Laravel中以1个表单更新2个表

时间:2018-10-01 15:43:20

标签: laravel

我有users表和profiles表,现在我想让用户以单一形式更新其信息。

逻辑

  1. 姓名和电子邮件将在用户表中更新
  2. 其余字段将在配置文件表中更新

代码

blade(窗体)

{{ Form::model($user, array('route' => array('profileupdate', $user->id), 'method' => 'PUT','files' => true)) }}
                <div class="row">
                    <div class="col-md-6">
                        <label for="name">Name</label>
                        {{Form::text('name', null, array('class' => 'search-field'))}}
                    </div>
                    <div class="col-md-6">
                        <label for="email">Email</label>
                        {{Form::text('email', null, array('class' => 'search-field'))}}
                    </div>
                    <div class="col-md-6 mt-3">
                        <label for="about">About</label>
                        {{Form::textarea('about', null, array('class' => 'search-field'))}}
                    </div>
                    <div class="col-md-6 mt-3">
                        <div class="row">
                            <div class="col-md-6">
                                <label for="phone">Phone</label>
                                {{Form::text('phone', null, array('class' => 'search-field'))}}
                            </div>
                            <div class="col-md-6">
                                <label for="website">Website</label>
                                {{Form::text('website', null, array('class' => 'search-field'))}}
                            </div>
                            <div class="col-md-6 mt-3">
                                <label for="state">State</label>
                                {{Form::text('state', null, array('class' => 'search-field'))}}
                            </div>
                            <div class="col-md-6 mt-3">
                                <label for="city">City</label>
                                {{Form::text('city', null, array('class' => 'search-field'))}}
                            </div>
                            <div class="col-md-12 mt-3">
                                <label for="photo">Photo</label>
                                {{Form::file('photo', array('class' => 'search-field'))}}
                            </div>
                        </div>
                    </div>

                    <div class="col-md-12">
                        {{ Form::submit('Update', array('class' => 'btn btn-success mt-5')) }}
                    </div>
                </div>

{{Form::close()}}

屏幕截图(上面的代码)

one

controller

public function update(Request $request, $id)
{
        $user = User::find($id);
        $user = User::where('id',$id)->first();
        $user->name = $request->input('name');
        $user->save();

        $profile = Profile::find($id);
        $profile = Profile::where('id',$id)->first();
        $profile->user_id = $request->input('user_id');
        $profile->about = $request->input('about');
        $profile->website = $request->input('website');
        $profile->phone = $request->input('phone');
        $profile->state = $request->input('state');
        $profile->city = $request->input('city');

        if ($request->hasFile('photo')) {
            $photo = $request->file('photo');
            $filename = 'photo' . '-' . time() . '.' . $photo->getClientOriginalExtension();
            $location = public_path('images/' . $filename);
            Image::make($photo)->resize(1300, 362)->save($location);
            $profile->photo = $filename;

            $oldFilename = $profile->photo;
            $profile->photo = $filename;
            Storage::delete($oldFilename);
        }

        $profile->save();
        return redirect()->route('profile', $user->id)->with('success', 'Your info are updated');
}
  

PS:请不要使用我的控制器方法,我只是将那些   这样您就可以知道哪个字段属于哪个表。

问题

有什么主意我可以同时将数据保存在2个表中吗?

我收到此错误

  

Symfony \组件\ HttpKernel \ Exception \ MethodNotAllowedHttpException没有消息

2 个答案:

答案 0 :(得分:1)

您需要检查是否同时保存

public function update(Request $request, $id)
{
    $user = User::find($id);
    $user = User::where('id',$id)->first();
    $user->name = $request->input('name');
    if($user->save())
    {
    $profile = Profile::find($id);
    $profile = Profile::where('id',$id)->first();
    $profile->user_id = $request->input('user_id');
    $profile->about = $request->input('about');
    $profile->website = $request->input('website');
    $profile->phone = $request->input('phone');
    $profile->state = $request->input('state');
    $profile->city = $request->input('city');

    if ($request->hasFile('photo')) {
        $photo = $request->file('photo');
        $filename = 'photo' . '-' . time() . '.' . $photo->getClientOriginalExtension();
        $location = public_path('images/' . $filename);
        Image::make($photo)->resize(1300, 362)->save($location);
        $profile->photo = $filename;

        $oldFilename = $profile->photo;
        $profile->photo = $filename;
        Storage::delete($oldFilename);
    }

    $profile->save();
    return redirect()->route('profile', $user->id)->with('success', 'Your info are updated');
}
    return redirect()->back()->with('error','Something went wrong');
}

Symfony \组件\ HttpKernel \ Exception \ MethodNotAllowedHttpException当您尝试通过在浏览器中访问URL来访问post方法时,将出现此异常。

答案 1 :(得分:0)

由于您不能在一个MySQL命令中插入多个表,因此无法满足您的要求。但是,您可以使用单笔交易

DB::trasaction(function() {
    // first update
    User::whereId($id)->update([
        'name'  => $reqquest->name,
        'email' => $request->email
    ]);


    // second update
    Profile::whereId($id)->update([
        'about'  => $request->website,
        'mobile' => $request->mobile,
        ..........
        ..........
    ]);
}
相关问题