You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on May 5, 2024. It is now read-only.
Hi
I am trying to use this on a project and it works fine with uploading the files with the unique id - but I was wondering how I can get it to use the uploaded files name and write it to the database.
This is my code (controller, text-model and imagemodel)
<?php
namespace Cart\Models;
use Cart\Models\User;
use Cart\Models\Admin;
use Cart\Models\Text;
use Cart\Models\Billede;
use Upload\Storage\FileSystem;
use Upload\File;
use Upload\Validation\Mimetype;
use Upload\Validation\Size;
use Illuminate\Database\Eloquent\Model;
class Text extends Model
{
protected $table = 'webshoptekst';
protected $foreignKey = 'billede_id';
protected $fillable = [
'overskrift',
'tekst',
'billede_id',
'filer',
'linktekst',
'navigationId',
'navn',
];
public static function txt() {
return Text::get()->all();
}
public function Pages()
{
return $this->hasOne(Text::class, 'id', 'navn');
}
}
My image model (billedemodel)
<?php
namespace Cart\Models;
use Cart\Models\Billede;
use Cart\Models\Text;
use Upload\Storage\FileSystem;
use Upload\File;
use Upload\Validation\Mimetype;
use Upload\Validation\Size;
use Illuminate\Database\Eloquent\Model;
class Billede extends Model
{
protected $table = 'webshopbilleder';
protected $fillable = [
'billedenavn',
];
public function text(){
return $this->hasOne(Text::class);
}
public function upload() {
$storage = new \Upload\Storage\FileSystem('../public/uploads');
$file = new \Upload\File('billede', $storage);
// Optionally you can rename the file on upload
$new_filename = uniqid('GK' . '-');
$file->setName($new_filename);
// Validate file upload
// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
$file->addValidations(array(
// Ensure file is of type "image/png"
// new \Upload\Validation\Mimetype('image/png'),
//You can also add multi mimetype validation
new \Upload\Validation\Mimetype(array('image/png', 'image/gif', 'image/jpg', 'image/jpeg', 'image/pdf')),
// Ensure file is no larger than 5M (use "B", "K", M", or "G")
new \Upload\Validation\Size('5M')
));
// Access data about the file that has been uploaded
$data = array(
'name' => $file->getNameWithExtension(),
'extension' => $file->getExtension(),
'mime' => $file->getMimetype(),
'size' => $file->getSize(),
'md5' => $file->getMd5(),
'dimensions' => $file->getDimensions()
);
// Try to upload file
try {
// Success!
$file->upload();
} catch (\Exception $e) {
// Fail!
$errors = $file->getErrors();
}
}
public $timestamps = false;
}
It uploads the files as it should with uniq-id but I can't get it to write the name to the database.
Can anyone help - please
The text was updated successfully, but these errors were encountered:
What you can do is retrieve the filename first then setName.
Without original filename
// Optionally you can rename the file on upload
$new_filename = uniqid('GK' . '-');
$file->setName($new_filename);
With original filename
// Optionally you can rename the file on upload
$original_filename = $file->getName();
$new_filename = uniqid('GK' . '-');
$file->setName($new_filename);
Hi
I am trying to use this on a project and it works fine with uploading the files with the unique id - but I was wondering how I can get it to use the uploaded files name and write it to the database.
This is my code (controller, text-model and imagemodel)
Text controller:
My textmodel
My image model (billedemodel)
It uploads the files as it should with uniq-id but I can't get it to write the name to the database.
Can anyone help - please
The text was updated successfully, but these errors were encountered: