我是 laravel 的新手,正在构建一个简单的应用程序。我在我的 Controller 中使用这个函数:
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
//
//return view('adminlte::home');
// $user= Auth::user()->id;
$csafe = csafe::where('id','=', $user->id)->get()->first();
return view('csaves.show', array('csafe' => $csafe));
}
但是当我在网络浏览器中打开 View 时,出现以下错误:
Undefined variable: user and only the query for users is shown to be run. "select * from
users
whereid
= '1' limit 1" I also tried to use $csafe = csafe::where('id','=', '{{$user->id}}')->get()->first(); this time it again fails erroring: Trying to get property of non-object
在我的查询中它显示:
"select * from users
where id
= '1' limit 1"
和
“从 csaves
中选择 *,其中 id
= '{{$user->id}}'”
限制我的 csaves 表以显示特定用户 ID 的值的最佳方法是什么?
最佳答案
你可以这样使用它:
use App\User;
use App\Csafe;
public function index()
{
$user = Auth::user();
$csafe = Csafe::where('userid','=', $user->id)->first();
return view('csaves.show', array('csafe' => $csafe));
}
我看到你的代码,你使用了一些错误,比如:
- 你没有声明 $user 变量
- 不调用和使用 Csafe Controller
- 同时使用 get() 方法和 first() 方法。一次只能使用其中的任何一个。
关于php - 如何连接 Blade 文件中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47114325/