英文:
How do I deal with my database id when I enter the id data to continue the data that has been deleted
问题
这是我的控制器代码:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Datakaryawan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class DatakaryawanController extends Controller
{
public function index()
{
$data = Datakaryawan::paginate(4);
return view('data-karyawan', ['data' => $data]);
}
public function create()
{
$data = Datakaryawan::all();
return view('data-karyawan-add', ['data' => $data]);
}
public function store(Request $request)
{
$data = Datakaryawan::create($request->all());
if ($data) {
Session::flash('status', 'success');
Session::flash('message', 'Berhasil menambah data karyawan');
}
return redirect('data-karyawan');
}
public function edit(Request $request, $id)
{
$id = Datakaryawan::findorfail($id);
return view('data-karyawan-edit', ['id' => $id]);
}
public function update(Request $request, $id)
{
$id = Datakaryawan::findorfail($id);
$id->update($request->all());
return redirect('/data-karyawan');
}
public function destroy($id)
{
Datakaryawan::where('id', $id)->delete();
return redirect()->to('/data-karyawan')->with('success', 'Berhasil menghapus data karyawan');
}
}
这是默认视图:
当我删除它时,ID 是 1,ID 2 不会变成 ID 1,而仍然是 ID 2:
之后,我添加了新数据,ID 会跟随现有表格:
英文:
This is my controller code
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Datakaryawan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class DatakaryawanController extends Controller
{
public function index()
{
$data = Datakaryawan::paginate(4);
return view('data-karyawan', ['data' => $data]);
}
public function create()
{
$data = Datakaryawan::all();
return view('data-karyawan-add', ['data' => $data]);
}
public function store(Request $request)
{
$data = Datakaryawan::create($request->all());
if ($data) {
Session::flash('status', 'success');
Session::flash('message', 'Berhasil menambah data karyawan');
}
return redirect('data-karyawan');
}
public function edit(Request $request, $id)
{
$id = Datakaryawan::findorfail($id);
return view('data-karyawan-edit', ['id' => $id]);
}
public function update(Request $request, $id)
{
$id = Datakaryawan::findorfail($id);
$id->update($request->all());
return redirect('/data-karyawan');
}
public function destroy($id)
{
Datakaryawan::where('id', $id)->delete();
return redirect()->to('/data-karyawan')->with('success', 'Berhasil menghapus data karyawan');
}
}
This is the default view
When I delete it, the id is 1, the id 2 doesn't become id 1, but it stays id 2
After that I added new data, and the id instead follows the existing table
答案1
得分: 1
这是数据库的行为方式。
我猜你有一个包含foreach循环的刀片文件,在其中你获取特定项,然后调用类似$item->id
的东西。只需将其更改为$loop->index
,你应该可以获得你想要的行为。
当你请求删除/更新行的数据时,请不要使用$loop->index
,而是使用实际的ID,你可以将ID添加到数据属性中,例如:
<div class="row" data-id="{{$item->id}}"...
英文:
This is how the database behaves.
I guess you have a blade file with a foreach loop where you get the specific item and then you call something like $item->id
. Just change that to $loop->index
and you should get the behaviour you want.
when you request delete/update the data of a row don't use this $loop->index
but use the actual id, you can just add the id in a data attribute like for example:
<div class"row" data-id="{{$item->id}}"...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论