PLease help me on how to upload image in the folder and the same time in the database with a random name.
There's an error: Call to a member function getName() on null.
Heres my code in controller
`public function actionInsert()
{
$destination = new DestinationModel();
$name=$this->request->getVar('name');
$place=$this->request->getVar('place');
$location=$this->request->getVar('location');
$category=$this->request->getVar('category');
$description=$this->request->getVar('description');
$latitude=$this->request->getVar('latitude');
$longitude=$this->request->getVar('longitude');
$image=$this->request->getFile('image');
$imageName = $image->getName();
$image->move('im/destination', $imageName);
if($place == 'Calapan City')
{
$place = 'Calapan';
}else if($category == 'Destination')
{
$category ='Destination';
}
$data = [
'name' => $name,
'place' => $place,
'location' => $location,
'category' => $category,
'image' => $place. '/'. $imageName,
'description' => $description,
'latitude' => $latitude,
'longitude' => $longitude
];
$destination->save($data);
return view('adding_place');
}`
I am trying to upload image using Laravel-8 as the backend.
Request Rules:
public function rules()
{
return [
'name' => [
'required',
'string',
'min:3',
'max:100',
Rule::unique('companies', 'name')
],
'website' => [
'nullable',
'max:100'
],
'logo' => 'nullable',
];
}
model:
protected $fillable = [
'id',
'name',
'website',
'logo',
];
Controller:
public function registerCompany(StoreCompanyRequest $request)
{
dd($request->all());
$arr_company = [
'name' => $request->name,
'website' => $request->website,
];
if ($request->hasFile('logo'))
{
$file = $request->file('logo');
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('YmdHis').'-'.$filename;
//move image to public/img folder
$file->move(public_path('storage/images/company'), $picture);
$arr_company['logo'] = $picture;
}
$newCompany = Company::create($arr_company);
return $this->success('Company Detail successfully Added.', [
'newCompany' => $newCompany,
]);
} catch(\Exception $e) {
DB::rollback();
Log::error($e);
return $this->error($e->getMessage(), $e->getCode());
}
}
Every other thing got inserted apart from the logo.
When I dd($request->all()); I got:
'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Since it is a registration form, that doesn't need user login, I only did:
const header = {
'Content-Type': 'application/json'
};
This only has to do with the image. All other fields got inserted are working fine.
How do I get it resolve?
Thanks
Since $imgPicture only set if request has file or else it will be undefined.
So to solve this issue change like below
'logo' => $imgPicture??null,
or
$arr_company = ['name'=> $request->companyName,];
if ($request->hasFile('logo'))
{
$file = $request->file('logo');
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('YmdHis').'-'.$filename;
//move image to public/img folder
$file->move(public_path('storage/images/company'), $picture);
$arr_company['logo']= $picture,
}
I'm inserting a record to a polymorphic imageable table, however it says column thread_id not found. I have not declared this thread_id column and I don't know where it's pulling it from. Here is the code it's trying to run.
protected static function bootRecordImage()
{
if (auth()->guest()) return;
foreach (static::getMethodToRecord() as $event) {
static::$event(function ($model) use ($event) {
$body = request()->body;
preg_match_all('/<img .*?(?=src)src=\"([^\"]+)\"/si', $body, $matches);
$images = $matches[1];
if($event == 'created') {
foreach ($images as $image) {
$model->images()->create([
'user_id' => auth()->id(),
'imageable_id' => $model->id,
'imageable_type' => get_class($model),
'path' => $image
]);
}
}
if($event == 'deleting') {
foreach ($images as $image) {
$model->images()->delete([
'user_id' => auth()->id(),
'imageable_id' => $model->id,
'imageable_type' => get_class($model),
'path' => $image
]);
if (File::exists(public_path($image))) {
File::delete(public_path($image));
}
}
}
});
}
}
My store method:
public function store(Request $request, Channel $channel, Spam $spam)
{
if (!auth()->user()) {
return back()->withInput()->with('flash', 'Sorry! You must be logged in to perform this action.');
}
if (!auth()->user()->confirmed) {
return back()->withInput()->with('flash', 'Sorry! You must first confirm your email address.');
}
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'channel_id' => 'required|exists:channels,id',
'g-recaptcha-response' => 'required'
// yes it's required, but it also needs to exist on the channels model, specifically on the id
]);
$response = Zttp::asFormParams()->post('https://www.google.com/recaptcha/api/siteverify', [
'secret' => config('services.recaptcha.secret'),
'response' => $request->input('g-recaptcha-response'),
'remoteip' => $_SERVER['REMOTE_ADDR']
]);
// dd($response->json());
if (! $response->json()['success']) {
throw new \Exception('Recaptcha failed');
}
$spam->detect(request('title'));
$spam->detect(request('body'));
$thread = Thread::create([
'user_id' => auth()->id(),
'channel_id' => request('channel_id'),
'title' => request('title'),
'body' => request('body'),
//'slug' => str_slug(request('title'))
]);
return redirect('/forums/' . $thread->channel->slug . '/' . $thread->slug);
}
As you can see, no where is a thread_id mentioned, yet in the error it looks like it's trying to insert into a thread_id column that I've never declared.
Thanks for reading.
I put the polymorphic relation in the model and the trait. Remove it from the Model and you're good to go.
I save image in database as array ["product-04.jpg"]. I don't know how to display image to view. I used Crinsane/LaravelShoppingcart and got the following error: "Cannot use object of type Gloudemans\Shoppingcart\CartItem as array". Can everyone help me?
ProductController I saved image in db:
if($request->hasFile('images')){
$files = $request->file('images');
$extension = ['png','jpg','gif','jepg'];
foreach ($files as $key => $item) {
$nameFile = $item->getClientOriginalName();
$exFiles = $item->getClientOriginalExtension();
if(in_array($exFiles, $extension)){
$item->move(public_path().'/upload/images',$nameFile);
$arrNameFile[] = $nameFile;
}
}
}
if($arrNameFile){
$dataInsert = [
'name_product' => $nameProduct,
'categories_id' => json_encode($categories),
'colors_id' => json_encode($colors),
'sizes_id' => json_encode($sizes),
'brands_id' => $brand,
'price' => $price,
'qty' => $qty,
'description' => $description,
'image_product' => json_encode($arrNameFile),
'sale_off' => $sale,
'status' => 1,
'view_product' => 0,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => null
];
if($pd->addDataProduct($dataInsert)){
$request->session()->flash('addPd','success');
return redirect()->route('admin.products');
} else {
$request->session()->flash('addPd','Fail');
return redirect()->route('admin.addProduct');
}
} else {
$request->session()->flash('addPd','Can not upload image');
return redirect()->route('admin.addProduct');
}
}
CartController: I add products and want to show list products in cart
public function addCart(Request $request, $id)
{
$product = Products::select('name_product', 'id', 'price', 'qty', 'image_product')->find($id);
if(!$product) return redirect('/');
Cart::add([
'id' => $id,
'name' => $product->name_product,
'qty' => 1,
'price' => $product->price,
'options' => [
'images' => json_decode($product->image_product, true),
]
]);
return redirect()->back();
}
public function getListCart(){
$products = Cart::content();
return view('frontend.cart.showCart', compact('products'));
}
And view i get image in src : {{ URL::to('/') }}/upload/images/{{ $product->image_product[0] }}
Now I'm a bit more into this ZF3 stuff. I could (with some help) implement nearly everything I wanted. To dive in the, for me new version, I developed a test project.
Some questions are still unanswered and I didn't find usable explanations.
My new issue is InputFilterAwareInterface. I tried the examples for strings from the tutorial, so far everything ok. But like always for the easy topics you find everything, if you go further it ends abruptly.
I need an Inputfilter for xls and xlsx files. I googled of course, read tutorials, searched in the zend tutorial, because I had the idea there must somewhere exist some complete reference, but I couldn't find any.
So I tried this one:
$inputFilter->add([
'type' => 'Zend\InputFilter\FileInput',
'name' => 'DCL_Path',
'required' => true,
'validators' => [
['name' => 'FileUploadFile'],
[
'name' => 'FileMimeType',
'options' => [
'mimeType' => ['text/xls', 'text/xlsx']
]
],
[
'name' => 'Filesize',
'options' => [
'max' => 4096
]
],
],
// 'filters' => [
// [
// 'name' => 'FileRenameUpload',
// 'options' => [
// 'target'=>'./data/upload',
// 'useUploadName'=>true,
// 'useUploadExtension'=>true,
// 'overwrite'=>true,
// 'randomize'=>false
// ]
// ]
// ],
]);
As you an see I'm still fighting the validator part. What would be the right syntax to validate xls and xlsx files with a maximum size of let's say 4 MB?
And after that, what about the filterarea, I did the following in my controller action just because I'm used to
if ($form->isValid()) {
$data = $form->getData();
// Upload path
$location = "public/files/";
// A bit validation of uploaded file
$allowedExtension = array('xls', 'xlsx');
$extension = explode('.', $data['DCL_Path']['name']);
$extension = end($extension);
//$import['DCL_Path']=$data['DCL_Path']['name'];
//$fileName = time() . '.' . $extension;
$fileName = $data['DCL_Path']['name'];
// Check if everything is OK!
//echo $fileName;
if (0 === $data['DCL_Path']['error'] && in_array($extension, $allowedExtension)) {
move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName);
} else {
echo 'Something went wrong!';
}
Is the move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName); obsolet with the filterstuff in the interface? And again how would be the syntax in this case?
And one of my biggest wish, does somebody know kind of a tutorial which explains plainly the different possibilities and keys of both options (validator and filter)? Sometimes I can't believe that you need so much time to find only the right keys.
EDIT 1: Show Form, filterstuff and changed controller
here is part my Form class:
<?php
namespace Import\Form;
use Zend\Form\Form;
class ImportForm extends Form
{
public function __construct($name = null)
{
// We will ignore the name provided to the constructor
parent::__construct('import');
$this->add([
'name' => 'DCLID',
'type' => 'hidden',
]);
$this->add([
'name' => 'UnitID',
'type' => 'text',
'options' => [
'label' => 'equipment',
],
]);
$this->add([
'name' => 'DCL_Path',
'type' => 'File',
//'required' => true,
'options' => [
'label' => 'path to file',
],
// 'name' => 'FileRenameUpload',
// 'filters' => [
// 'target'=>'./public/files',
// 'useUploadName'=>true,
// 'useUploadExtension'=>true,
// 'overwrite'=>true,
// 'randomize'=>false
// ],
// 'validators' => [ // Validators.
// // Put validator info here.
// ]
]);
here part of the class extended InputFilterAwareInterface
<?php
namespace Import\Model;
use DomainException;
use Zend\Filter\StringTrim;
use Zend\Filter\StripTags;
use Zend\Filter\ToInt;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\Validator\StringLength;
class Import implements InputFilterAwareInterface
{
public $DCLID;
public $DCL_Path;
public $Unitname;
public $UnitID;
public $Importdate;
public $Importuser;
public $Importok;
public $DCL_Type;
public $Changed_per_User;
public $Description_Changes;
private $inputFilter;
public function exchangeArray(array $data)
{
$this->DCLID= !empty($data['DCLID']) ? $data['DCLID'] : null;
$this->UnitID= !empty($data['UnitID']) ? $data['UnitID'] : null;
$this->DCL_Path= !empty($data['DCL_Path']) ? $data['DCL_Path'] : null;
$this->Importdate= !empty($data['Importdate']) ? $data['Importdate'] : null;
$this->Importuser= !empty($data['Importuser']) ? $data['Importuser'] : null;
$this->Importok= !empty($data['Importok']) ? $data['Importok'] : null;
$this->DCL_Type= !empty($data['DCL_Type']) ? $data['DCL_Type'] : null;
$this->Changed_per_User= !empty($data['Changed_per_User']) ? $data['Changed_per_User'] : null;
$this->Description_Changes= !empty($data['Description_Changes']) ? $data['Description_Changes'] : null;
}
public function getArrayCopy()
{
// echo var_dump(get_object_vars($this)
// );
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new DomainException(sprintf(
'%s does not allow injection of an alternate input filter',
__CLASS__
));
}
public function getInputFilter()
{
if ($this->inputFilter) {
return $this->inputFilter;
}
$inputFilter = new InputFilter();
// $inputFilter->add([
// 'name' => 'DCLID',
// 'required' => false,
// 'filters' => [
// ['name' => ToInt::class],
// ],
// ]);
// Validator für das Upload Element
$inputFilter->add([
'type' => 'Zend\InputFilter\FileInput',
'name' => 'DCL_Path', // Element's name.
'required' => true, // Whether the field is required.
'filters' => [ // Filters.
[
'name' => \Zend\Filter\File\RenameUpload::class,
'options' => [
'use_upload_extension' => true,
'randomize' => false,
'overwrite' => true,
'target' => 'public/files',
],
],
],
'validators' => [ // Validators.
[
'name' => \Zend\Validator\File\Extension::class,
'options' => [
'extension' => 'xls, xlsx',
'message' => 'File extension not match',
],
],
[
'name' => \Zend\Validator\File\MimeType::class,
'options' => [
'mimeType' => 'text/xls', 'text/xlsx',
'message' => 'File type not match',
],
],
[
'name' => \Zend\Validator\File\Size::class,
'options' => [
'min' => '1kB', // minimum of 1kB
'max' => '4MB',
'message' => 'File too large',
],
],
]
]);
and my part of my controlleraction, I think inhere might be the problem, something is probably not logic:
$form = new ImportForm();
$form->get('submit')->setValue('Add'); //Änderung des LAbels des Submit Buttons, um das Form wiederverwenden zu können
//echo "hier";
$request = $this->getRequest();
if (! $request->isPost()) { //wurden Daten über POST geschickt?
return ['form' => $form]; //Keine Daten, nur Form anzeigen, nicht verarbeiten
}
else {
//Es wurden Daten gesendet
//echo "Daten";
$import = new Import(); //Neue Instanz von Import
$form->setInputFilter($import->getInputFilter()); //Filter an Form binden
$form->setData($request->getPost()); //Daten abholen
//echo $form->isValid();
if (! $form->isValid()) {
return ['form' => $form]; //Wenn die Daten nicht valide sind
}
else{ //aus Tableadapter
$import->exchangeArray($form->getData());
$data = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
$form->setData($data);
if ($form->isValid()) {
$data = $form->getData();
// Upload path
// $location = "public/files/";
// $allowedExtension = array('xls', 'xlsx');
// $extension = explode('.', $data['DCL_Path']['name']);
// $extension = end($extension);
$fileName = $data['DCL_Path']['name'];
// // Check if everything is OK!
// //echo $fileName;
// if (0 === $data['DCL_Path']['error'] && in_array($extension, $allowedExtension)) {
// move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName);
// } else {
// echo 'Something went wrong!';
// }
//-----------------------------------------------------------------
// t_dcl befüllen
//-----------------------------------------------------------------
//$namen = explode(",", $import ); //Konvertierung des Strings in ein Array
//echo "<pre>"; var_dump($namen); echo "</pre>"; //Formartierte Ausgabe des Arrays
$this->table->saveImport($import);
EDIT2: Post part of controlleraction to discuss order of some statements:
controlleraction
$form = new ImportForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if (! $request->isPost()) {
return ['form' => $form];
}
else {
$import = new Import(); //Neue Instanz von Import
$form->setInputFilter($import->getInputFilter());
$form->setData($request->getPost());
$data = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
$form->setData($data);
if (! $form->isValid()) {
return ['form' => $form];
}
else{
$import->exchangeArray($form->getData());
$data = $form->getData();
$fileName = $data['DCL_Path']['name'];
Is the position of $form->setInputFilter($import->getInputFilter()); correct? Or when do I have to bind the Inputfilter to form?
There is a small issue left: I now have a message:
File type not match
I tried to upload a .xlsx file
Please try this for InputFilter
$inputFilter->add([
'type' => 'Zend\InputFilter\FileInput',
'name' => 'DCL_Path', // Element's name.
'required' => true, // Whether the field is required.
'filters' => [ // Filters.
[
'name' => \Zend\Filter\File\RenameUpload::class,
'options' => [
'use_upload_extension' => true,
'randomize' => false,
'overwrite' => true,
'target' => 'public/files',
],
],
],
'validators' => [ // Validators.
[
'name' => \Zend\Validator\File\Extension::class,
'options' => [
'extension' => 'xls, xlsx',
'message' => 'File extension not match',
],
],
[
'name' => \Zend\Validator\File\MimeType::class,
'options' => [
'mimeType' => 'text/xls', 'text/xlsx',
'message' => 'File type not match',
],
],
[
'name' => \Zend\Validator\File\Size::class,
'options' => [
'max' => '4MB',
'message' => 'File too large',
],
],
]
]);
And here for controller
if($this->getRequest()->isPost()) {
// merge post and files
$request = $this->getRequest();
$data = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
// passing data
$form->setData($data);
// execute validator
if($form->isValid()) {
// execute file filters.
$data = $form->getData();
}
}
By using \Zend\Validator\File\Extension, \Zend\Validator\File\MimeType and \Zend\Validator\File\FileSize you don't need to check manually in your contoller using this code.
if (0 === $data['DCL_Path']['error'] && in_array($extension, $allowedExtension)) {}
Because validation will be executed when we call $form->isValid().
And by using \Zend\Filter\File\RenameUpload, you don't need to use move_uploaded_file() anymore. Because this filter will move the uploaded file to destination foder we defined in 'target' => 'public/files' option.
Filtering is executed when we call $form->getData();
And about explanation for Validator and Filter, I suggest you to create another post. By using a separate question, it will be easy to search in search engine and will help another to find it.