mafortis
mafortis

Reputation: 7128

Update 2 tables with 1 form in laravel

I have users table and profiles table, now i want let users to update their info in single form.

Logic

  1. name & email will update in users table
  2. rest of the fields will update in profiles table

Code

blade (form)

{{ 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()}}

Screenshot (code above)

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: please don't go with my controller method, I just put data that way so you can know what field belongs to what table.

Question

Any idea how i can save my data in 2 tables at the same time?

And I get this Error

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

Upvotes: 0

Views: 6812

Answers (2)

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

Since you can't insert into multiple tables in one MySQL command, there is no way to accomplish your requirement. But You can however use a Single Transaction.

DB::transaction(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,
        ..........
        ..........
    ]);
}

Upvotes: 1

Gurpal singh
Gurpal singh

Reputation: 1549

You need to check whether save in both or not

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 \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException this exception comes when you are trying to access post method by hiiting the url in browser.

Upvotes: 4

Related Questions