如何处理我的数据库ID,当我输入ID数据以继续已删除的数据。

huangapple go评论103阅读模式
英文:

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,当我输入ID数据以继续已删除的数据。

当我删除它时,ID 是 1,ID 2 不会变成 ID 1,而仍然是 ID 2:

如何处理我的数据库ID,当我输入ID数据以继续已删除的数据。

之后,我添加了新数据,ID 会跟随现有表格:

如何处理我的数据库ID,当我输入ID数据以继续已删除的数据。

英文:

This is my controller code

&lt;?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(&#39;data-karyawan&#39;, [&#39;data&#39; =&gt; $data]);
}
public function create()
{
$data = Datakaryawan::all();
return view(&#39;data-karyawan-add&#39;, [&#39;data&#39; =&gt; $data]);
}
public function store(Request $request)
{
$data = Datakaryawan::create($request-&gt;all());
if ($data) {
Session::flash(&#39;status&#39;, &#39;success&#39;);
Session::flash(&#39;message&#39;, &#39;Berhasil menambah data karyawan&#39;);
}
return redirect(&#39;data-karyawan&#39;);
}
public function edit(Request $request, $id)
{
$id = Datakaryawan::findorfail($id);
return view(&#39;data-karyawan-edit&#39;, [&#39;id&#39; =&gt; $id]);
}
public function update(Request $request, $id)
{
$id = Datakaryawan::findorfail($id);
$id-&gt;update($request-&gt;all()); 
return redirect(&#39;/data-karyawan&#39;);
}
public function destroy($id)
{
Datakaryawan::where(&#39;id&#39;, $id)-&gt;delete();
return redirect()-&gt;to(&#39;/data-karyawan&#39;)-&gt;with(&#39;success&#39;, &#39;Berhasil menghapus data karyawan&#39;);
}
}

This is the default view

enter image description here

When I delete it, the id is 1, the id 2 doesn't become id 1, but it stays id 2

enter image description here

After that I added new data, and the id instead follows the existing table

enter image description here

答案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-&gt;id. Just change that to $loop-&gt;index and you should get the behaviour you want.

when you request delete/update the data of a row don't use this $loop-&gt;index but use the actual id, you can just add the id in a data attribute like for example:

&lt;div class&quot;row&quot; data-id=&quot;{{$item-&gt;id}}&quot;...

huangapple
  • 本文由 发表于 2023年8月10日 21:00:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定