【问题标题】:How to catch 1 colomn (id) on table in edit form如何在编辑表单中捕获表上的 1 列(id)
【发布时间】:2020-01-10 03:28:01
【问题描述】:

我有 1 个表格数据。我在这张桌子上有功能编辑。在这个编辑表单中,我选择了下拉菜单来显示数据库中的 pluck 。 我有 3 张桌子。这个名字是aduan,ipsrs,teknisi

结构图

id 
user_id
ipsrs_id
teknisi_id
aduan
etc.....

ipsrs 表

id
nama_bagian
etc....

泰克尼西表

id
ipsrs_id
nama_teknisi
etc...

这是我的控制器:

public function index(Request $request)
{
    $ipsrs = DB::table('ipsrs')->pluck('nama_bagian','id');


    $belum_kerjakan = Aduan::with('users')->where('status','Belum Dikerjakan')->get();

    $teknisi        = Teknisi::where('ipsrs_id' , 1)->pluck('nama_teknisi', 'id'); 
    $dalam_proses   = Aduan::with('users')->where('status','Sedang Dikerjakan')->get();
    $selesai        = Aduan::with('users')->where('status','Selesai')->get();
    return view('admin.admin_dashboard',[
        'belum_dikerjakan' => $belum_kerjakan,
        'dalam_proses'     => $dalam_proses,
        'selesai'          => $selesai,
        'ipsrs'            => $ipsrs,
        'teknisi'          => $teknisi,
    ]);


}

例如这个变量 $belum_dikerjakan 正在显示表格数据,我在这个表格上有功能编辑(模式)。

但这我不知道如何捕获数据 (ipsrs_id) 以在 pluck 中设置 where 子句。我想将 1 更改为 ipsrs_id 表格,但是如何?

【问题讨论】:

    标签: laravel pluck


    【解决方案1】:

    如果我理解了问题,那么这里就是答案

    pull only the ids from ipsrs table and pass to Teknisi table whereIn method
    
    $ipsrsIds = DB::table('ipsrs')->pluck('id')->toArray();
    
    $teknisi = Teknisi::whereIn('ipsrs_id' , $ipsrsIds)->pluck('nama_teknisi', 'id'); 
    

    【讨论】:

    • 此输出 ipsrs_id 只是 '1' ,但如果我在此查询中使用 where 将显示如下,(SQL: select nama_teknis, id from teknisi where ipsrs_id in (1, 1, 1, 1, 1, 2)) 我删除 1 个字符以显示结果查询。
    • 但如果我只在显示的地方使用: (SQL: select nama_teknis, id from teknisi where ipsrs_id = 1)
    【解决方案2】:
    public function edit($id)
        {
            $category =Category::findOfFail($id);
            return view('admin.category.edit',compact('category'));
        }
    
    
    public function update(Request $request, $id)
        {
            $this->validate($request,[
                'name' => 'required|unique:categories'
            ]);
            $category = Category::find($id);
            $category->name = $request->name;
            $category->slug = str_slug($request->name);
            $category->save();
            Toastr::success('Category Successfully Updated','Success');
            return redirect()->route('admin.category.index');
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多