如何连接 blade 文件中的表

how to join tables in blade file

我是 laravel 的新手,正在构建一个简单的应用程序。我在我的控制器中使用这个功能:

  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));
}

但是当我在网络浏览器中调出视图时,出现以下错误:

Undefined variable: user and only the query for users is shown to be run. "select * from users where id = '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" 和 "select * from csaves where id = '{{$user->id}}'"

限制我的 csaves table 以显示特定用户 ID 的值的最佳方法是什么?

这是因为用户变量$user被注释:

改变这个:

//  $user= Auth::user()->id;

进入: $user= Auth::user()->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));
}

我看到你的代码,你使用了一些错误,比如:

  1. 您没有声明 $user 变量
  2. 不调用和使用 Csafe 控制器
  3. 同时使用get()方法和first()方法。一次只能使用其中的任何一个。