我想查看在 Carbon::now 的同一个月创建的 post

I want to see the post created in the same month of Carbon::now

public function showallcategoryfood(){
$carbon = Carbon::now(); 
$foods=Food::all();
DB::table('food')->whereMonth('created_at','=',$carbon);
return view('test.logout')->with('foods',$foods);
}

一直显示所有记录,6月份还没拿!

all() 方法 returns 集合表示的底层数组。

试试这个:

public function showallcategoryfood(){
   $carbon = Carbon::now()->format('m');
   $foods=Food::whereMonth('created_at', '=', $carbon)->get();
   return view('test.logout')->with('foods', $foods);   
}

你读过docs了吗?

The whereMonth method may be used to compare a column's value against a specific month of a year:

$users = DB::table('users')
                ->whereMonth('created_at', '12')
                ->get();

所以在你的情况下它将是:

$users = DB::table('users')
                ->whereMonth('created_at', Carbon::now()->month)
                ->get();