2016-08-15 72 views
0

沒有什麼能夠找到在laravel 5.2中存儲大量動態標題,子標題,項目符號和段落的複雜文本內容的最佳方法。什麼是最好和最簡單的方法?什麼是數據庫結構和存儲多個標題的方法。保存帖子的標題和正文是另一回事,很容易。需要幫助......laravel在mysqli數據庫中複雜的文本內容存儲

回答

0

您可以使用mediumTextlongText作爲您的Column ....開頭;首先去你的Console並創建一個像這樣的遷移:php artisan make:migration Articles。然後,一旦創建遷移文件,將其打開,然後在該文件的up()方法中添加以下行。像這樣:

<?php 
    // FILE_NAME: 2016_08_15_163807_Articles.php 

    use Illuminate\Database\Migrations\Migration; 
    use Illuminate\Database\Schema\Blueprint; 

    class Articles extends Migration { 

     public function up() { 
      // Create table for storing data 
      Schema::create('articles', function (Blueprint $table) { 
       $table->increments('id'); 
       $table->string('title'); 
       $table->string('heading')->nullable(); 
       $table->string('sub_heading')->nullable(); 
       $table->string('photo')->nullable(); 
       $table->mediumText('body')->nullable(); //<== ENOUGH FOR COMPLEX TEXT 
       //$table->longText('complex_text')->nullable(); //<== MORE THAN ENOUGH FOR COMPLEX TEXT 
       $table->timestamps(); 
      }); 
     } 

然後app目錄內,創建一個名爲Article文件,也可以像這樣通過命令行生成它:

php artisan make:model Article 

要確保在創建新表,運行:

php artisan migrate 

現在你有這將具有的屬性,如titleheading的文章對象,sub_headingphotobody。但重要的是打開App\Article類並設置一個重要變量:

<?php 

    namespace App; 

    use Illuminate\Database\Eloquent\Model; 

    class Article extends Model { 

     /** 
     * The attributes that are mass assignable. 
     * 
     * @var array 
     */ 
     protected $fillable = [ 
      'title', 'heading', 'sub-heading', 'photo', 'body' 
     ]; 
    } 
+0

此行是否會在數據庫中完全添加html標記?怎麼運行的? – root

+0

謝謝這麼多,但我知道這個東西。你寫了關於遷移,這是用完整的。我想知道數據庫應該如何處理這種複雜的文本內容 – root

+0

一個混淆是是否要分離所有標題和副標題,然後將所有內容保存到complex_text – root