0
遷移class CreatePostsTable extends Migration
Laravel範圍不起作用
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();
});
}
模式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');
}
}
控制器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'));
}
}
所以預計在主頁上我只接收approved
帖子但是所有帖子都顯示在頁面上即怎麼了?
那麼,在以前版本的Laravel'$ query-> where('is_approved','==','1');'工作得很好,我不知道它改變了。非常感謝幫助,'$ query-> where('is_approved',1);'起作用! – Heidel
很高興幫助。 :)我真的不知道什麼時候可能會改變。我現在可以訪問的環境中最老的版本是5.2,所以它一定在此之前。 –