gpt4 book ai didi

php - 如何从多个表中获取数据并在laravel中使用 '->with()'发送到html

转载 作者:行者123 更新时间:2023-11-29 06:02:07 26 4
gpt4 key购买 nike

我必须要表“用户”和“部门”,我想从这两个表中获取数据并以 html 格式放入。这是我的代码,它不起作用,因为其中缺少一些东西。这是 Controller 的代码:

 public  function showProfileForm($id){
$user = DB::table('users')->where('id','=',$id)->get();

$dpt = DB::table('departments')->orderBy('department_name','asc')->get();
return view('profile.showProfile')->with(['selDpt', $dpt, 'user', $user]);
}

最佳答案

get() 方法将数据检索为多维数组。因此,当您只检索一个数组时,您必须在查询结束时使用 first() 方法。所以只需将查询从

$user = DB::table('users')->where('id','=',$id)->get();
to
$user = DB::table('users')->where('id','=',$id)->first();

现在,您必须通过数组 传递数据。所以只需更改行

return view('profile.showProfile')->with(['selDpt', $dpt, 'user', $user]);
To
return view('profile.showProfile')->with(['selDpt' => $dpt, 'user' => $user]);

shoProfile.blade.php

您必须使用 $selDpt$user 变量来使用或回显数据。希望它会起作用。

了解更多信息 https://laravel.com/docs/5.4/views#passing-data-to-views

关于php - 如何从多个表中获取数据并在laravel中使用 '->with()'发送到html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44165203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com