This is My code,
search some products by elasticsearch use a packagist elasticquent:
$query = [
"body" => [
"query" => [
"function_score" => [
"query" => [
"multi_match" => [
"query" => $keyword,
"analyzer"=>"standard",
"type" => "best_fields",
"fields" => [
0 => "name^5",
1 => "content^1",
],
"fuzziness" => "AUTO",
"prefix_length" => 2
],
],
"field_value_factor" => [
"field" => "popular",
"modifier" => "log1p",
"factor" => "0.1",
],
"boost_mode" => "sum",
"max_boost" => "1.5",
],
],
],
];
$products = Product::complexSearch($query)->paginate(3);
//paging
$products->setPath('');
dd($products);
the result in browser is:
ElasticquentPaginator {#795 ▼
#total: 290
#lastPage: 97
#items: Collection {#790 ▼
#items: array:10 [▼
0 => Product {#801 ▶}
1 => Product {#804 ▶}
2 => Product {#807 ▶}
3 => Product {#779 ▶}
4 => Product {#789 ▶}
5 => Product {#785 ▶}
6 => Product {#791 ▶}
7 => Product {#782 ▶}
8 => Product {#798 ▶}
9 => Product {#788 ▶}
]
}
#perPage: 3
#currentPage: 1
#path: ""
#query: []
#fragment: null
#pageName: "page"
+"hits": array:3 [▶]
}
the Result number only 10, but the total is 290,and the paginate(3) ,3 is not work,what should i do ?
thank you!
Elasticsearch search size defaults to 10 as you can see in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html.
You can ask for more results by setting the size parameter to the desired number. Note that from version 2.0 onwards, you cannot set the size to >= 10000. If you wish to retrieve all the results that match your query you will have to use the scroll API: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html.
Related
I'm trying to calculate time difference in minutes via Laravel and Carbon.
My query: $findPauseWorky = ClockingSystemHours::where('total_time','=', null)->where('worky_id','=',$id)->whereNotNull('end_time')->get();
When I get my collection from my query the result looks like this:
Collection {#407
#items: array:1 [
0 => ClockingSystemHours {#408
#dates: array:3 [
0 => "start_time"
1 => "end_time"
2 => "total_time"
]
#original: array:8 [
"id" => 2
"start_time" => "16:21:29"
"end_time" => "23:20:52"
"total_time" => null
"worky_id" => 2
"employee_id" => 1
"created_at" => "2019-01-24 16:21:29"
"updated_at" => "2019-01-24 16:21:29"
]
As you can see in the result I've set my $dates in my model.
If i execute this dd($findPauseWorky->first()->start_time); it results in this:
Unexpected data found.
↵Unexpected data found.
↵Data missing
When i execute this: dd($findPauseWorky->first()->worky_id); it results in this :
2
What am I doing wrong? Or is there a reason why I cant access the data?
Maybe is not possible but i have in table answers for radio input with id of question and answer like id = 36 , answer = Yes. My code below get all answers
public function getAnswer() {
return $this->hasMany('App\Answer', 'user_id', 'id')
->with('question')
->whereHas('question.group', function($query) {
$query->where('name', 'simple_quesion');
});
}
and i have collection which is ok
#items: array:9 [▼
0 => getAnswer {#400 ▶}
1 => getAnswer {#401 ▶}
2 => getAnswer {#402 ▶}
but is any way to get keys and assign with this collection like
#items: array:9 [▼
35 => getAnswer {#400 ▶}
37 => getAnswer {#401 ▶}
42 => getAnswer {#402 ▶}
in this method public function getAnswer() ?
[UPDATE]
full collection
Collection {#396 ▼
#items: array:9 [▼
0 => getAnswer {#400 ▼
#fillable: array:3 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 18
"user_id" => 11
"answer_id" => 34
"answer" => "YES"
]
#original: array:7 [▶]
#changes: []
And i need key answer_id => 34
#items: array:9 [▼
34 => getAnswer {#400 ▼
Ok i am so stupid. This is so easy. In blade or controller i can use:
{{ dd($user->getAnswer->pluck('answer', 'answer_id')) }}
Try this:
In Answer model, add this:
public function question(){
return $this->belongsTo("App\Question"); //the column name of the question should be "question_id"
}
in Question model, do:
public function answers(){
return $this->hasMany("App\Answer");
}
And in your controller, where you want to get the question and its answers, you just do:
Question::with('answers')->all(); //assuming you want to get all the questions and their answers
I have the next collection:
Collection {#356 ▼
#items: array:31 [▼
0 => {#359 ▼
+"id": 17
+"zone_id": 2
+"name_de": "Österreich"
+"name_en": "Austria"
+"name_iso": "AUSTRIA"
+"tld": "at"
+"iso3166": "AT"
+"phone": 43
+"vat_regex": "/^U[0-9]{8}$/"
+"shop_id": 17
+"country_id": 165
}
1 => {#360 ▼
+"id": 2
+"zone_id": 2
+"name_de": "Belgien"
+"name_en": "Belgium"
+"name_iso": "BELGIUM"
+"tld": "be"
+"iso3166": "BE"
+"phone": 32
+"vat_regex": "/^[01][0-9]{9}$/"
+"shop_id": 17
+"country_id": 25
}]
}
And I want to get the next result as associative array:
[
"AT" => "Austria",
"BE" => "Belgium"
]
I'm trying to do it using:
$keyed = $countries->map(function ($item) {
return [$item->iso3166 => $item->name_en];
});
But I'm getting:
Collection {#357 ▼
#items: array:31 [▼
0 => array:1 [▼
"AT" => "Austria"
]
1 => array:1 [▼
"BE" => "Belgium"
]
]
}
What I'm doing wrong or how can I achieve the associative array?
Note: I'm using Laravel 5.2 so mapWithKeys() Collection method is not implemented.
You want to use function ->pluck('name_en', 'iso3166').
I just setting up Maatwebsite/Laravel-Excel plugin from here https://github.com/Maatwebsite/Laravel-Excel/. I want to read excel file and display data in Html table in a blade In laravel. I got datas by using following:
$content=Excel::load($file, function($reader) {
})->get();
when I echo $content; I have following ::
SheetCollection {#762 ▼
#title: ""
#items: array:3 [▼
0 => RowCollection {#623 ▼
#title: "Sheet1"
#items: array:4 [▼
0 => CellCollection {#693 ▼
#title: null
#items: array:2 [▼
"name" => "Abc"
"city" => "City1"
]
}
1 => CellCollection {#692 ▼
#title: null
#items: array:2 [▼
"name" => "Xyz"
"city" => "city2"
]
}
2 => CellCollection {#710 ▼
#title: null
#items: array:2 [▼
"name" => "Name2"
"city" => "city3"
]
}
3 => CellCollection {#709 ▼
#title: null
#items: array:2 [▼
"name" => "Name2"
"city" => "city3"
]
}
]
}
1 => RowCollection {#728 ▼
#title: "Sheet2"
#items: []
}
2 => RowCollection {#697 ▼
#title: "Sheet3"
#items: []
}
]
}
Those are data on my xlsx file . Now I want to render it on view blade . any Idea???? thanks
You can use the Laravel Collections methods to get data. Explore the methods but I think you can try something like this.
return $content->all();
or iterate each row and store them formated in your data to render it in a blade view using each.
$content->each(function($row){
// process here each row
});
I hope it can help you.
Can some please tell me how to display the results of this array in a view in Laravel 5?
RowCollection {#464 ▼
#title: "Worksheet"
#items: array:3 [▼
0 => CellCollection {#472 ▼
#title: null
#items: array:2 [▼
"name" => "Owen Kelley"
"age" => 29.0
]
}
1 => CellCollection {#473 ▼
#title: null
#items: array:2 [▼
"name" => "Jim Jackson"
"age" => 50.0
]
}
2 => CellCollection {#474 ▼
#title: null
#items: array:2 [▼
"name" => "Sally Anne"
"age" => 35.0
]
}
]
}
I have been struggling with this for ages!
Many thanks.
You should use foreach, example:
In controller/routes.php:
return view('yourviewname')->with('elephants', 'elephants')
In blade view:
#foreach($elephants as $elephant)
{{ $elephant->name }}
#endforeach