Because:
if ($request->hasFile('file')) { // <-- this return false
My data(title and images name) are not saved in database and images not uploaded to public directory.I got data saved message but my data is not saved in database . i have a form with two fields title input field and browse image field
.i have add more button ,from which javascript generates again two more fields each time user click that button.
public function postUpload(Request $request)
{
//dd($request->all());
foreach($request->get('textbox') as $textbox) {
//file upload code
if ($request->hasFile('file')) {
$destinationPath = public_path() . '/uploads/photos'; // upload path
$extension = $request->file('file')->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renamaing image
$request->file('file')->move($destinationPath, $fileName); // uploading file to given path
$data = new Item;
$data->title = $textbox['name'];
$data->file= $fileName;
$data->save();
}else{
}
}
return "data saved";
}
form
<form action="{{url('upload')}}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label>
<input type='textbox' id='textbox1' name="textbox[1][name]">
<input type='file' id='desc1' name="textbox[1][file]">
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type="submit" value="submit">
</form>
model
class Item extends Model
{
protected $table = 'items';
public $fillable = ['title','file'];
}
ondd($request->all())i got
array:2 [▼
"_token" => "e1JhMKXZmJGJ2GYxrDlZU26JcyMOnUoyyDMNnRsz"
"textbox" => array:2 [▼
1 => array:2 [▼
"name" => "first image"
"file" => UploadedFile {#27 ▼
-test: false
-originalName: "Screenshot (1).png"
-mimeType: "image/png"
-size: 164089
-error: 0
}
]
2 => array:2 [▼
"name" => "second image"
"file" => UploadedFile {#28 ▼
-test: false
-originalName: "Screenshot (2).png"
-mimeType: "image/png"
-size: 164334
-error: 0
}
]
]
]
Please or to participate in this conversation.