我有users
表和profiles
表,现在我想让用户以单一形式更新其信息。
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()}}
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没有消息
答案 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,
..........
..........
]);
}