Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
PHP
Route::get('/myfood', 'HomeController@myfood')->name('home.myfood');
Route::get('/foods', 'HomeController@foods')->name('home.foods');


Controller:
PHP
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:
PHP
<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:
PHP
<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.
Posted
Updated 17-Apr-22 2:49am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900