my phpadmin database I have data
i want to get data where price is yes
Solutions
$filteredDatas = [];
foreach ($data as $profile) {
$profileStatus = json_decode($profile->profile_status, true);
if (isset($profileStatus['price']) && $profileStatus['price'] == 'yes') {
$filteredDatas[] = $profile;
}
}
Explanation
Suppose you have an array of profiles stored in the $data variable. Each profile object has various properties, and one of them is profile_status, which is a JSON-encoded string. The goal is to filter profiles based on the condition that the 'price' in the decoded JSON should be set to 'yes'.
Here's an example with two profiles:
$data = [
(object)[
'id' => 1,
'name' => 'John',
'profile_status' => '{"price": "yes", "social_media": "no"}',
],
(object)[
'id' => 2,
'name' => 'Jane',
'profile_status' => '{"price": "no", "social_media": "yes"}',
],
];
Now, let's walk through the loop:
First Iteration (John's Profile):
- $profile is the first profile object (John's profile).
- $profileStatus is the result of decoding the JSON string in profile_status.
The condition isset($profileStatus['price']) && $profileStatus['price'] == 'yes' is true because 'price' is set to 'yes' in John's profile
.
Therefore, John's entire profile is added to $filteredDatas.
Second Iteration (Jane's Profile):$profile is the second profile object (Jane's profile).
$profileStatus is the result of decoding the JSON string in profile_status.
The condition is false for Jane because 'price' is not set to 'yes'.
Jane's profile is not added to $filteredDatas.
After the loop, $filteredDatas will contain an array with profiles that meet the specified condition (in this case, profiles where 'price' is set to 'yes')
. In this example, it would only contain John's profile.
my controller function
public function influencerProfileDashboard(Request $request)
{
log::info("influencerProfileDashboard ()");
$currentURL = url()->current();
$login_email = Auth::user()->email;
$profiles = Addprofile::all();
$prices = [];
$data = DB::table('addprofiles')
->leftJoin('countries', 'addprofiles.country_id', '=', 'countries.country_id')
->leftJoin('states', 'addprofiles.state_id', '=', 'states.state_id')
->leftJoin('cities', 'addprofiles.city_id', '=', 'cities.city_id')
->leftJoin('users', 'addprofiles.user_id', '=', 'users.id')
->where('addprofiles.user_email', '<>', $login_email)
->select('addprofiles.*', 'countries.country_name', 'states.state_name', 'cities.city_name', 'users.name', 'users.email', 'addprofiles.file_pic')
->orderBy('id', 'desc')
->get();
$filteredDatas = [];
foreach ($data as $profile) {
$profileStatus = json_decode($profile->profile_status, true);
if (isset($profileStatus['price']) && $profileStatus['price'] == 'yes') {
$filteredDatas[] = $profile;
}
}
Log::info('Filtered Data:', ['data' => $filteredDatas]);
$filteredData = collect($filteredDatas)->filter(function ($item) {
return !empty($item->facebook) || !empty($item->twitter) || !empty($item->youtube) || !empty($item->wordpress) || !empty($item->tumblr) || !empty($item->instagram) || !empty($item->quora) || !empty($item->pinterest) || !empty($item->reddit) || !empty($item->koo) || !empty($item->scoopit) || !empty($item->slashdot) || !empty($item->telegram) || !empty($item->fb_grp) || !empty($item->linkedin_grp) || !empty($item->linkedin) || !empty($item->roposo) || !empty($item->chingari) || !empty($item->mitron);
});
$perPage = 5;
$currentPage = $request->input('page', 1);
$offset = ($currentPage - 1) * $perPage;
$limit = $perPage;
$slicedData = $filteredData->slice($offset, $limit);
$paginator = new LengthAwarePaginator(
$slicedData,
$filteredData->count(),
$perPage,
$currentPage,
['path' => $request->url(), 'query' => $request->query()]
);
$previousPageUrl = $paginator->previousPageUrl();
$nextPageUrl = $paginator->nextPageUrl();
return view('pages.influencersd',compact('paginator','previousPageUrl','nextPageUrl','filteredData','currentURL'));
}
Top comments (0)