When editing a table, it updates the last entry in the DB instead of the selected one

30
January 11, 2019, at 2:10 PM

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 would be the table with the column id as my primary key using an auto_increment for identification.

Controller:

 /**
 * 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.');
    }   
}

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.

Answer 1

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

READ ALSO
Searching a table for like matches based on 2 different vars

Searching a table for like matches based on 2 different vars

I have a form that submits 2 values to the post target:

50
Ordering by the order of values in a SQL IN() clause

Ordering by the order of values in a SQL IN() clause

I am wondering if there is away (possibly a better way) to order by the order of the values in an IN() clause

46
Why my function is not returning a value using another function as input?

Why my function is not returning a value using another function as input?

I have written two functionsThe first one utilized a select query and returns a value

21
Display results from multiple groupings in one query

Display results from multiple groupings in one query

I have a queries similar to below:

37