【问题标题】:Trying to sort and get collection of data尝试对数据进行排序和收集
【发布时间】:2020-06-17 14:57:02
【问题描述】:

我正在尝试通过从reports 表中传递 id 来从users 表中获取用户的数据
我想显示reason 的名称reported_userreported_by 的名称
当我运行dd($report->all()); 时,它会显示:-

array:2 [▼
  0 => App\Report {#1057 ▼
    #fillable: array:3 [▼
      0 => "reported_by"
      1 => "reported_user"
      2 => "reason"
    ]
    #connection: "sqlite"
    #table: "reports"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:6 [▶]
    #original: array:6 [▶]
    #changes: []
    #casts: []
    #classCastCache: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #guarded: array:1 [▶]
  }
  1 => App\Report {#1056 ▶}
]

我也面临此错误:-
此集合实例上不存在属性 [reported_user]。
$reportedBy 也是如此

这是我的控制器

public function reports()
    {
        $report = Report::all();

        $reportedUser = DB::table('users')
        ->where('id', '=', $report->reported_user)
        ->get();

        $reportedBy = DB::table('users')
        ->where('id', '=', $report->reported_by)
        ->get();



        return view('admin.report', compact('report'));
}

这是我的报告表:-

public function up()
    {
        Schema::create('reports', function (Blueprint $table) {
            $table->id();
            $table->integer('reported_by')->unsigned();
            $table->integer('reported_user')->unsigned();
            $table->string('reason');
            $table->timestamps();
        });
    }

谁能告诉我这是否是执行此任务的正确方法以及为什么会出现这样的错误。

【问题讨论】:

    标签: mysql database laravel eloquent


    【解决方案1】:

    您正在尝试从集合而不是对象的单个实例中绘制属性。

    集合$report 不是单个对象,它是报表的集合。该集合的每个报告都将具有属性reported_user,而不是整个集合。

    要修复,您可以从数据库中获取单个报告:

     $report = Report::first();
    

    您可以在哪里访问此对象的reported_user 字段,或者您可以循环查看您从原始集合中提取的报告:

    foreach($report as $r){
      $reportedUser = DB::table('users')
        ->where('id', '=', $r->reported_user)
        ->first();
     }
    

    建议将原始报表集合命名为$reports,以防止混淆并表明它是一个集合,而不是单个报表对象。

    另外 - 请注意我在循环中做了同样的事情 - 使用 first() 方法而不是 get() 获取一个对象,而不是一个集合。

    编辑

    上面是一个简单的例子来解释。要防止多次 DB 调用,您还可以这样做:

    $reports = Report::pluck('reported_user');
    
    $reportedUsers = DB::table('users')
       ->whereIn('id', $reports)
       ->get();
    

    您现在拥有所有reported_user 的完整详细信息的集合。然后,您可以循环该 $reportedUsers 集合并获取每个集合背后的详细信息。

    【讨论】:

    • 这不会只给我一个用户吗?因为我想显示所有reported_user和reported_by的详细列表
    • 是的,每个循环一个用户。问题是您最初使用$report = Report::all(); 绘制了一个集合,并试图从整个集合中获取报告的用户。如果您将上述循环与该集合一起使用,并将每个 $reportedUser 添加到用户数组或集合中,您将获得所有报告用户的列表。你很接近 - 它会起作用 - 你只需要通过循环而不是 $reportedUser 抓取中的单个对象。当然,您可以收紧此代码以获取所有 ID - 让我编辑答案并向您展示。一分钟
    • 是的,这一切都说得通。但通常当我在集合中运行 dd 时,我得到的是值,但在这种情况下 dd($report->all()); 我看不到任何值,为什么会这样?
    • 你介意帮我解决另一个问题吗,如果是,那么这里是链接:- stackoverflow.com/questions/62397745/…
    • 很高兴为您提供帮助。我也会看看另一个问题......不知道我是否会对此有所了解,但我会读一读!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2022-07-07
    • 2023-03-22
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多