2014-07-19 62 views
1

試用cviebrokc/eloquent-sluggable我嘗試通過刀片視圖訪問雄辯,迴應鏈接到一個帖子的slu slu。數據庫表,使用sqlite,有一個slug列,但通過Eloquent獲取帖子並傾銷$post->getSlug()返回NULL。這是設置如下:cviebrock/eloquent-sluggable返回null getSlug()

在我的刀片觀點:

@foreach (Posts::get() as $post) 
    {{ var_dump($post->getSlug()) }} 
@endforeach 

在我Posts.php模型:

<?php 
use Cviebrock\EloquentSluggable\SluggableInterface; 
use Cviebrock\EloquentSluggable\SluggableTrait; 

class Posts extends Eloquent implements SluggableInterface{ 

    use SluggableTrait; 

    protected $sluggable = array(
     'build_from' => 'title', 
     'save_to' => 'slug', 
    ); 
} 

我應該將其指定爲一個新的實例,如果是的話,怎麼樣?或者我會怎麼去做呢?

回答

0

發現$post->getSlug()只得到指定的slug字段。要獲得slu,,將其保存到數據庫並將其顯示給用戶,您需要使用sluggify()創建slu and,然後將其存儲在數據庫中。從模型中做它看起來像這樣

//Check if slug is empty and generate if it isn't 
public function slugCheck() 
{ 
    if(empty($this->getSlug())) // Is slug empty 
    { 
     $this->sluggify();  // Create slug 
     $this->save();   // Save slug to database 
    } 
    return $this->getSlug(); // return the slug to echo out 
}