Subview blade doesn't POST file upload properly.
Hello. I have a file upload modal stored within a blade called create.blade.php.
The create.blade.php is used as a subview within edit.blade.php. However the only time I can get the post to hit my Controller@store action is when I override the route for edit.blade.php with create.blade.php. As a modal the files never reach and nothing is saved to database.
Here is my code:
Route - web.php
Route::get('/company/{company_id}/templates/{template_id}', 'CompanyController@template_edit');
Route::get('/company/{company_id}/templates/{template_id}/clone', 'CompanyController@template_clone');
Route::post('/company/{company_id}/templates/{template_id}/clone', 'CompanyController@template_clone_save');
Route::get('/company/{company_id}/templates/{template_id}/{rev_id}', 'CompanyController@template_edit');
//attachments
Route::get('/company/{company_id}/templates/{template_id}/create','AttachmentController@create');
Route::post('/company/{company_id}/templates/{template_id}/create','AttachmentController@store');
Route::post('/company/{company_id}/templates/{template_id}/','AttachmentController@store');
Controller - AttachmentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use Illuminate\Support\Facades\Storage;
use App\Attachment;
use App\DocumentTemplate;
use Ramsey\Uuid\Uuid;
use App\PhoenixConfig;
//pulled from CompanyController
use App\Appointment;
use App\Customer;
use App\DeleteRequests;
use App\DocumentTemplateRevision;
use App\Estimate;
use App\Franchisee;
use App\Job;
use App\MarketingSource;
use App\Sale;
use App\Todo;
use Couchbase\Document;
use QuickBooksOnline\API\DataService\DataService;
class AttachmentController extends Controller {
//begin create
public function create($company_id, $template_id /*,$rev_id*/
) {
return view('company.templates.create', compact('company_id'), compact('template_id'));
}
//end create
//begin store
public function store(Request $request, $company_id, $template_id /*,$rev_id*/
) {
$this->validate($request, ['filenames' => 'required|max:100000']);
if ($request->hasfile('filenames')) {
foreach ($request->file('filenames') as $file) {
$name = time() . '.' . $file->extension();
Storage::disk('local')->putFileAs('attachment', $file, $name);
$originalname = $file->getClientOriginalName();
$path = storage_path('app/attachment/' . $name);
//begin logging
$uploadarray = [
['filename' => 'File: ' . $name . "\n"],
['originalname' => 'Upload Name: ' . $originalname . "\n"],
['filepath' => 'File Path: ' . $path . " \n"],
['filesize' => 'File Size: ' . $file->getSize() . "\n"],
['filetype' => 'File Type: ' . $file->extension() . "\n"],
['fileurl' => 'URL: ' . Storage::url('app/attachment/'.$name) . "\n"],
];
$uploadvar='';
foreach ($uploadarray as $var){
$uploadvar .= implode('',$var);
}
$uploadcontent = $uploadvar;
$ullog = 'storage/upload' . $name . '.txt';
File::put($ullog,$uploadcontent);
//end logging
//begin record
$attachment = new Attachment();
$attachment->name = $name;
$attachment->originalname = $originalname;
$attachment->filepath = $path;
$attachment->filetype = $file->extension();
$attachment->filesize = $file->getSize();
$attachment->fileurl = Storage::url('app/attachment/'.$name);
$attachment->company_id = $company_id;
$attachment->template_id = $template_id;
$attachment->rev_id = 0;
$attachment->save();
//end record
}
}
return back()->with('success', 'Your files have been successfully added.');
}
//end store
}
View - company/templates/edit.blade.php
<!-- Begin Attachment Error / Success Pane -->
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Sorry!</strong> There were problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
<!-- End Attachment Error / Success Pane -->
<!-- Begin Attachment Modal Button -->
<button type="button" class="btn btn-primary float-right ml-2 mr-2" data-toggle="modal" data-target="#uploadModal">
Add Attachment
</button>
@include('company.templates.create')
<!-- End Attachment Modal Button -->
<!-- Begin Attachment Modal JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$('#uploadModal').on('shown.bs.modal')
});
$(document).ready(function() {
$(".btn-submit").click(function(){
var lsthmtl = $(".clone").html();
});
$("body").on("click",".btn-success-add",function(){
var lsthmtl = $(".clone").html();
$(".increment").after(lsthmtl);
});
$("body").on("click",".btn-danger",function(){
$(this).parents(".hdtuto").remove();
});
});
</script>
<!-- End Attachment Modal JS -->
Subview - company/templates/create.blade.php
<form method="post" action="{{url('company')}}/{{ request()->route('company_id') }}/templates/{{ request()->route('template_id') }}/create" enctype="multipart/form-data">
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadModalTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="uploadModalLongTitle">Upload New Attachment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container lst">
{{csrf_field()}}
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-success btn-success-add" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<div class="clone hide">
<div class="hdtuto control-group lst input-group" style="margin-top:10px">
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-danger" type="button"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-submit btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</form>
I don't receive any errors and the upload indicates success, but no files make it to the storage directory, no logs are recorded, indicating the attachmentcontroller@post action isnt being used. When I set the route to:
Route::get('/company/{company_id}/templates/{template_id}/','AttachmentController@create');
It comes back with my create blade instead of the edit blade and uploads work, however I need it to work from within the subview.
Please or to participate in this conversation.