Laravel get cache key using regex or like operator - laravel

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;

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

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

Laravel query in if cause

I run the below query, if cause true then where cause is apply like below, but when i run below query it gives me "Out of memory (allocated 503316480) (tried to allocate 492834816 bytes)" error, So another easy way to check the where cause above if condition.
Here below is my code.
$user_id = "";
$doctor_id = "";
$start_date = "";
$end_date = "";
if(isset($request->user_id)) {
$user_id = $request->user_id;
}
if(isset($request->doctor_id)) {
$doctor_id = $request->doctor_id;
}
if(isset($request->start_date)) {
$start_date = $request->start_date;
}
if(isset($request->end_date)) {
$end_date = $request->end_date;
}
$query = DB::table('request_approves')
->join('city_masters', 'request_approves.city_id', '=', 'city_masters.id')
->join('doctors', 'request_approves.doctor_id', '=', 'doctors.id')
->select('doctors.*','city_masters.*','request_approves.*');
if($user_id !== "") {
$query->where('request_approves.user_id', 2);
}
if($doctor_id !== "") {
$query->where('request_approves.doctor_id',3);
}
if($start_date !== "" & $end_date !==""){
$query->whereBetween('request_approves.approve_time', array($start_date,$end_date));
}
$query->get();
Not sure what it is happening, but your code seems wrong to me .
On the if statements you do something like this $query->where('request_approves.user_id', 2); but where this returned values go ? To continue the same query you should do it like this:
$query = DB::table('request_approves')
->join('city_masters', 'request_approves.city_id', '=', 'city_masters.id')
->join('doctors', 'request_approves.doctor_id', '=', 'doctors.id')
->select('doctors.*','city_masters.*','request_approves.*');
if($user_id !== "") {
$query = $query->where('request_approves.user_id', 2);
}
if($doctor_id !== "") {
$query = $query->where('request_approves.doctor_id',3);
}
if($start_date !== "" & $end_date !==""){
$query = $query->whereBetween('request_approves.approve_time', array($start_date,$end_date));
}
$query = $query->get();
PHP uses memory to perform queries as well. So performing queries from a PHP application will indeed use more memory than performing them straight from MySQL.
I did a simple calculation and 503316480 bytes comes out to 503MB. So I would suggest modifying your php.ini settings. This should let you output data, and from there you can tune your SQL.
In your php.ini
memory_limit=503M

Laravel Eloquent ORM save: update vs create

I've got a table and I'm trying to use the save() method to update/create lines. The problem is that it always create new lines. The following code gives me about 12 lines each time I run it. They don't have a unique id before inserting them but the combination of name and DateTimeCode is unique. Is there's a way to use those two in order for laravel to recognize them as exist()? Should I consider using if exist with create and update instead?
foreach ($x as $y) {
$model = new Model;
$model->name = ''.$name[$x];
$model->DateTimeCode = ''.$DateTimeCode[$x];
$model->value = ''.$value[$x];
$model->someothervalue = ''.$someothervalue[$x];
$model->save();
};
I assume this would work in laravel 4.x:
foreach ($x as $y) {
$model = Model::firstOrCreate(array('name' => name[$x], 'DateTimeCode' => DateTimeCode[$x]));
$model->value = ''.$value[$x];
$model->someothervalue = ''.$someothervalue[$x];
$model->save();
};
I think that in this case you will need to search for it before updating or creating:
foreach ($x as $y) {
$model = Model::where('name', name[$x])->where('DateTimeCode', DateTimeCode[$x])->first();
if( ! $model)
{
$model = new Model;
$model->name = ''.name[$x];
$model->DateTimeCode = ''.DateTimeCode[$x];
}
$model->value = ''.$value[$x];
$model->someothervalue = ''.$someothervalue[$x];
$model->save();
};

Propel to Doctrine Code Snippets

This is a totally newbie question, so please bear with me. I am learning symfony from the online Jobeet and Askeet tutorials, but most of my hacks have involved Doctrine, so I am not familiar at all with Propel. I have managed so far by researching online and modifying to fit my needs, but I need a little help here.
Could someone give me a hand in translating these code snippets into Doctrine?
public function setTag($v)
{
parent::setTag($v);
$this->setNormalizedTag(Tag::normalize($v));
}
public function getTags()
{
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(QuestionTagPeer::NORMALIZED_TAG);
$c->add(QuestionTagPeer::QUESTION_ID, $this->getId());
$c->setDistinct();
$c->addAscendingOrderByColumn(QuestionTagPeer::NORMALIZED_TAG);
$tags = array();
$rs = QuestionTagPeer::doSelectRS($c);
while ($rs->next())
{
$tags[] = $rs->getString(1);
}
return $tags;
}
public function getPopularTags($max = 5)
{
$tags = array();
$con = Propel::getConnection();
$query = '
SELECT %s AS tag, COUNT(%s) AS count
FROM %s
WHERE %s = ?
GROUP BY %s
ORDER BY count DESC
';
$query = sprintf($query,
QuestionTagPeer::NORMALIZED_TAG,
QuestionTagPeer::NORMALIZED_TAG,
QuestionTagPeer::TABLE_NAME,
QuestionTagPeer::QUESTION_ID,
QuestionTagPeer::NORMALIZED_TAG
);
$stmt = $con->prepareStatement($query);
$stmt->setInt(1, $this->getId());
$stmt->setLimit($max);
$rs = $stmt->executeQuery();
while ($rs->next())
{
$tags[$rs->getString('tag')] = $rs->getInt('count');
}
return $tags;
}
public static function getTagsForUserLike($user_id, $tag, $max = 10)
{
$tags = array();
$con = Propel::getConnection();
$query = '
SELECT DISTINCT %s AS tag
FROM %s
WHERE %s = ? AND %s LIKE ?
ORDER BY %s
';
$query = sprintf($query,
QuestionTagPeer::TAG,
QuestionTagPeer::TABLE_NAME,
QuestionTagPeer::USER_ID,
QuestionTagPeer::TAG,
QuestionTagPeer::TAG
);
$stmt = $con->prepareStatement($query);
$stmt->setInt(1, $user_id);
$stmt->setString(2, $tag.'%');
$stmt->setLimit($max);
$rs = $stmt->executeQuery();
while ($rs->next())
{
$tags[] = $rs->getString('tag');
}
return $tags;
}
I recommend you to forget Askeet tutorial, it is for the 1.0 release, and lots of things change since this version.
But, you can find a svn dump of a doctrine version of Askeet. You should use SVN to rebuild the repo (don't know how to perform that).
On an other hand, if you need to handle tag in sf1.4 project, I recommend you to use the plugin sfDoctrineActAsTaggablePlugin.

Resources