0

I am having a problem with PHP at the moment, I am getting this error:

Object of class stdClass could not be converted to string. 

I use Codeigniter 3.x.

This is function in my model:

public function create_post($post_image){

    $this->load->library('session');
    $slug = url_title($this->input->post('title'));

    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'body' => $this->input->post('body'),
        'category_id' => $this->input->post('category_id'),
        'user_id' => $this->session->userdata('user_id'),
        'post_image' => $post_image
    );

    return $this->db->insert('posts', $data);
}

And this is the controller:

public function create(){

    // Check login
    if(!$this->session->userdata('logged_in')){
        redirect('users/login');
    }

    $data['title'] = 'Create Post';

    $data['categories'] = $this->post_model->get_categories();

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('body', 'Body', 'required');

    if($this->form_validation->run() === FALSE){
        $this->load->view('templates/header');
        $this->load->view('posts/create', $data);
        $this->load->view('templates/footer');
    } else {
        // Upload Image
        $config['upload_path'] = './assets/#images/posts';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width'] = '2000';
        $config['max_height'] = '2000';

        $this->load->library('upload', $config);

        if(!$this->upload->do_upload()){
            $errors = array('error' => $this->upload->display_errors());
            $post_image = 'noimage.jpg';
        } else {
            $data = array('upload_data' => $this->upload->data());
            $post_image = $_FILES['userfile']['name'];
        }

        $this->post_model->create_post($post_image);

        // Set message
        $this->session->set_flashdata('post_created', 'Your post has been created');

        redirect('posts');
    }
}

I have error in model in function:

return $this->db->insert('posts', $data);

and in controller:

$this->post_model->create_post($post_image);

I don't really know what this problem is, so any help would be great.

Littm
  • 4,923
  • 4
  • 30
  • 38
AniaS
  • 1
  • 1
    check https://stackoverflow.com/questions/3607550/object-of-class-stdclass-could-not-be-converted-to-string – Vickel Jun 14 '20 at 22:16

1 Answers1

0

try to var_dump($data['categories']). looks like it return an Object but you threat it as a string.