Reputation: 1
As a premise, the membership bulletin board is functioning, and the total of keywords such as "delicious" and "bad" in the posted content of the logged-in user can be acquired and displayed with the following code. Would you please teach me where to modify to list the total number of keywords posted by all authenticated users?
Route::get('/myfood', 'HomeController@myfood')->name('home.myfood');
Route::get('/foods', 'HomeController@foods')->name('home.foods');
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Post;
use App\Models\Comment;
use Auth;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function myfood()
{
$user = auth()->user()->id;
$myusers=User::where('id', $user)->get();
$PostsOisiCount = Post::where('user_id', $user)->where(function($query) {$query->where('body', 'like', '%delicious%')
->orWhere('body', 'like', '%bad%');})->count();
$PostsCount = Post::where('user_id', $user)->count();
return view('myfood', compact('user', 'myusers', 'PostsOisiCount', 'PostsCount'));
}
public function foods()
{
$users = User::all();
$PostsOisiCount = Post::where('body', 'like', '%delicious%')
->orWhere('body', 'like', '%bad%')
->count();
$PostsCount = Post::count();
return view('foods', compact('users', 'PostsOisiCount', 'PostsCount'));
}
}
<table>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Results</th>
</tr>
</thead>
<tbody>
@foreach($myusers as $myuser)
<tr>
<th>{{$myuser->id}}</th>
<td>{{$myuser->name}}</td>
<td>
($PostsOisiCount / $PostsCount ) * 100
</td>
</tr>
@endforeach
</tbody>
</table>
<table>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Results</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<th>{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>
($PostsOisiCount / $PostsCount ) * 100
</td>
</tr>
@endforeach
</tbody>
</table>
Upvotes: 0
Views: 103
Reputation: 8082
I will try to help you but first lets make your code more performant:
When you want to do a count
on a Model, you don't need to do ->get()->count()
as that is getting the data from the database (->get()
) and that could be HUGE, even running out of memory, and then counting the array values (->count()
). Instead of doing that, you can directly count on the database and the database will always do this way faster than any language. So your could would be: $total = Model::conditions()->count()
, $total
will directly have a positive (or 0
) integer number, and that's it!
Your code should be like:
$users = User::all();
$PostsOisiCount = Post::where('user_id', $users)->where('body', 'like', '%delicious%')->orWhere('body', 'like', '%bad%')->count();
$PostsCount = Post::where('user_id', $users)->count();
Have in mind that you are not currently getting the Logged in User's data, but EVERYONE data, because you are doing User::all()
, so you will get all IDs that are not soft deleted. Instead of doing that, you can still get everything by removing where('user_id', $users)
:
$PostsOisiCount = Post::where('body', 'like', '%delicious%')->orWhere('body', 'like', '%bad%')->count();
$PostsCount = Post::count();
To be able to just get data from authenticated
Users, that is absolutely way more complex than it seems and it depends a lot on your Authentication "driver"... Where are you storing or tracking sessions? If they are logged in, what should you read? So this is very dependant on your authentication implementation.
Upvotes: 3