编辑表时,它将更新数据库中的最后一个条目,而不是所选的条目

时间:2019-01-10 22:25:42

标签: php mysql laravel laravel-5

我有一个可以包含多个帖子的表格,比方说,我去了edit any random post

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

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)不会显示任何错误。

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

1 个答案:

答案 0 :(得分:0)

问题在于您发送给帖子的ID,请问ID是从哪里来的?我会保存每个评论及其ID的信息,并在发布时返回它,如果您可以发布HTML或ID来自何处,那将有助于检查程序