php - Laravel - 仅检索属于 id 的图像
问题描述
从 mysql 检索所有图像,而不是假设只检索属于表单 id 的图像。有什么建议么?
汽车模型
class Cars extends Model
{
public $table = 'cars';
protected $primaryKey = 'id';
public function carevidence()
{
return $this->hasMany('App\CarEvidence', 'cars_id', 'id');
}
}
车证模型
class CarEvidence extends Model
{
protected $table = 'carevidence';
protected $fillable = [
'cars_id',
'car_images',
];
public function car()
{
return $this->belongsTo('App\Cars', 'cars_id', 'id');
}
}
存储控制器
if ($post->save()) {
if ($request->hasFile('carEvidence')) {
$files = $request->file('carEvidence');
foreach ($files as $file) {
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move(public_path('/image'), $filename);
CarEvidence::create([
'cars_id' => $post->id,
'car_images' => $filename
]);
}
}
}
检索控制器
public function edit($id)
{
$cars = Cars::with('carevidence')->get();
return view(
'Validation.validation',
compact('validations', 'validators', 'departments', 'defects', 'cars')
);
}
检索视图
@foreach($cars as $car)
@foreach($car->carevidence as $evidence)
<img src="{{ asset('image/'.$evidence->car_images) }}" target="_blank" height="60px" width="60px" />
@endforeach
@endforeach
检索时的图像
解决方案
删除第一个 foreach 解决了这个问题!
@foreach($car->carevidence as $evidence)
<img src="{{ asset('image/'.$evidence->car_images) }}" target="_blank" height="60px" width="60px" />
@endforeach
推荐阅读
- flutter - Flutter:onEdittingComplete和onSubmitted之间的TextField区别
- python - 如何在加权直方图中获取特定 bin 的数据点?
- php - Codeigniter 输入到有条件的数据库
- c - 没有crontab,我如何每天发送信号?
- excel - excel翻译函数为公式
- php - 如何将多个查询的结果存储在变量中并使用 php.ini 访问它。(程序风格)
- reactjs - 在 Next.js 中使用 Google ReCaptcha v3
- rust - 0usize 代表什么,为什么它没有在原语中列出?
- kotlin - kotlin 中计算器的一元和二元减号运算符
- java - 我无法让 CompletableFuture supplyAsync 方法返回任何东西