"As shown in the code below, the total of multiple strings such as ""delicious"" and ""delicious"" in the body column of the DB table can be displayed on the Blade, but if there are multiple words such as ""delicious"" and ""delicious"" in one post, it will be counted as multiple."
I apologize for the inconvenience, but I would appreciate it if you could tell me how to correct it.Thank you for your cooperation.
controller:
$posts=DB::table('Post') ->select(db::raw("body,
(LENGTH(body)-LENGTH(REPLACE(body, 'delicious', ')))/LENGTH('delicious')AS cntA,
(LENGTH(body)-LENGTH(REPLACE(body, 'bad', ')))/LENGTH('bad')AS cntB,
")->distinct()->where('user_id', $user)->get();
Blade:
<p>"Delicious" was {{$posts->sum('cntA')}} times in total.</p>
<p> "bad" was {{$posts->sum('cntB')}} times in total.</p>
If you want body
to count the number of records that contain 'delicious'
once or more, you can count the number of body like'%delicious%'
sum(body like'% delicious%').
However, depending on the number of lines in the record and the size of the body
can be heavy, so if that happens, you may want to consider doing something other than RDBMS, such as deploying a full-text search engine.
© 2024 OneMinuteCode. All rights reserved.