php - 如何连接 Blade 文件中的表

标签 php mysql laravel-5.4

我是 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 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" 和 “从 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));
}

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

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

关于php - 如何连接 Blade 文件中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47114325/

相关文章:

php - vtiger crm : single csv file to multiple database table

php - 如何更改 SQLite 数据库中的值?

laravel - 更新 Laravel 5.4。到 5.5 中断验证响应

javascript - Laravel 和 axios 提交后清除表单

java - 在准备好的语句中使用 ORDER BY

php - 如何在 Laravel 5.4 中对 Json 输出进行分组?

php - 在 Safari/Mac 中需要有关 jQuery/AJAX 调用的帮助

php - PHP的socket_recv MSG_PEEK标志是什么意思?

java - Java .NET与PHP Rails

php - 如何将 implode() 的数组元素结果插入每一行?