Get object value from array in cache laravel - laravel

I have an array in Redis cache like that
127.0.0.1:6379> MGET laravel:campaign1107
1) "a:1:{s:21:\"unsubscriberCount1107\";i:2;}"
127.0.0.1:6379>
Now I need to get unsubscriber1107 value. I tried to this way
dd(cache()->get($arrayCacheKey[$cacheKey]));
but, it's doesn't work. How can I access this object?
My code for set cache
public function updateUnsubscriberCountCache($campaign_id, $type)
{
$assoc = [];
$arrayCacheKey = 'campaign'.$campaign_id.'';
$cacheKey = 'unsubscriberCount'.$campaign_id.'';
if($type == 'unsubscriberLogCount') {
$cacheKey = 'unsubscriberCount'.$campaign_id.'';
if( cache()->get($cacheKey) > 0) {
cache()->increment($cacheKey);
//cache()->forget($cacheKey);
} else {
$total = UnsubscribeLog::select('unsubscribe_logs.*')->leftJoin('tracking_logs', 'tracking_logs.message_id', '=', 'unsubscribe_logs.message_id')->where('tracking_logs.campaign_id', '=', $campaign_id)->distinct('subscriber_id')->count('subscriber_id');
//cache()->forever($cacheKey, $total);
$assoc[$cacheKey] = $total;
cache()->forever($arrayCacheKey, $assoc);
}
}
}

You're storing the value as an array using $arrayCacheKey but earlier in the code you're trying to access it using the $cacheKey which has a different value.
If you want to get the value of unsubscriber1107 you will need to use a combination of both keys:
$campaignData = cache()->get($arrayCacheKey); //To get the array value from the cache
$count = $campaignData ? $campaignData[$cacheKey] : null; //get the count
The above assumes that the va

Related

Foreach loop is showing error while storing multiple id's

I am creating a group and also storing users id's in it but its showing error in foreach loop i.e. Invalid argument supplied for foreach().
Here is my controller code :
public function createGroup(Request $request)
{
$user_id = request('user_id');
$member = request('member');
$data = array(
'name'=>$request->name,
);
$group = Group::create($data);
if($group->id)
{
$resultarr = array();
foreach($member as $data){
$resultarr[] = $data['id'];
}
$addmem = new GroupUser();
$addmem->implode(',', $resultarr);
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
return $this->sendSuccessResponse([
'message'=>ResponseMessage::statusResponses(ResponseMessage::_STATUS_GROUP_SUCCESS)
]);
}
}
I am adding values like this,
Desired Output,
I just want that each member to store with different id's in table and group id will be same.
Please help me out
Avoid that if check, it does absolute nothing.
if($group->id)
Secondly your input is clearly a string, explode it and you will have the expected results. Secondly don't save it to a temporary variable, create a new GroupUser immediately.
foreach(explode(',', $member) as $data){
$addmem = new GroupUser();
$addmem->user_id = $data;
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
}
That implode line makes no sense at all, i assumed there is a user_id on the GroupUser relation.
u need to send array from postman
like
Key | value
member[] | 6
member[] | 3
or
$memberArray = explode(",", $member = request('member'))
if($group->id)
{
$resultarr = array();
foreach($memberArray as $data){
$resultarr[] = $data['id'];
}
$addmem = new GroupUser();
$addmem->implode(',', $resultarr);
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
return $this->sendSuccessResponse([
'message'=>ResponseMessage::statusResponses(ResponseMessage::_STATUS_GROUP_SUCCESS)
]);
}

Undefined variable: data", exception: "ErrorException",…}

I have an alias in this code that is RIGHT (no_antrian, 3) as code
when I call the alias with $ code, laravel says Undefined variable: data ", exception:" ErrorException ", ...}
$result = DB::table('data_antrian')->select(DB::raw('RIGHT(no_antrian,3) as kode'))->where('tanggal', '=', $tanggal)->orderBy('id', 'desc')->take(1)->get();
$rows = DB::table('data_antrian')->where('tanggal', '=', $tanggal)->count();
if ($rows > 0) {
// $data = mysql_fetch_assoc($result);
// $kode = $data['kode']+1;
$kode = $data['kode']+1;
} else {
$kode = '001';
}
You do not have data defined, meanwhile also you treat your $result variable as a single entity and it is a collection. Therefor i think this is what you want to do.
$result = DB::table('data_antrian')->select(DB::raw('RIGHT(no_antrian,3) as kode'))->where('tanggal', '=', $tanggal)->orderBy('id', 'desc')->take(1)->first();
$rows = DB::table('data_antrian')->where('tanggal', '=', $tanggal)->count();
if ($rows > 0) {
$kode = $result->kode + 1;
} else {
$kode = '001';
}
In the first query instead of get(), i only get a single result which you can since you only take(1). Using result instead of data and using it as an object since it is an object.

How to check the value on the array cart

I have an array cart on codeigniter and creates a function to separate if there is engrave or not on the array like this,
$cart_count = 0;
$cart_cek = $this->cart->contents();
foreach ($cart_cek as $value2) {
if($cart_count >= 1){
break;
}
else{
if($value2['engrave-text'] != ""){
$order_id = $this->generate_order_id_costume();
$cart_count++;
}
else{
$order_id = $this->generate_order_id();
$cart_count++;
}
}
}
but when I run the function it always goes into else with value
$order_id = $this->generate_order_id();
even though there is engrave in the cart array.
For example, I have 3 items in the cart that each have the engrave field. How do I know that there are at least 1 engrave field filled in all the carts?
If you want to check that if the cart contains at least one engrave-text field is filled, you could use array_column to get the engrave-text arrays, then array_filter to check if it's empty :
$cart_cek = $this->cart->contents();
if (!empty(array_filter(array_column($cart_cek, 'engrave-text')))) { // if at least one 'engrave-text' has a value, call generate_order_id_costume() method
$order_id = $this->generate_order_id_costume();
} else { // otherwise call generate_order_id() method
$order_id = $this->generate_order_id();
}
First of all, you don't need to check the count to stop the loop and try using isset().
Try this if you want to check if $this->cart->contents(); has at least 1 engrave-text object.
$cart = $this->cart->contents();
foreach ($cart as $cart_value) {
if(isset($cart_value['engrave-text'])){
$order_id = $this->generate_order_id_costume();
} else {
$order_id = $this->generate_order_id();
}
}

Change the Value of an existing Laravel Collection key

I have a Laravel Collection $page in which I am trying to update some values based on a value in another collection. But instead up updating a new element is being added to $page.
if($page->get('Lang') !== null ){
$l = $page->get('Lang');
foreach ($l as $thisLang => $value) {
if($lang == $thisLang){
// this is where I am attempt is overwrite the variable
$page->Name = $value->Name;
break;
}
}
}
Being fairly new to Laravel I am not sure what I am doing wrong. Any code corrections are appreciated.
I have also tried :
$page->update( ["Name" , $value->Name] );
but get the error
"Method Illuminate\Support\Collection::update does not exist."
Instead of use $page->Name need use $page['Name'].
Corrected code is
if($page->get('Lang') !== null ){
$l = $page->get('Lang');
foreach ($l as $thisLang => $value) {
if($lang == $thisLang){
// this is where I am attempt is overwrite the variable
$page['Name'] = $value->Name;
break;
}
}
}

only one value is get use of explode variable in get method

i have use explode variable to get the value using get method but that return only one value
$part_id=explode(",",$request->input('part_id'));
$get_ltr = part::where('status',1)->where('part_no',$part_id)->get();
foreach($get_ltr as $key =>$value)
{
$ltr[$key] = $value['ltr'];
}
that gives me only one record what is issue in my code please help me
Look like you should use whereIn().
$ids = explode(',', $request->input('part_id'));
$get_ltr = part::where('status',1)->whereIn('part_no', $ids)->get();
foreach($get_ltr as $key =>$value){
$ltr[$key] = $value['ltr'];
}
You should try this:
$color_id=explode(",",$request->input('color_id'));
$part_id=explode(",",$request->input('part_id'));
$qty=explode(",",$request->input('qty'));
$qty_ltr = [30,15];
$get_ltr = part::where('status',1)->whereIn('part_no',$part_id)->get();
foreach($get_ltr as $key =>$value)
{
$ltr[$key] = $value['ltr'];
}

Resources