简体   繁体   中英

Laravel 5.2 all data reset when update 1 single data

I have table dosen and I want to update data to "statusdosen" column.

I have url : ...admin/dosen/status

When I click save, the page redirect to ..admin/dosen and all data in that row has been reset (empty).

This is my view

{!! Form::model($value, ['route' => ['admin.dosen.update', $value->user_id], 'method' => 'PUT']) !!}
  <br>
 {!! Form::select('statusdosen', array('1' => 'Dikampus', '0' => 'Tidak Dikampus'), null, ['placeholder' => 'Pilih Status'], ['class' => 'form-control']) !!} 
  <br><br>
 !! Form::button('<i class="fa fa-check-square-o"></i> Simpan', ['type' => 'submit', 'class' => 'btn btn-primary btn-sm'] )  !!}
{!! Form::close() !!}

Method :

 public function status()
{
    $dosen = Dosen::paginate(10);
    return view('admin/dosen.status', compact('dosen'));
}

public function update($id)

{
    $dosenUpdate = Request::only(['nipy', 'namadosen', 'alamatdosen', 'notelpdosen', 'tempatlahirdosen', 'tanggallahirdosen']);
    $user = User::find($id);
    $user->dosen()->update($dosenUpdate);
    return redirect('admin/dosen')->with('message', 'Data berhasil diubah!');

}

what i want is after i click save it not redirect to other page. do i must create new update method data for 'statusdosen'?

Ok that explains evreything

$user = User::find($id);

Actually this is same as

Update 
      dosen
Set
  Columnname1=$data, Columnnam2=data2
Where
     user_id = $id

This means all rows with this user_id will be updated.

You should be doing like below

public function update($id) { 
    $dosenUpdate = Request::only(['nipy', 'namadosen', 'alamatdosen', 'notelpdosen', 'tempatlahirdosen', 'tanggallahirdosen']);
     //$user= User::find($id); $user->dosen()->update($dosenUpdate);
    $dosen = dosen::find($id); // parameter $id must be id of the dosen you want to update
    $dosen->update(dosenUpdate);
    return back()->with('message', 'Data berhasil diubah!');
}

The return back() will take you back to the previous page but all input value will not be there as the page is being reloaded.

If you do want to keep the data you've two ways

  1. Use return back()->withInput() and store the data to the corresponding input using old .
  2. Use AJAX, and return a response as json.

    Eg:

    return response()->json(['message'=>'Data berhasil diubah!');

I think you are looking for something along the line of this

public function update(Request $request, $id) {
    // Make sure the value is true/1 or false/0
    $this->validate($request, ['statusDosen' => 'boolean']);

    // Retrieve only the status
    $statusDosen = $request->get('statusDosen');

    // Update user->dosen()
    $user = User::find($id);
    $user->dosen()->update($statusDosen);

    // Redirect back to the page you came from with your message
    return redirect()->back()->with('message', 'Data berhasil diubah!');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM