// This works$messages =Message::where('read_at is null')->get();// Won’t work - will return 0 messages$messages =Message::all();$unread_messages = $messages->where('read_at is null')->count();// Will work$unread_messages = $messages->where('read_at','')->count();
// How to get sum of post_views with pagination?$posts =Post::paginate(10);// This will be only for page 1, not ALL posts$sum = $posts->sum('post_views');// Do this with Query Builder$query =Post::query();// Calculate sum$sum = $query->sum('post_views');// And then do the pagination from the same query$posts = $query->paginate(10);
分页组件中的唯一标识
我们可以在分页组件中像序列号那样使用每趟循环中的索引 index ,作为分页组件的唯一标识。
...<th>Serial</th>...@foreach ($products as $product)<tr><td>{{ $loop->index + $product->firstItem() }}</td>...@endforeach
$key ='name';// Still validif ($this->collection->has($key)===false) {$this->collection->put($key,...);}return$this->collection->get($key);// Using the `getOrPut()` method with closurereturn$this->collection->getOrPut($key,fn() => ...);// Or pass a fixed valuereturn$this->collection->getOrPut($key, $value='teacoders');