Laravel - 仅检索属于该 id 的图像
Laravel - Retrieve only images that belong to the id
从 mysql 中检索所有图像,而不是假设仅检索属于表单 ID 的图像。有什么建议吗?
汽车型号
class Cars extends Model
{
public $table = 'cars';
protected $primaryKey = 'id';
public function carevidence()
{
return $this->hasMany('App\CarEvidence', 'cars_id', 'id');
}
}
CarEvidence 模型
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 已解决问题![=11=]
@foreach($car->carevidence as $evidence)
<img src="{{ asset('image/'.$evidence->car_images) }}" target="_blank" height="60px" width="60px" />
@endforeach
从 mysql 中检索所有图像,而不是假设仅检索属于表单 ID 的图像。有什么建议吗?
汽车型号
class Cars extends Model
{
public $table = 'cars';
protected $primaryKey = 'id';
public function carevidence()
{
return $this->hasMany('App\CarEvidence', 'cars_id', 'id');
}
}
CarEvidence 模型
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 已解决问题![=11=]
@foreach($car->carevidence as $evidence)
<img src="{{ asset('image/'.$evidence->car_images) }}" target="_blank" height="60px" width="60px" />
@endforeach