目前,我正在使用 spatie/laravel-translatable 包和 slug cviebrock/eloquent-sluggable,但是当我想去特定的帖子时,我的意思是当点击帖子的“阅读更多”按钮时,它会找不到页面。这是我的代码:
路线:
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware'=>'setLocale'], function() {
Route::get('/news-events', 'FrontController@newsEvents')->name('news_events');
Route::get('/news-events/{id}', 'FrontController@newsSingle')->name('news-single');
});
模型:
<?php
namespace App;
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\Services\SlugService;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
class NewsPost extends Model
{
use HasTranslations;
use SluggableScopeHelpers;
use Sluggable;
protected $translatable = [
'title',
'title_ar',
'description',
'description_ar'
];
protected $fillable = [
'is_publish',
'title',
'description',
'cat_id',
'media_id'
];
protected $sluggable = array(
'build_from' => 'title',
'save_to' => 'slug',
);
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable()
{
return [
'slug' => [
'source' => 'title'
]
];
}
public function category(){
return $this->belongsTo('App\NewsCategory', 'cat_id');
}
public function media(){
return $this->belongsTo('App\NewsMedia','media_id');
}
}
首页:
<div class="post-prev-more">
<a href="{{route('news-single', [app()->getLocale(), $news_post->slug])}}" class="btn btn-mod btn-gray btn-round">Read More <i class="fa fa-angle-right"></i>
</a>
</div>