Laravel 获取图片数组并自定义存储

23. January 2018 Laravel 0

控制器:

$schooldetail = CollegeDetail::find($id);
if ($request->hasFile(‘imgs’)) {
$imgs = $request->file(‘imgs’);
foreach ($imgs as $key => $value) {
$path = $value->storeAs($id . ‘/photos’, Uuid::uuid1()->getHex() . ‘.jpg’, “college”);
$img[] = explode(‘/’, $path)[2];
}
if (empty($schooldetail->imgs)) {
$schooldetail->imgs = implode(‘,’, $img);
} else {
$schooldetail->imgs = $schooldetail->imgs . ‘,’ . implode(‘,’, $img);
}
}

视图:

<form action=”/updatecollegedetail” id=”testform” enctype=”multipart/form-data” method=”post”>
{{ csrf_field() }}
<input name=”id”>
<input name=”imgs[]” type=”file”>

<input name=”imgs[]” type=”file”>
<button type=”submit”>Save</button>

</form>