通过关系获取图像

Get Images via relationship

charts,我想通过关系获取 trade_id 的每个图像。

例如trade_id为62,则从chartstable中获取trade_id中所有有62的图片。

charts table 结构:

Trade 型号:

    public function chart()
{
    return $this->hasMany('App\Chart');
}

@if($trades)
     <tbody>
     @foreach($trades as $trade)
           <tr>
              <td> <img src="{{$trade->chart->file }}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%"> </td>
          </tr>
     @endforeach
     </tbody>
            @else
            <h1>No trades</h1>
@endif

输出有错误:

Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$file (View: C:\xampp\htdocs\try\ytl\resources\views\member\add-single-trade\index.blade.php)

控制器:

    public function index()
{
    $user_id = Auth::user()->id;
    $trades = Trade::where('user_id','=',Auth::id())->get();
    return view('member.add-single-trade.index', compact('trades'));

}

DD($Trade): 结果

Trade {#541 ▼
  #dates: array:1 [▼
    0 => "deleted_at"
  ]
  #fillable: array:10 [▼
    0 => "user_id"
    1 => "symbol_id"
    2 => "is_action"
    3 => "rate"
    4 => "tradedate"
    5 => "note"
    6 => "trade_id"
    7 => "exchange_id"
    8 => "market_id"
    9 => "stoploss"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:14 [▼
    "id" => 3
    "user_id" => 1
    "symbol_id" => 5
    "exchange_id" => 1
    "market_id" => 1
    "is_action" => 1
    "rate" => 5641
    "tradedate" => "2018-04-05 00:00:00"
    "note" => ""
    "created_at" => "2018-04-18 13:00:23"
    "updated_at" => "2018-04-18 13:00:23"
    "deleted_at" => null
    "quantities" => 0
    "stoploss" => 0
  ]
  #original: array:14 [▼
    "id" => 3
    "user_id" => 1
    "symbol_id" => 5
    "exchange_id" => 1
    "market_id" => 1
    "is_action" => 1
    "rate" => 5641
    "tradedate" => "2018-04-05 00:00:00"
    "note" => ""
    "created_at" => "2018-04-18 13:00:23"
    "updated_at" => "2018-04-18 13:00:23"
    "deleted_at" => null
    "quantities" => 0
    "stoploss" => 0
  ]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  +exists: true
  +wasRecentlyCreated: false
  #forceDeleting: false
}

型号:

public function charts() {
   return $this->hasMany('App\Chart');
}

查看:

 @foreach($trades as $trade)
       <tr>
          <td>@foreach($trade->charts->all() as $chart) <img src="{{$chart->file }}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%">@endforeach</td>
      </tr>
 @endforeach

将关系添加到 foreach 中。

 @foreach($trades->chart as $trade)
           <tr>
              <td> <img src="{{$trade->file }}"> </td>
          </tr>
     @endforeach

您必须遍历图表:

@if($trades)
     <tbody>
     @foreach($trades as $trade)
        @foreach($trade->chart as $chart)
           <tr>
              <td> <img src="{{$chart->file }}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%"> </td>
          </tr>
        @endforeach
     @endforeach
     </tbody>
            @else
            <h1>No trades</h1>
@endif

还有一件事,图像的路径将不起作用,假设您在 public 中的图像文件夹中有图像,您必须更改:

<img src="{{$chart->file}}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%">

<img src="{{Storage::get('public/charts/', $chart->file) }}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%">