从charts
开始,我想通过关系获取trade_id
的每张图片。
例如,如果trade_id
为62,则从charts
表中获取trade_id
中包含62的所有图片。
charts
表格结构:
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
OUTPUT有错误:
未定义属性:Illuminate \ Database \ Eloquent \ Relations \ HasMany :: $ file(查看: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
}
答案 0 :(得分:0)
型号:
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
答案 1 :(得分:0)
在foreach中添加关系。
@foreach($trades->chart as $trade)
<tr>
<td> <img src="{{$trade->file }}"> </td>
</tr>
@endforeach
答案 2 :(得分:0)
你必须遍历图表:
@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
还有一件事情,图像的路径将无法正常工作,假设你在公共图像文件夹中有图像文件夹,你必须乱用:
<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%">