tuytoosh's avatar

laravel save method not saving data completely

I am using Laravel to create a web bot that collects data from other websites and stores it in my MySQL database. When I want to save body I use dd($this->render($post)); and it is good. Yet, when I use $post->save() for saving my post in db, it not saving the body of my post completely and some of text is missing.

My body is at least 10000 characters and I always have this problem.

I checked text and longtext for body column type and is not there any difference...

Where is problem?

edit :

this is my index method :

 public function getIndex()
    {
        $temp = App\Temp::where('status' , 0)->orderBy('id' , 'desc')->first();
        $temp->status = 1;
        $temp->save();
        $post = new App\Post;
        $post->title = $temp->title;
        $post->link = $temp->link;
        $post->desc = $temp->desc;
        $post->cat_id = $temp->cat_id;
        $post->url_id = $temp->url_id;
        $post->body = $this->render($post);
        $post->save();
        echo "+";
    }

When I am using dd($this->render($post)); before save, it shows full text without any problem... but after save when I fetch the body, some characters is missing from the end of post...

and this is render() method...

public function render($post)
    {
        echo "Start : ";
        $this->ftp->createFolder('/'.$post->url_id.'/'.$post->id."/");
        echo "Dir - ";
        $mixed_body = $this->desc($post->title);
        echo "Mix - ";
        $body ="";
        $body = $body . '<h3><a href='.$this->postUrl.'>'.$this->postTitle.'</a></h3>';
        echo "Title - ";

        while(strlen($mixed_body) > 100)
        {
            $body = $body . $this->randImage($post);
            $body = $body . $this->randTitle();

            //insert a random paragraph
            $number = rand(100 , strlen($mixed_body));//temporary
            $paragraph = substr($mixed_body , 0 , $number);
            $mixed_body = substr($mixed_body , $number , strlen($mixed_body)-$number);

            $body = $body . '<p>' . $paragraph . '</p>';
            echo "P|";
        }
        echo "\nDone : ".strlen($body);
        return $body;
    }

others methods in render() are appending some text to $body and those are not important.

and my model :

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model {

    public function tags()
    {
        return $this->hasMany('App\Tag');
    }

}
0 likes
3 replies
thomaskim's avatar

I checked text and longtext for body column type and is not there any difference...

Does that mean you have tried both text and longtext? What is the current column type?

tuytoosh's avatar
tuytoosh
OP
Best Answer
Level 2

I find my problem reason... sometimes I have a character like � in my body that laravel not saves the characters after it... I find to use this line to delete � character...

$post->body = mb_convert_encoding($this->render($post), 'UTF-8', 'UTF-8');

Please or to participate in this conversation.