【问题标题】:When editing a table, it updates the last entry in the DB instead of the selected one编辑表格时,它会更新数据库中的最后一个条目而不是选定的条目
【发布时间】:2019-06-05 20:38:01
【问题描述】:

我有一个可以有多个帖子的表,假设我去edit any random post

它显示了正确的information about said post,但是一旦我change the data 并单击我的按钮以保存信息并更新帖子,它就会updates the last post that was created 而不是所选的。

Posts 将是具有 id 列作为我的主键的表,使用 auto_increment 进行标识。

HTML:

{{Form::open(['url'=> 'employer/jobs', 'method' => 'POST'])}}
{{csrf_field()}}

<div class="row">
<div class="col-md-4">
<h4>Title *</h4>
<input type="text" class="form-control" name="title" placeholder="Name" required value="{{old('title')}}">
</div>

<div class="employees-button text-center no-padding">
<button type="submit" class="btn btn-jobs-post job-employee">Save changes</button>
</div>

{{Form::close()}}

控制器:

 /**
 * return view for edit current post by id
 */

public function show($id){
    $company = Companies::where('user_id', Auth::id())->first();
    if(count($company)){

        $jobEdit = Posts::find($id);
        $categories = Categories::where('status','active')->get();
        $cities = Cities::all();
        $job_types = JobTypes::where('status','active')->get();

        return view('employer.edit_job', compact('jobEdit', 'categories', 'cities', 'job_types', 'company')); }
        else{
        return Redirect::to('employer/company/add');
    }
}

 /**
 * update post detail in database
 */

public function updateJob(Request $request, $id){

    $this->validate($request, [
        'title' => 'required|max:50',
        'category' => 'required|numeric',
        'last_date' => 'required',
        'description' => 'required|min:20',
        'city' => 'required',
        'vacancies' => 'nullable|numeric',
    ]);

    $job = Posts::find($id);

    $job->title             = $request->title;
    $job->description       = $request->description;
    $job->type              = $request->type;
    $job->cat_id            = $request->category;
    $job->experience        = $request->experience;
    $job->city_id           = $request->city;
    $job->total_vacancies   = $request->vacancies;
    $job->job_type_id       = $request->job_type;
    $job->salary            = $request->salary;
    $job->last_date         = Carbon::createFromFormat('d/m/Y', $request->last_date);
    $job->shift             = $request->shift;
    $job->status            = 'active';

    $job->save();

    if($request->add_type == 'option2'){
        Session::flash('job_id', $job->id);
         return redirect()->route('theoption2')->with( 'job', $job );
    } else {
        return redirect()->back()->withSuccess('Updated.');
    }   
}

如果更改Posts::findOrFail($id) 不会显示任何错误。

任何关于为什么它总是更新最后一个条目的信息的帮助将不胜感激! 先感谢您。

【问题讨论】:

  • 显示你如何初始化调用更新函数的表单。
  • 更改此:$job = Posts::find($id); 为:$job = Posts::where(['id' =&gt; $id])-&gt;first();
  • 你能发一下你的Post模特吗?
  • @AntonyMN 我这样初始化表单:{{ Form::open(['url'=&gt; ['employer', 'job', $job-&gt;id, 'edit'], 'method' =&gt; 'POST']) }}
  • @EmmanuelHD 不幸的是,它具有相同的行为,它不断更新数据库中的最后一个条目。

标签: php mysql laravel laravel-5


【解决方案1】:

问题在于您发送到帖子的 id,请问 id 来自哪里?我会保存每条评论及其 id 以保存并在发布时返回它,如果你可以发布你的 html 或 id 来自哪里,这将有助于检查你的程序

【讨论】:

  • 它正在调用{{ Form::open(['url'=&gt; ['employer', 'job', $job-&gt;id, 'edit'], 'method' =&gt; 'POST']) }},我刚刚用html更新了帖子,谢谢!
  • @EmekaOkafor 是正确的 - 您发送到 updateJob 函数的 ID 已关闭。请检查正在发送的 ID 并确认它与您单击的 ID 相同。请向我们展示按钮 href 的 HTML 以及编辑表单。我想您在创建新的Job 时向我们展示了该表单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
相关资源
最近更新 更多