Laravel explode() array multiplefile - php

hello i have been create an multiple upload file in my add-modal.php something like this :
and the attachment is successfully implode() into array, here is my controller.php, which used to insert into my DB :
public function create(Request $request)
{
$datatest=array();
$request->validate([
'attachment_name' => 'nullable',
'attachment_name.*' => 'file|mimes:pdf|max:5048',
]);
if($files = $request->file('attachment_name'))
{
foreach($request->file('attachment_name') as $image )
{
$name = date('dmY') . "-" .$image->getClientOriginalName();
$image->move(public_path().'/storage/file/', $name);
$datatest[] = $name;
}
}
$insert['attachment_name'] = implode("|",$datatest);
MediaOrder::create($insert);
flash_success_store('Media Order successfully added.');
if ($request->ajax()){
return redirect_ajax_notification('media-order.index');
}else{
return redirect_ajax('media-order.index');
}
}
and then here is the attachment_name or file in the datatable :
so i would like to explode() the attachment_name, then create a download file for each attachment_name array, here is my view-modal-blade.php which contain the download button :
as for now each attachment_name or the file .pdf is still in one value, here is my view-modal-blade.php value which used to placeholder the attachment_name value :
<div class="row mg-t-20">
<label class="col-sm-4 form-control-label">ATTACHMENT:</label>
<div class="col-sm-8 mg-t-10 mg-sm-t-0 input-group control-group increment">
<input name="attachment_name[]" id="attachment_name_view" class="form-control" readonly></input>
<div class="input-group-btn">
<button class="btn btn-primary" type="button"><i class="fa fa-save"></i></button>
</div>
</div>
</div>
so my question is :
Is there a way to foreach and explode() the attachment_name based on the data id an then put theexplode() value like the first image?, so user can download each attachment_name file individualy? or maybe someone have an better alternate solution, thank you!.
Progress :
i have tried using foreach(explode('|', $data->attachment_name)) but the attachment_name ended up becomde undefined for the attachment_name. i will provide anything that might able to help, and apologize for my english.

Related

How sort json array result in codeigniter 4

if mind, anyone can tell me how to sort by created_at on json array.
I want the newest comment on top, not on the bottom.
Here is my code :
Model (to insert and get comment data):
public function add_komen($data){
return $this->komen->insert($data);
}
public function get_komen($id){
return $this->komen->where('id_user', $id)->get()->getResultArray();
}
Controller (get data & get result using array for views):
public function do_add_komentar(){
$data['nama_komentar'] = $this->request->getPost('nama');
$data['isi_komentar'] = $this->request->getPost('komentar');
$data['id_user'] = $_SESSION['id_user'];
$update = $this->UndanganModel->add_komen($data);
if($update){
echo json_encode(array('status' => 'sukses','nama' => \esc($data['nama_komentar']),'komentar' => \esc($data['isi_komentar']) ));
}else{
echo json_encode(array('status' => 'gagal'));
}
}
Views (Form & Get Result):
<input id="nama" type="text" class="form-control mt-2" placeholder="Nama Anda" required>
<textarea id="komentar" class="form-control" id="exampleFormControlTextarea1" placeholder="Pesan anda.." rows="3" required></textarea>
<button id="submitKomen" class="btn btn-primary btn-block" >Kirim</button>
<div class="layout-komen">
<?php foreach($komen as $key => $data) { ?>
<div class="komen" >
<div class="col-12 komen-nama">
<?= \esc($data['nama_komentar']); ?>
</div>
<div class="col-12 komen-isi">
<?= \esc($data['isi_komentar']); ?>
</div>
</div>
<?php } ?>
</div>
You have no need to sort json array. This can be achieved simply by sorting for created_at in your model function for get result. I am assuming that created_at is column name in your table. if not then replace created_at with the column name. See code here
public function get_komen($id){
return $this->komen->where('id_user', $id)->orderBy("created_at", "desc")->get()->getResultArray();
}
check and reply

saving image in database and show in view laravel 5.6

i want to store image in my database as client agreement but i can't save any thing in database just the path i give manually here is what i have done
controller :
$filename = $request->file('agreement')->store('public/images');
$client = Client::create([
'title' => $request->title,
'description' => $request->description,
'fax'=>$request->fax,
'adrress1'=>$request->adrress1,
'telephone1'=>$request->telephone1,
'client_type'=>$request->client_type,
'sellpercent'=>$request->sellpercent,
'agreement'=>'uploads/agreement/'. $filename,
view :
.
.
.
<div class="form-group row">
<label class="col-form-label col-lg-2">test image</label>
<div class="col-lg-10">
<input type="file" name="agreement" class="form-control-plaintext">
</div>
</div>
<button type="submit" class="btn btn-primary btn-raised legitRipple">submit</button>
with this code i get this error on store function
Call to a member function store() on null
and if i remove store function i just save null in database
Firstly add enctype='multipart/form-data' in your form tag as an attribute.
Secondly,
replace $filename = $request->file('agreement')->store('public/images'); with $filename = $request->file('agreement')->move('public/images');
Hope that helps.

Laravel How to upload EXCEL file to database?

My Laravel version is 5.6.and PHP is 7.
I found some upload sample on the internet, but most of them do not work.
Have anyone can provide a complete example to me?
I found a similar case on the Internet.
Laravel 5.6 Excel and CSV import export using maatwebsite example
Unfortunately, it doesn't work, too.
Here is my source code.
blade.php
<form class="form-upload" method="POST" action="{{url('/excelUpload')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="file">excel upload</label>
<input type="file" id="file" name="ex_file">
<p class="help-block">!!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default">cancel</button>
<button type="submit" class="btn btn-primary" id="upload_f">submit</button>
controller.php
public function excelUpload(Request $request){
if($request->hasFile('StudentUploadSample')){
return "yes i have a file";
}
return "nofile";
}
However, the result always show "nofile".
I didn't use any "use". Should I use something?
The example on the web page does not use any "use", too.
In addition, I read some of the websites that the files will be placed under the storage folder, but I can't find my file "StudentUploadSample.xlsx" in the storage folder.
Have anyone can provide a complete example to me or how to solve this problem?
Replace the name of file and use the reference site where you will get the proper code with explanation.
Reference site like: http://www.expertphp.in/article/laravel-5-maatwebsite-import-excel-into-db-and-export-data-into-csv-and-excel
public function importFileIntoDB(Request $request){
if($request->hasFile('sample_file')){
$path = $request->file('sample_file')->getRealPath();
$data = \Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
$arr[] = ['name' => $value->name, 'details' => $value->details];
}
if(!empty($arr)){
\DB::table('products')->insert($arr);
dd('Insert Record successfully.');
}
}
}
dd('Request data does not have any files to import.');
}
You use the wrong name of the file in your controller:
if($request->hasFile('ex_file')){
return "yes i have a file";
}
So fix that, and try to check.
replace <input type="file" id="file" name="ex_file"> with <input type="file" id="file" name="StudentUploadSample">
and you are done.
you can use laravel excel.
address:https://github.com/Maatwebsite/Laravel-Excel
/**
* my Controller code
*
*/
$updateFile = $request->file('update_file');
$fileSize = (int) filesize($updateFile);
$fileExtension = $updateFile->getClientOriginalExtension();
$filePath = $updateFile->getRealPath();
$excelData = Excel::load($filePath)->get()->toArray();
or you can use: https://phpspreadsheet.readthedocs.io/en/develop/

codeigniter : Cant display value from input text using $_POST to SQLcode inside model

the function sumqtyin() inside codeigniter model
function sumqtyin(){
$kdbahan = $_POST['kode_bahan_baku']; //i wanna echo a value from the input text kode_bahan_baku but it always says error "undefined index kode_bahan_baku"
$datenow = date("Y-m-d");
return $this->db->query("
SELECT IFNULL(SUM(qty_in),0) AS qty_in
FROM trans_stock_movement
WHERE tanggal_movement='$datenow'
AND status_aktif='YES' AND kode_bahan_baku = '$kdbahan' ");
}
my view input text for kode_bahan_baku inside a post form
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Bahan Baku</label>
<div class="col-sm-2">
<div class="input-group">
<input type="text" class="form-control" id="nama_bahan_baku" name="nama_bahan_baku" placeholder="Bahan Baku" value="" style="width:150px" required="required">
<input type="text" id="kode_bahan_baku" name="kode_bahan_baku" value="" class="form-control">
<div class="input-group-btn">
<button type="button" class="btn btn-info btn-flat" data-toggle="modal" id="btnMenu" data-target="#menuModal">
<i class="fa fa-fw fa-search"></i>
</button>
</div>
</div>
</div>
</div>
please help :(
Your controller is the go-between of your view and your model, as such, you should be grabbing the data out using $this->input->post('kode_bahan_baku') in the controller and passing those details through to the model:
Controller:
$kdbahan = $this->input->post('kode_bahan_baku');
$dbResultObj = $this->yourLoadedModel->sumqtyin($kdbahan);
then use the object $dbResultObj like: $dbResultObj->qty_in to get out the result
Model:
function sumqtyin($kdbahan){
$datenow = date("Y-m-d");
return $this->db->select('SUM(IFNULL(qty_in,0)) AS qty_in')
->where('tanggal_movement', $datenow)
->where('status_aktif', 'YES')
->where('kode_bahan_baku', $kdbahan)
->get('trans_stock_movement')
->result();
}
Please Follow the MVC structure , Before you make any manipulation on database you just have to follow the MVC method .
VIEW => CONTROLLER => MODEL => CONTROLLER => VIEW
like #Brian Ramsey did . check out the guidelines of codeigniter
https://www.codeigniter.com/user_guide/

homestead laravel 5 : link Download file from storage

i am need hint link to download upload file, currently i am following this answer
here is my code
Routes file :
Route::get('getDownload/{remind_letter}',
['as'=>'downloadData',
'uses'=>'DockController#getDownload']);
Controller File :
public function getDownload($remind_letter)
{
$download = Dock::where('remind_letter','=',$remind_letter)->firstOrFail();
$file =Storage::disk('local')->get($download->remind_letter);
return (new response($file))->header('Content-Type', $entry->m1);
}
result file
<div class="row">
<div class="form-group">
<div class="col-xs-5">
<label class="col-sm-7">Remind Letter :</label>
<input type="text" name="remind_letter" value="{{$getData->remind_letter}}">
</div>
<div><a href="{{ route('downloadData',$getData->remind_letter) }}">
<button class="btn btn-success btn-sm">Download</button></a></div>
</div>
</div>
and the result link dock.start/star/getDownload/home/vagrant/Code/dock/storage/app/25/JTK/2015/Renewal License Kaspersky/Moonarch Security/tmp/phpQaGzoD.pdf
but the link is always show up error 404 and i can't download file
FYI when upload file i am using storage_path() function
it fix, i am wrong logic, when upload file it should using getFilename() function, $entry->filename = $filename->getFilename().$extension;

Categories