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

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'];
}

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)
]);
}

Get object value from array in cache 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

Laravel get cache key using regex or like operator

I want to get Laravel Redis cache key using regex or like key operator.
I have tried everything and spend lost of time but no luck with that can anyone please help on this.
$redis = Cache::getRedis();
$keys = $redis->keys("*$key_name*");
$count = 0;
$result = [];
foreach ($keys as $key) {
$result[$key] = $redis->get($key);
}
return $result;
I have tried above code but no luck. can any one help on this?
You can implement something like
$redis = Cache::getRedis();
$keys = $redis->keys("*$key_name*");
$count = 0;
$result = [];
foreach ($keys as $key) {
$result[$key] = $redis->get($key);
}
return $result;

Iterating through result set in Laravel controller

I am trying to simply iterate though a result set in laravel on the controller side. This is what I tried but I get the following error:
Cannot use object of type stdClass as array
Controller snippet:
$result = DB::select($query);
foreach($result as $r){
echo $r['email'];
}
I appreciate any help with this,
Thanks in advance!
You need to use it as an object:
$result = DB::select($query);
foreach($result as $r){
echo $r->email;
}
Or if for some reason you want to use it as array, you need to convert it first:
$result = DB::select($query)->toArray();
foreach($result as $r){
echo $r['email'];
}
After pulling data from database you can convert it to array but it is optional, it's depends on your necessity then you can simply use the foreach to loop through to each distinct object and get access their property
$data = DB:: table('categories')
->where('parent_id', $request->value)
->get()->toArray();
$output = '<option value="">Select '.ucfirst($request->dependent).'</option>';
foreach($data as $row){
$output.= '<option value="'.$row->id.'">'.$row->title.'</option>';
}
return response($output, 200);
The Laravel Collection class supports the standard methods for iterables: map, sort, filter etc. see: vendor/laravel/framework/src/Illuminate/Collections/Collection.php
if you're querying on a model:
$results = MyAwesomeModel::where('something' '=' 'other')->get();
//this is valid
$results->map(function($result) {
echo $result->field;
});
//this is also valid
for($i = 0; $i < sizeof($results); $i++){
echo $results[$i]->field;
}
I would suggest avoiding ->toArray() on hydrated results (instances of models) because you loose all the model properties.

How to concatenate variables comes from model in controller

I need to concatenate variables come from model.I send the role_id from controller to model and get the role name according to its id.
controller:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
foreach($data['rec'] as $i)
{
$name['x']=$this->amodel->get_name($i->role_id);
}
$this->load->view('sections',array_merge($data,$name));
}
I write $name['x'].=$this->amodel->get_name($i->role_id); but it shows the error which undefined index:x.How can I concatenate the role name in cotroller to send it to view?
you probably didnt define $name
$name = array();
// your foreach
foreach ()
btw, the concatenating was ok
$var = "foo";
$var .= "foo"
// will result in "foofoo"
If you want to append something using the .= syntax you need to make sure the variable or array exists first.
Try this:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
$name = array();
foreach($data['rec'] as $i) {
if (isset($name['x'])) {
$name['x'] .= $this->amodel->get_name($i->role_id);
} else {
$name['x'] = $this->amodel->get_name($i->role_id);
}
}
$this->load->view('sections',array_merge($data,$name));
}

Resources