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?
Web.php:
Route::get('/myfood', 'HomeController@myfood')->name('home.myfood');
Route::get('/foods', 'HomeController@foods')->name('home.foods');
Controller:
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'));
}
}
myfoodBlade:
<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>
foodsBlade:
<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>
What I have tried:
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.