在Laravel的查询中没有显示关系

时间:2017-10-17 13:54:05

标签: php laravel laravel-5

我的模型Item跟随

public function report()
{
    return $this->hasMany('App\Report', 'item_id','id');
}

public function details( $item_id ) { $flags = Item::find($item_id)->report->unique('user_id'); return view('flags.details', compact('flags')); } 模型中

{{ dd(collect($flags)) }}

在控制器中

items

为什么当我在我的view.blade中Item::find($item_id)时,即使我查询<?php namespace App; use Illuminate\Database\Eloquent\Model; class Report extends Model { protected $table = 'reports'; protected $primaryKey = 'report_id'; protected $fillable = [ 'item_id', 'report_body', 'user_id', 'report_reason' ]; public function reportedItem() { return $this->belongsTo('App\Item', 'item_id', 'id'); } public function user() { return $this->hasOne('App\User', 'id', 'user_id'); } } ,我也从>>> my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 表中看不到任何内容?

dd输出

enter image description here

n

3 个答案:

答案 0 :(得分:0)

试试这个

$flags = Item::find($item_id)->with('report')->get()

答案 1 :(得分:0)

一种方法是在你的控制器中使用这样的东西

public function details( $item_id )
{
    $flags = Report::with('reportedItem')->where('item_id', '$item_id')->get();

答案 2 :(得分:0)

以下应该有效

{% extends 'base.html' %}

<!DOCTYPE html>
<html>

    {% block head %}
  <head>
    <meta charset="utf-8">
    <title>See patient</title>
  </head>

  {% endblock %}
  <body>


{% block body %}

<div class = "row" style = "margin-top: 80px;">

  <div class="col-sm" >

      <nav class =" "  >

        <ul class = "nav flex-column">

                         <li class = "nav-item"><a class = "nav-link" href = "{% url 'nesting:nesting'%}" ><small>Create  Identity </small></a></li>
                         <!-- <li class = "nav-item"><a class = "nav-link" href = "#"><small>Update </small></a></li>
                         <li class = "nav-item"><a class = "nav-link" href = "#"><small>Move to Trash</small> </a></li> -->
          </ul>

      </nav>
  </div>

  <div class = "col-sm" >

{% for Identity in Identities %}

    <div class = "card " style = "margin-top: 20px;" >

            <ul class = "list-group list-group-flush">
               <li class = "list-group-item"> <small class = "text-muted">Created On : {{Identity.Timestamp}}</small></li>

              <li class = "list-group-item"><a class = "nav-link" href = "{% url 'nesting:Medical_History_nest' %}" >{{Identity.First_Name}}  {{Identity.Last_Name}} </a> <p>NIS:  {{ Identity.NIS }}</p></li>
              <li class = "list-group-item"><p> <small>Contact: {{ Identity.Contact}}</small></p></a></li>
              <li class = "list-group-item"><p> <small>Birthday: {{ Identity.date_of_birth}}</small></p></a></li>
              <li class = "list-group-item"><a class = "nav-link" href = "{%url 'nesting:Symptoms_nest_list'%}" ><small>Create Medical State </small></a></li>

            </ul>
    </div>
          {% endfor %}
  </div>

<div class="col-sm" ></div>

</div>

{% endblock %}
  </body>
</html>

然后当你dd($ flags)时,查找“关系”标签,在里面你会看到信息。