问题描述:

I'm creating an application in which the the user uploads images that will later be dynamically changed. They populate different pre-coded groups such as base color, eye color, etc. with different options. For instance, under eye color, they may create a field called Light Green and another field named Blue that they would then upload images for.

The page where they upload is dynamically generated from the database using the pre-set groups like so:

<div class="center"><h2>Male</h2></div>

@foreach ($species->eyeColors as $m)

<div class="form-group{{ $errors->has('eye.m.' . strtolower(str_replace(" ", "_", $m->name))) ? ' has-error' : '' }}">

<label class="col-md-4 control-label">{{ $m->name }}</label>

<div class="col-md-6">

<input type="file" class="form-control" name="eye.m.{{strtolower(str_replace(" ", "_", $m->name))}}">

@if ($errors->has('m_' . strtolower(str_replace(" ", "_", $m->name))))

<span class="help-block">

<strong>{{ $errors->first('m' . strtolower(str_replace(" ", "_", $m->name))) }}</strong>

</span>

@endif

</div>

</div>

@endforeach

</div>

Each trait has male and female counterparts that are uploaded separately. I'd like to have the code upload the images to the server under the name given in the database (strtolower(str_replace(" ", "_", $m->name))) but I'm not sure how to loop through each specific group in the form.

I tried using the same syntax as you'd use with the array Validator:

 // Male Eye Colors

foreach ($request->items['eye.m.*'] as $k) {

$file = $k;

$name = $k->getClientOriginalName();

$path = 'images/assets/bases/' . $species->name . '/male/eyes';

$file->move($path, $name);

}

but got the following error: Undefined index: eye.m.* So it appears I can't loop through same as the Laravel Validator could. This also would save the name as the file uploaded and not the one established in the database, but i'm not sure how to extract that data from the fields.

相关阅读:
Top