doufei2194 2015-11-05 18:34
浏览 337
已采纳

laravel save方法不能完全保存数据

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 "
Done : ".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');
    }

}
  • 写回答

1条回答 默认 最新

  • drb56625 2015-11-05 21:10
    关注

    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');

    thanks from all to help me, I have a very bad headache and want to rest...

    thanks from all again, good night/morning/afternoon (according to your time-zones) :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号