Welcome Guest, Not a member yet? Register   Sign In
I need help with this issue
#1

Creating an update function on my personal project would be glad if anyone can help.
This is the controller
public function update($id){
            helper(['form']);
            $model = new BlogModel();
            $data = $model->find($id);
            //echo $data['image'];
            if ($this->request->getMethod() == 'post') {
            $rules = [
                'image' => 'is_image[image]',
                'title' => 'required|min_length[3]|max_length[255]',
                'body' => 'required',    
            ];
            $old_img = $data['image'];
            $file=$this->request->getFile('image');

            if ( $this->validate($rules)) {

            if($file->isValid() && !$file->hasMoved())
            {
               
                if(file_exists("uploads/images/".$old_img)){
                    \unlink("uploads/images/".$old_img);
                }
                $imagename = $file->getRandomname();
                $file->move('./uploads/images/', $imagename);
            }
            else{
                $imagename = $old_img;
            }
            $newData = [
                'title' => $this->request->getVar('title'),
                'body' => $this->request->getVar('body'),
                'slug' => url_title($this->request->getVar('title')),
                'image' => $imagename
            ];          
            $model->update($id,$newData);
            $session = \Config\Services::session();
            $session->setFlashdata('success','Post was created Successfully');
            return redirect()->to('/');  
        }else{
            $data['validation'] = $this->validator;  
        }  
    }
Reply
#2

What's your issue?

Please don't paste code as an image file.
I can't read it, it is too small, and it is difficult to run.
Reply
#3

Thanks
Would try not to
Reply




Theme © iAndrew 2016 - Forum software by © MyBB