Laravel 5 我如何看待使用ServiceProvider发布的文章具有雄辩的价值
我在sidebar.blade.php中有一个博客类别Laravel 5 我如何看待使用ServiceProvider发布的文章具有雄辩的价值,laravel-5,eloquent,Laravel 5,Eloquent,我在sidebar.blade.php中有一个博客类别 @foreach ($categories as $category) <li> <a href="{{ route('category', $category->slug)}}"><i class="fa fa-angle- right"></i> {{$category->title}}</a> <span class="badge pull-right"&
@foreach ($categories as $category)
<li>
<a href="{{ route('category', $category->slug)}}"><i class="fa fa-angle- right"></i> {{$category->title}}</a>
<span class="badge pull-right">{{$category->posts()->count()}}</span>
</li>
@endforeach
这是Post.php
名称空间应用程序
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;
class Post extends Model
{
//Table Name
protected $table = 'posts';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
/*protected $fillable = [
'title',
'excerpt',
'body',
'categery_id',
'image',
];*/
protected $dates = ['published_at'];
public function author()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function getImageUrlAttribute($value)
{
$imageUrl = "";
if( ! is_null($this->image))
{
$imagePath = public_path() . "/img/" . $this->image;
if(file_exists($imagePath)) $imageUrl = asset("img/" . $this->image);
}
return $imageUrl;
}
public function getDateAttribute()
{
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}
public function getExcerptHtmlAttribute(){
return $this->excerpt ? Markdown::convertToHtml(e($this->excerpt)) : NULL;
}
public function getBodyHtmlAttribute()
{
return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL;
}
public function scopeLatestFirst($query)
{
return $query->orderBy('published_at', 'desc');
}
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
}
Web.php
Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about');
Route::get('/category/{category}', [
'uses' => 'PostsController@category',
'as' => 'category'
]);
Route::resource('books', 'BooksController');
Route::resource('posts', 'PostsController');
Route::resource('categories', 'CategoriesController', ['except'=> ['create']]);
Composer ServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Category;
use App\Post;
class ComposerServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('layouts.sidebar', function($view){
$categories = Category::with(['posts' => function($query){
$query->published();
}])->orderBy('title', 'asc')->get();
return $view->with('categories', $categories);
});
}
}
回顾一下我需要做的事情
我只想让我发布的帖子在柜台上显示,而不是我数据库中的所有帖子
(<span class="badge pull-right">{{$category->posts->count()}}</span>)
({{$category->posts->count()})
Post.php
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
public函数范围已发布($query)
{
return$query->where('published_at','试试这个:
<span class="badge pull-right">{{$category->posts()->published()->count()}}</span>
{{$category->posts()->published()->count()}
没有尝试,但这应该可以解决问题。试试这个:
<span class="badge pull-right">{{$category->posts()->published()->count()}}</span>
{{$category->posts()->published()->count()}
没有尝试,但这应该可以解决问题。请。这会向其他用户显示问题已经解决。@ImagineRPower如果答案对你有帮助,它可能会对其他人有帮助。请将答案标记为已接受并投票。谢谢我尝试将答案标记为已接受,但我得到了答案“感谢您的反馈!记录声誉低于15的人的投票,但不要更改公开显示的帖子分数。”抱歉!确定没有问题,那么当您获得15个声誉时,请将问题标记为已接受:-)请。这会向其他用户显示问题已解决。@ImagineRPower如果答案对您有帮助,它可能会帮助其他用户。请将答案标记为已接受并投票。谢谢我试图将答案标记为已接受,但我得到“感谢您的反馈!记录声誉低于15的人所投的票,但不要更改公开显示的帖子分数。“对不起!确定没有问题,那么当您获得15个声誉时,请将问题标记为已接受:-)
<span class="badge pull-right">{{$category->posts()->published()->count()}}</span>