What line is giving the error (according to the stack trace)?
Call to a member function where() on null error in laravel
i have a system whereby on a user selects the company from which they prefer to buy from.on selecting the company,a page should show all the products in which the company sells and not all the products from the products table.i have been able to achieve that but on selecting the company the page shows all the products from the products table rather than only the products the selected company .i have used a pivot table to link the company with the product it sells.how can I show only the products from a specific company only?here is my function which shows the products.upon adding "find($company_id)" instead of "->get();" i get an error "Call to a member function where() on null error in laravel"
public function showDocumentSupplierView($company_id)
{
$merchant_id = $company_id;
Log::debug('**** showDocumentSupplierView() ****');
Log::debug('company_id='.$company_id);
$user_data = new UserData();
Log::debug('company_id='.$company_id);
$productids= DB::table('merchantproduct')->
where('merchant_id',$company_id)->
pluck('product_id');
$ids = merchantproduct::where('merchant_id',
$user_data->company_id())->pluck('product_id');
$collection = collect($productids);
$merged = $collection->merge($ids);
$ids=$merged->all();
$inventory_data = product::whereIn('ptype',
// added hydrogen here on this list
['inventory', 'rawmaterial', 'warranty', 'oilgas','hydrogen'])->
whereNotNull('name')->whereIn('id', $ids)->find($company_id);
$company_detail = \App\Models\Company::where('id',
$merchant_id)->first();
$my_company_detail = \App\Models\Company::find($user_data->company_id());
$issuer_merchant_id = $my_company_detail->id;
$currency = \App\Models\Currency::where('id',
$my_company_detail->currency_id)->first();
if (empty($currency)) {
$currency = \App\Models\Currency::where('code', "MYR")->first();
}
$merchantlinkrelation = DB::table('merchantlinkrelation')->
join('merchantlink', 'merchantlink.id', '=', 'merchantlinkrelation.merchantlink_id')->
where([
'merchantlink.initiator_user_id' => $my_company_detail->owner_user_id,
'merchantlink.responder_user_id' => $company_detail->owner_user_id,
])->
orWhere([
'merchantlink.responder_user_id' => $my_company_detail->owner_user_id,
'merchantlink.initiator_user_id' => $company_detail->owner_user_id,
])->
where([
'merchantlinkrelation.ptype' => 'dealer',
'merchantlinkrelation.company_id' => $my_company_detail->id
])->
orderBy('merchantlinkrelation.created_at', 'desc')->
first();
$warranty_prd_ids = product::whereIn('ptype',
['warranty'])->
whereNotNull('name')->
whereIn('id', $ids)->pluck('id');
$pro_prd_ids = product::whereIn('ptype',
['customization'])->
whereNotNull('name')->
whereNotNull('prdcategory_id')->
whereIn('id', $ids)->pluck('id');
$pro_inventory_ids = product::whereIn('ptype',
['inventory'])->
whereNotNull('name')->
whereNotNull('photo_1')->
whereNotNull('prdcategory_id')->
whereNotNull('prdsubcategory_id')->
whereNotNull('prdprdcategory_id')->
whereIn('id', $ids)->pluck('id');
// followed prices from wholesales
$inventory_idx = product::whereIn('ptype',
['inventory'])->
whereNotNull('name')->
whereIn('id', $ids)->pluck('id');
$warranty_price = warranty::
whereNull('deleted_at')->
whereIn('product_id', $warranty_prd_ids)->pluck('price', 'product_id');
// followed prices from pro service
$proservice_price = prd_proservices::
whereNull('deleted_at')->
whereIn('product_id', $pro_prd_ids)->pluck('price', 'product_id');
// followed prices from inventory
$inventory_price = prd_inventory::
whereNull('deleted_at')->
whereIn('product_id', $pro_inventory_ids)->pluck('price', 'product_id');
foreach ($warranty_price as $key => $value) {
$in_float = $value / 100;
// $warranty_price[$key] = is_int($in_float) ? $in_float.'.00' : $in_float;
$warranty_price[$key] = $in_float;
}
foreach ($proservice_price as $key => $value) {
$in_float = $value / 100;
// $warranty_price[$key] = is_int($in_float) ? $in_float.'.00' : $in_float;
$proservice_price[$key] = number_format($in_float, 2);
}
/*
foreach ($inventory_data as $key => $value) {
# code...
if($value->ptype =='inventory'){
if( empty($inventory_price[$value->id])){
unset($inventory_data[$key]);
}
}
else if($value->ptype =='warranty'){
if( empty($warranty_price[$value->id])){
unset($inventory_data[$key]);
}
}
else if($value->ptype =='customization'){
if( empty($proservice_price[$value->id])){
unset($inventory_data[$key]);
}
}
}
*/
$wholesale_prices = new Wholesale();
$product_whole_sale_price_and_range = [];
foreach ($inventory_idx as $key => $value) {
$wholesale = $wholesale_prices->where('product_id', $value)->get()->toArray();
if (count($wholesale) !== 0) {
$price_in_float = $wholesale[0]['price'] / 100;
$per_item_whole_sale_price = $price_in_float; /// $wholesale[0]['unit']
$wholesale[0]['per_item_whole_sale_price'] = number_format($per_item_whole_sale_price, 2);
} else {
$wholesale[0]['per_item_whole_sale_price'] = "0.00";
$wholesale[0]['price'] = "0.00";
}
$product_whole_sale_price_and_range[$value] = $wholesale;
}
$fuel_ids = $inventory_data->where('ptype', 'oilgas')->pluck('id')->toArray();
$product_oil_gas_price = [];
foreach ($fuel_ids as $f) {
$ogFuelPrice = DB::table('prd_ogfuel')->where('product_id', $f)->first()->wholesale_price ?? 0;
// dd($ogFuelPrice);
/*
DB::table('og_fuelprice')->join('prd_ogfuel','prd_ogfuel.id',
'=','og_fuelprice.ogfuel_id')->
whereDate('og_fuelprice.start' , '<=',\Carbon\Carbon::today())->
orderBy('og_fuelprice.start', 'desc')->
where('prd_ogfuel.product_id', $f)->
select("og_fuelprice.*")->
first()->price ?? 0;*/
$product_oil_gas_price[$f] = number_format($ogFuelPrice / 100, 2);
}
$ids = merchantlocation::where('merchant_id', $issuer_merchant_id)->pluck('location_id');
$location = location::where([['branch', '!=', 'null']])->whereIn('id', $ids)->latest()->get();
return view('data.documentsupplier', compact(
'inventory_data', 'merchant_id', 'company_detail',
'my_company_detail', 'product_whole_sale_price_and_range', 'wholesale_prices',
'currency', 'issuer_merchant_id',
'warranty_price',
// 'product_oil_gas_price',
'proservice_price',
'location', 'merchantlinkrelation'
));
}
@stephen waweru suppose you have a conventional merchants relationship defined on the product Model, and products relation on the merchant Model:
// Product
public function merchants()
{
return $this->belongsToMany(merchant::class, 'merchantproduct');
}
// Merchant
public function products()
{
return $this->belongsToMany(product::class, 'merchantproduct');
}
Now there are two ways you could get the products:
$inventory_data = product::whereIn('ptype', ['inventory', 'rawmaterial', 'warranty', 'oilgas','hydrogen'])
->whereNotNull('name')
->whereIn('id', $ids)
->whereHas('merchants', fn ($builder) => $builder->where('id', $company_id))
->get();
Or, if you have a merchant instance:
$merchant = merchant::find($company_id);
$inventory_data = $merchant->products()
->whereIn('ptype', ['inventory', 'rawmaterial', 'warranty', 'oilgas','hydrogen'])
->whereNotNull('name')
->whereIn('id', $ids)
->get();
Choose one or the other.
Please or to participate in this conversation.