首页 > 解决方案 > 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

检索时的图像

在此处输入图像描述

标签: phphtmllaraveluploaddata-retrieval

解决方案


删除第一个 foreach 解决了这个问题!

@foreach($car->carevidence as $evidence)
    <img src="{{ asset('image/'.$evidence->car_images) }}" target="_blank" height="60px" width="60px" />
@endforeach

推荐阅读