how to save image name to DB using Codeigniter 4

Asked

Viewed 27 times

1

The only question here is to save the name I catch with var $newname. I can get the name already modified and even print on the screen, but when I step on set() to save in DB nothing happens. The problem itself is all here:

$news_model->set('img_name', $newName);

Here’s all the code:

<?php namespace App\Controllers;

class Publicar extends BaseController
{
    public function index()
    {
        $news_model = new \App\Models\NewsModel();
        $data['news'] = $news_model->find();
        $image_news = new \App\Models\ImageModel();
        $data['image_news'] = $image_news->find();
        $data['title'] =  'news social - Add Notícia';
        $data['msg'] = '';
        $data['class_error'] = '';
        $data['erros'] = '';
        date_default_timezone_set('America/Sao_Paulo');
        $data_create = date('Y-m-d H:i');

        if($this->request->getMethod() == 'post'){
            $news_model = new \App\Models\NewsModel();
            $news_model->set('title', $this->request->getPost('title_news'));
            $news_model->set('type_news', $this->request->getPost('type_news'));
            $news_model->set('description', $this->request->getPost('description'));
            $news_model->set('date_posted', $data_create);            

            if ($news_model->insert()) {
                // Upload images                
                if($file = $this->request->getFile('img_name'))
                {
                    $newName = $file->getRandomName();
                    $file->move('upload', $newName);
                    // Save name
                    $news_model->set('img_name', $newName);
                }

                $data['erros'] = ['Notícia adicionada com sucesso. Obrigado fulano' . $newName];
                $data['class_error'] = 'success';
            } else {
                $data['erros'] = $news_model->errors();
                $data['class_error'] = 'danger';
            }
        }

        echo view('publicar_form', $data);
    }
}

Plus, I thought it was an argument problem, but here I can save the good date using a var:

$news_model->set('date_posted', $data_create);

1 answer

1

I got it! In fact I had to take the objects of the img before playing in the Insert, because it already takes all the data and could not lead to var $newname, this the solution:

if($this->request->getMethod() == 'post'){
            $news_model = new \App\Models\NewsModel();
            $news_model->set('title', $this->request->getPost('title_news'));
            $news_model->set('type_news', $this->request->getPost('type_news'));
            $news_model->set('description', $this->request->getPost('description'));
            $file = $this->request->getFile('img_name');
            // Take name img for save DB
            $newName = $file->getRandomName();
            $news_model->set('img_name', $newName);
            $news_model->set('date_posted', $data_create);

            if ($news_model->insert()) {
                // Upload images                
                if($file)
                {
                    $file->move('upload', $newName);                    
                }
                
                $data['erros'] = ['Notícia adicionada com sucesso. Obrigado fulano'];
                $data['class_error'] = 'success';
            } else {
                $data['erros'] = $news_model->errors();
                $data['class_error'] = 'danger';
            }
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.