Migration class CreatePostsTable extends Migration
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->text('number')->nullable();
$table->text('text');
$table->boolean('is_approved')->default(0);
$table->timestamp('published_at');
$table->timestamps();
$table->softDeletes();
});
}
model Post
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
protected $fillable = [
'text',
'number'
];
protected $dates = ['published_at', 'deleted_at'];
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::now('Europe/Moscow');
}
public function scopeApproved($query)
{
$query->where('is_approved', '==', '1');
}
}
Controller HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Post;
class HomeController extends Controller
{
public function index() {
$posts = Post::latest('published_at')->approved()->paginate(20);
return view('home.index', compact('posts'));
}
}
So it is expected that on the Home page I receive only approved posts but instead all posts are shown on the page. What's wrong?