Laravel Mongodb - Integer value with like operator is not working - laravel

I am using this package for my laravel application for the mongodb database: https://github.com/jenssegers/laravel-mongodb
Now, I have and id column which contain integer value like: 32312794972256
Now, I am searching like this:
$filtered = array_filter( $request->all());
$trimmed_array = array_map('trim', $filtered);
$new_arr = [];
foreach( $trimmed_array as $k => $v ) {
if( $k !== 'page' && $k !== 'projectToken' && $k !== 'userId' ) {
if( is_numeric( $v) ) {
$v = (int) $v;
}
$new_arr[] = [$k, 'LIKE', '%'. $v .'%' ];
}
}
Log::info( $new_arr );
$projectToken = $request->input('projectToken');
$userId = $request->input('userId');
$this->set_connection( $projectToken, $userId );
$get_project_id = DB::connection('mysql')->table('projects')->where('token', $projectToken )->get(['id_project'])->first();
$collection = 'products_' . $get_project_id->id_project;
$collection = 'products_103';
if(count($new_arr)) {
$search = DB::connection('mongodb')->collection( $collection )->where($new_arr)->paginate(100);
} else {
$search = DB::connection('mongodb')->collection( $collection )->paginate(100);
}
But above search query is not showing any result for this integer search. Do you know why?

Related

Getting my values from an if statement to show outside of the if statement in laravel

I'm trying to create a command that lets me write to a json file. I'm able to do that but I'm struggling to get some values out of my if statement.
Here is my code
foreach($update as $key => $value)
{
$name = '';
$hours = '';
if($value != 0)
{
$name = $key;
$hours = $value;
}
$array = [
$name => $hours
]
}
but when I run my code I get a blank $name and a blank $hours
You're always getting last record. Also, you can ignore redundant values in your data.
I guess these lines can help you
foreach($update as $key => $value)
{
if ($value == 0) {
continue;
}
$name = $key;
$hours = $value;
$array[$name] = $hours;
}
maybe these lines help you
$update = [1 => 'a', 2 => 'b', 3 => 'c'];
foreach ($update as $key => $value) {
if ($value !== 0) {
$name = $key;
$hours = $value;
$array[$name] = $hours; // $array[]= [$name=>$hours];
}
}
note : if $update array values contain strings will return true at all because at php
('a' == 0) will return true for that i used !==

how to make count data

public function index()
{
$yek = $this->input->post("KEY");
$cek = $this->m_user->get('KEY' => $yek)->num_rows();//line 14
$response = array();
if ($cek > 1)
{
$list = $this->m_masalah->jenis();
}
foreach ($list as $key ) {
$arr = array();
$arr['ID_MASALAH'] = $key['ID_MASALAH'];
$arr['MASALAH'] = $key['MASALAH'];
$arr['JENIS'] = $key['JENIS'];
array_push($response, $arr);
}
echo $json_response = json_encode($response);
}
this is my erorr
Parse error: syntax error, unexpected '=>'
(T_DOUBLE_ARROW), expecting ',' or ')' in
E:\xampp\htdocs\sengketa_dpbt\application\controllers\Masalah.php on line 14
The => symbol is used for assigning array elements, but you didn't declare it as an array here. Just make this change:
$cek = $this->m_user->get(array('KEY' => $yek))->num_rows();

SQLSTATE[23000]

I have a trouble about phpmyadmin of laravel
I want to update the data from the view on the db via controller but I get an error occurred
// save all seats
foreach ($request->all() as $key => $param) {
if ($key === '_token') continue;
$data = explode('-', $key);
$bs = new BookedSeat();
$bs->booking_id = $booking->id;
$bs->day_id = str_replace('c', '', $data[0]);
$bs->table_id = str_replace('t', '', $data[1]);
$bs->number = 0;
$bs->status = 'requested';
$bs->save();
}
its an error when i try update data
its my db
Updated
// save all seats
foreach ($request->all() as $key => $param) {
if ($key === '_token') continue;
$data = explode('-', $key);
$bs = new BookedSeat();
$bs->booking_id = $booking->id;
$day = DB::table('days')->where('name',$data[0])->get();
$bs->day_id = $day->id;
$bs->table_id = str_replace('t', '', $data[1]);
$bs->number = 0;
$bs->status = 'requested';
$bs->save();
}

Elastica Filter and Query Array

I'm using a form for an advanced search. The form inputs are representative of the data in the elasticsearch index. My model receives an array of filter terms and query terms.
$data = array(
'Filter' => array(
'FilerId' => 14592
),
'Query' => array(
'FiledDate' => '2015-08-06',
),
);
I'm using a foreach loop to create the filter and query
foreach ($data['Filter'] AS $field => $value) {
$filter = new \Elastica\Filter\Term();
$filter->setTerm($field, $value);
$filterArray[] = $filter;
}
foreach ($data['Query'] AS $field => $value) {
$query = new \Elastica\Query\QueryString($value);
$query->setDefaultOperator('AND')
->setDefaultField($field);
$queryArray[] = $query;
}
$query = new \Elastica\Query();
$query
->setFields(['TranId'])
->setQuery($queryArray)
->setFilter($filterArray);
$search->setQuery($query);
$numberOfEntries = $search->count();
$comma_separated = 0;
if ($numberOfEntries) {
foreach ($search->scanAndScroll() as $scrollId => $resultSet) {
$results = $resultSet->getResults();
$totalResults = $resultSet->getTotalHits();
foreach ($results as $result) {
$fields = $result->getFields('TransId');
$transid[] = $fields['TranId'][0];
} // ... handle Elastica\ResultSet
}
$comma_separated = implode(", ", $transid);
}
return array('transactions' => $comma_separated, 'total' => $totalResults);
I am getting an error and I can't find the reason why?
Here is some updated code. I'm getting results but not what I thought I should get.
$boolFilter = new \Elastica\Filter\BoolFilter();
foreach ($data['Filter'] AS $field => $value) {
$term = new \Elastica\Filter\Term();
$term->setTerm($field, $value);
$boolFilter->addMust($term);
}
$boolQuery = new \Elastica\Query\BoolQuery();
foreach ($data['Query'] AS $field => $value) {
$match = new \Elastica\Query\Match();
$match->setFieldQuery($field, $value)
->setFieldAnalyzer($field, 'whitespace')
->setFieldOperator($field, 'AND');
$boolQuery->addMust($match);
}
$query = new \Elastica\Query();
$query
->setFields(['TranId'])
->setQuery($boolQuery)
->setFilter($boolFilter);
//print $error->getError();
print "<pre>";
print_r ($query->toArray());
print "</pre>";
$search->setQuery($query);
$numberOfEntries = $search->count();
$comma_separated = 0;
if ($numberOfEntries) {
foreach ($search->scanAndScroll() as $scrollId => $resultSet) {
$results = $resultSet->getResults();
$totalResults = $resultSet->getTotalHits();
foreach ($results as $result) {
$fields = $result->getFields('TransId');
$transid[] = $fields['TranId'][0];
} // ... handle Elastica\ResultSet
}
$comma_separated = implode(", ", $transid);
}
return array('transactions' => $comma_separated, 'total' => $totalResults);
Alright! I needed to add the Standard analyzer instead of the whitespace analyzer. The whitespace analyzer works but only breaks the phrase up on spaces. The standard analyzer breaks the phrase up and make the case lower.
https://www.elastic.co/guide/en/elasticsearch/guide/current/analysis-intro.html#analyze-api
$boolFilter = new \Elastica\Filter\BoolFilter();
foreach ($data['Filter'] AS $field => $value) {
$term = new \Elastica\Filter\Term();
$term->setTerm($field, $value);
$boolFilter->addMust($term);
}
$boolQuery = new \Elastica\Query\BoolQuery();
foreach ($data['Query'] AS $field => $value) {
$match = new \Elastica\Query\Match($value);
$match->setFieldQuery($field, $value);
$match->setFieldAnalyzer($field, 'standard');
$boolQuery->addMust($match);
}
$filterQuery = new \Elastica\Query\Filtered();
$filterQuery->setFilter($boolFilter);
$filterQuery->setQuery($boolQuery);
$query = new \Elastica\Query($filterQuery);
$query->setFields(['TranId']);
print "<pre>";
print_r ($query->toArray());
print "</pre>";
$search->setQuery($query);
$numberOfEntries = $search->count();
$comma_separated = 0;
if ($numberOfEntries) {
foreach ($search->scanAndScroll() as $scrollId => $resultSet) {
$results = $resultSet->getResults();
$totalResults = $resultSet->getTotalHits();
foreach ($results as $result) {
$fields = $result->getFields('TransId');
$transid[] = $fields['TranId'][0];
} // ... handle Elastica\ResultSet
}
$comma_separated = implode(", ", $transid);
}
return array('transactions' => $comma_separated, 'total' => $totalResults);

Laravel url_title helper

Is there a Laravel 4 equivalent to the codeigniter url_title() function? Or should I just copy it over?
For reference this is the codeigniter one:
function url_title($str, $separator = '-', $lowercase = FALSE)
{
if ($separator == 'dash')
{
$separator = '-';
}
else if ($separator == 'underscore')
{
$separator = '_';
}
$q_separator = preg_quote($separator);
$trans = array(
'&.+?;' => '',
'[^a-z0-9 _-]' => '',
'\s+' => $separator,
'('.$q_separator.')+' => $separator
);
$str = strip_tags($str);
foreach ($trans as $key => $val)
{
$str = preg_replace("#".$key."#i", $val, $str);
}
if ($lowercase === TRUE)
{
$str = strtolower($str);
}
return trim($str, $separator);
}
well I would vote for using something little better adjusted to your need and laravel itself https://github.com/MattHans0n/slug
how to use packages you could read on that page http://laravel.com/docs/packages

Resources