Laravel-仅检索属于该ID的图像

问题描述 投票:0回答:1

取而代之的是从mysql检索所有图像,它假定仅检索属于表单ID的图像。有什么建议吗?

汽车模型

class Cars extends Model
{
    public $table = 'cars';

    protected $primaryKey = 'id';

    public function carevidence()
    {
        return $this->hasMany('App\CarEvidence', 'cars_id', 'id');
    }

}

CarEvidence Model

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

检索时的图像

enter image description here

php html laravel upload data-retrieval
1个回答
0
投票

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

@foreach($car->carevidence as $evidence)
    <img src="{{ asset('image/'.$evidence->car_images) }}" target="_blank" height="60px" width="60px" />
@endforeach
© www.soinside.com 2019 - 2024. All rights reserved.