编辑表时,它将更新数据库中的最后一个条目,而不是所选的条目 [英] When editing a table, it updates the last entry in the DB instead of the selected one

查看:77
本文介绍了编辑表时,它将更新数据库中的最后一个条目,而不是所选的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以包含多个帖子的表,比方说,我去编辑任何随机变量发布.

I have a table that can have multiple posts, let’s say that I go to edit any random post.

显示正确的有关该帖子的信息,但是一旦我更改数据,然后单击我的按钮以保存信息并更新帖子,它更新最后创建的帖子,而不是所选的帖子.

It shows me with the correct information about said post, however once I change the data and click on my button to save the information and update the post, it updates the last post that was created instead of the chosen one.

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

Posts would be the table with the column id as my primary key using an auto_increment for identification.

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

If changed Posts::findOrFail($id) does not display any error.

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

Any help on why it's always updating the information on the last entry would be greatly appreciated! Thank you in advance.

推荐答案

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

the problem is with the id you are sending to the post, please where is the id coming from? i would have saved each comment with its id to be saved and return it when posting, if you can post your html or where the id is coming from, that will help check your program

这篇关于编辑表时,它将更新数据库中的最后一个条目,而不是所选的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆