php


Call to undefined method Illuminate\Database\Query\Builder::map() in laravel 5.4


My product controller:
public function category(Request $request, $name, $main){
if($request->data){
$explode_id = array_map('intval', explode(',', $request->data));
$category_id = Category::where('name', $name)->value('id');
if($category_id == "")
{
$all_categories_id = Category::pluck('id');
}
else
{
$all_categories_id = Category::where('parent_id', $category_id)->pluck('id');
$all_categories_id->push($category_id);
}
$product_id = Product::where('name', 'like','%'.$main.'%')->whereIn('id', $explode_id)->pluck('id');
$id = ProductCategory::whereIn('product_id', $product_id)->whereIn('category_id', $all_categories_id)->pluck('id');
$products = Product::find($id);
}
else{
$category_id = Category::where('name', $name)->value('id');
if($category_id == "")
{
$all_categories_id = Category::pluck('id');
}
else
{
$all_categories_id = Category::where('parent_id', $category_id)->pluck('id');
$all_categories_id->push($category_id);
}
$product_id = Product::where('name', 'like','%'.$main.'%')->pluck('id');
$id = ProductCategory::whereIn('product_id', $product_id)->whereIn('category_id', $all_categories_id)->pluck('id');
$products = Product::find($id);
}
//Categories Name in Sidebar
$category = ProductCategory::whereIn('product_id', $id)->pluck('parent_id');
$category_name = Category::whereIn('id', $category)->pluck('name');
//Categories Name in dropdown
$main_categories = Category::where('parent_id', '0')->pluck('name');
$user = Auth::user();
$directory = 'uploads/users/images/'.$user->id;
$main_categories = Category::where('parent_id', '0')->pluck('name');
if (is_dir($directory)) {
$files = scandir ($directory);
$img_file = $directory.'/'.$files[2];
$user['front_img'] = $img_file;
}
$profile = $user['front_img'];
//Product Image Mapping
$products->map(function ($product) {
$directory = 'uploads/products/images/'.$product->id;
$brand = Brand::select('name')->where('id', '=', $product->brand)->pluck('name');
$brand_name = $brand->first(function($value, $key) {
return $key == 'name';
});
if (is_dir($directory)) {
$files = scandir ($directory);
$img_file = $directory.'/'.$files[2];
$product['front_img'] = $img_file;
$product['brand'] = $brand_name;
return $product;
}
return $product;
});
return view('pages/product', compact('main_categories', 'profile', 'products', 'name', 'category_name'));
}
This is code in my product controller When i try to run this code it show the error here i used map() to map the products images and display it my view but why this error came ..Please any one help me..
That's because map() does not exist.
$products = Product::find($id);
is just short for
$products = Product::where('id', $id)->first();
So you're only getting one single Product model back. Then you're trying to call map() on it, which only exists on Collections that are returned if the results contains multiple datasets / Models. But you're explicitly asking for a single one. So you're getting Illuminate\Database\Eloquent\Model in response and then try to call map() on that. Since this method does not exist, Laravel tries to defer to Illuminate\Database\Query\Builder, and if does not find that method there, it bombs out with the above mentioned Exception.
So: There is only one $products, no need to map over it, just use it directly. Also, this would be even more clear if you called it $product instead of $products.

Related Links

AJAX POST Object return partial data
How to implement email piping in codeigniter?
Copy advanced custom field content from other page using if statement
Unknown column type “json” requested running Doctrine 2 migrations
PHP Combine Date and Time fields to form DateTime
Grouping Selected Rows From Table
Using Prestashop's API
Google App Engine with Let's Encrypt SSL in Php
fusioncharts stacked graph set SeriesName in array
Is casting to array in a foreach considered a bad practise?
How can I insert a specific value from one multidimensional array into another?
Replacement in the php array
Filter list that does not have an invalid email and opt-out email using SugarCRM 7 API
PHP File_Get_Contents()
my captcha images is in the form of textbox
Will foreach loop work better on my work?

Categories

HOME
mod-rewrite
mc
android-fragments
long-integer
redmine
stacktrace.js
ios-simulator
bing-search
google-play-services
nsis
elk-stack
fedora
angular-meteor
angular2-template
siesta
cqrs
dhcp
webstore
phonegap-build
coroutine
multiple-tables
clr
android-custom-view
spyder
connection-refused
multi-upload
mime-types
ini
sybase-ase
beego
jackrabbit-oak
iwebbrowser2
audit
source-insight
winsock
sca
amazon-kms
ftp-server
cloudera-quickstart-vm
rdp
utf
coverage.py
julius-speech
zend-form
gcal
common-table-expression
ssi
return-type
beeline
extjs4.1
spring.net
predicates
.when
dymola
nitrousio
oid
allegro
pjax
xcode6.4
persistent-object-store
openmrs
vim-plugin
servicestack-bsd
python-cffi
calcite
extend
android-handler
cache-manifest
flow-js
ipod
azure-xplat-cli
sablecc
findersync
directorysearcher
operands
password-recovery
cakephp-2.2
dc
nesper
aho-corasick
neo4jphp
oxygene
map
cloo
mp4parser
pacman
ckeditor.net
towers-of-hanoi
dynamic-c
motodev-studio
server-variables
nsrangeexception
nosetests
ognl
nsconnection
django-pagination
anonymous-types
ccl
resharper-5.1
source-code-protection
watchpoint

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile