Always getting error when cart is empty, Laravel 5 - php

Facing this error:
ErrorException in CartController.php line 35: Trying to get property of non-object
This is the code:
public function index()
{
$this->data['details'] = Cart::content();
$this->data['shipping'] = Shipping::where('region_category_id',session('location'))->where('type',session('type_komoditi'))->first();
$regType = session('regType');
$regId = session('id_wilayah');
$qReg = RegionCategory::find($regId);
if($regType == 'children') {
$this->data['minimalWeight'] = $qReg->minimal_weight;
//$this->data['minimalBuy'] = $qReg->min_buy;
}
$this->data['minimalBuy'] = $qReg->min_buy; //this is line 35
$this->data['regType'] = $regType;
\Session::put('price',Cart::total());
\Session::put('totalPrice',Cart::total());
\Session::put('paycode',0);
return view('client.carts.index',$this->data);
}
when I delete the line 35 the error is gone, but that code is important for using minimal buy filter.
How to resolve this error?

That's error cause $qReg->min_buy return null.
You can fix by return default value like this:
$this->data['minimalBuy'] = $qReg->min_buy ?? 0;
That's mean if is null return 0

It's saying that $qReg is not an object and you are trying to get the value from it using min_buy property.
Try printing the object $qReg using print_r($qReg); and exit the script after that.
print_r($qReg);
exit();
And check whether that object has the property min_buy or not.
Why is $this->data['minimalBuy'] = $qReg->min_buy; after if block ? It's being overwritten even if if($regType == 'children') is false.

Related

Codeigniter, Severity: error --> Exception: Too few arguments to function, admin dashboard shows HTTP error 500

I am faced with an error in log files
This
ERROR - 2022-05-13 02:47:21 --> Severity: error --> Exception: Too few arguments to function Transactions_model::get_pending_dash(), 0 passed in /Applications/MAMP/htdocs/application/controllers/admin/Dashboard.php on line 47 and exactly 1 expected /Applications/MAMP/htdocs/application/models/Transactions_model.php 2134
Here is exact code under controllers for dashboard.php on line 47:
$transactions = $this->transactions_model->get_pending_dash();
Here is exact code under transactions_model.php models :
// total transactions ////////////////////////////////////////////
function total_dash_transactions()
{
$s= $this->db->select("COUNT(*) as num")->get("transactions");
$r = $s->row();
if(isset($r->num)) return $r->num;
return 0;
return $result[0]->Transactions;
}
function get_pending_dash($user)
{
$where = "status = '1' AND type = '2'";
return $this->db->where($where)->order_by('id', 'DESC')->limit(20)->get("transactions");
}
Because in your function get_pending_dash requires $user as a parameter, but based on your code also you did not use it so if you did not use the parameter just delete the $user like the code below.
function get_pending_dash()
{
$where = "status = '1' AND type = '2'";
return $this->db->where($where)->order_by('id', 'DESC')->limit(20)->get("transactions");
}

PHP (Laravel) issue

I am receiving an error from running the below code?
(1/1) ErrorException
Creating default object from empty value
Code:
public function setServiceSetting(Request $request) {
if (!$request->has('setting_key') ||
!$request->has('setting_value')) {
return $this->getScriptingResponse();
}
$settingKey = $request->input('setting_key');
$settingValue = $request->input('setting_value');
$settings = WebsiteSettings::first();
if ($settings == null) {
return;
}
$setting->$settingKey = $settingValue;
$settings->save();
}
Error Line:
$setting->$settingKey = $settingValue;
I know the column exists, below anyone suggests that. I also know both parameters are non-null.
I think that this is incorrect
$settings->$settingKey = $settingValue;
try this:
$settings->settingKey = $settingValue;

Codeigniter 2: Why does $this->db->get('documents') return false?

I just started using CodeIgniter 2 and probably I have overseen something obvious but I can't figure it out.
My MySQL Database has a table named Documents with a column named online_since.
Function _getMaxOnlineSince() should get the maximum online_since value and return it as a string.
In application/models/browse_model.php:
private function _getMaxOnlineSince() {
$this->db->select_max('online_since');
$oQuery = $this->db->get('documents');
return $oQuery->num_rows() > 0 ? $oQuery->row()->online_since : false;
}
And I get an error message in the Browser:
Fatal error: Uncaught Error: Call to a member function num_rows() on
boolean
because of $oQuery is false: var_dump($oQuery) returns bool(false)
var_dump($this->db->select_max('online_since')) returns object(CI_DB_mysqli_driver)#14 (73) {...}
The Codeigniter 2 application worked on the old server with PHP5.3 but after the update to PHP 7 it crashes on this point.
Why does $this->db->get('documents') return false and how can I fix it?
I could manage to get this function work with sql queries:
private function _getMaxOnlineSince() {
$oQuery = "select max(online_since) from documents";
$sMaxOnlineSinceDate = $this->db->query($oQuery);
return $sMaxOnlineSinceDate->num_rows() > 0 ? $sMaxOnlineSinceDate->row()->online_since : false;
}
But there are many more places where I use
$this->db->get('documents')
I suppose in my case CodeIgniter has trouble to evaluate this statement for some reason. I'll debug further.
You can modify your function _getMaxOnlineSince() in below manner
private function _getMaxOnlineSince() {
$this->db->select_max('id','online_since');
$res1 = $this->db->get('documents');
if ($res1->num_rows() > 0) {
return $res1->row();
}
else {
return false
}
return NULL;
}
Also please check whether the table name documents is correct. Hope this will help you

Laravel 5.3 Check if result exist

I'm having trouble getting this thing to work. Basically this function is called once I click the delete button of an user config. Users can have multiple configs. What happens is that once I delete a config it looks for another one, if found it sets that as the current profile. If not it should set the config_done back to 0. the first bit works fine but if $firstProfile returns nothing, instead of entering the else it returns me an error..
code:
public function delete(UserConfig $config) {
$user = Auth::user();
$uid = $user->id;
if ($uid == $config->user_id) {
UserConfig::where('id', $config->id)->delete();
$firstProfile = UserConfig::where('user_id', $uid)->first()->id;
if(count($firstProfile)){
$user = User::find($uid);
$user->current_profile = $firstProfile;
$user->save();
return view('/settings');
} else {
$user = User::find($uid);
$user->config_done = 0;
$user->save();
return view('/home');
}
}
}
error:
ErrorException in UserConfigController.php line 100:
Trying to get property of non-object
in UserConfigController.php line 100
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/Users/jordykoppen/git/beef-monitor/app/Http/Controllers/UserConfigController.php', '100', array('config' => object(UserConfig), 'user' => object(User), 'uid' => '1')) in UserConfigController.php line 100
Keep in mind that I'm very new to Laravel and the whole MVC environment.
Eloquent will return null if no result is found. You're dealing with objects here, so rather than checking the count, you can just do this.
$firstProfile = UserConfig::where('user_id', $uid)->first();
if($firstProfile){
// success
} else {
// not result
}
The $firstProfile variable will either be an instance of UserConfig or it will be null, no need to check the count.

Trying to get property of non-object with PHP

I have a strange issue; my code is working on my localhost, but when I try to use it online. It's showing an error in the return line:
Trying to get property of non-object
Here is the code:
public static function displayContenuAsString ($id,$class="traduction survol_video",$div="div") {
return "<$div id=\"contenu_$id\" class=\"$class\"></$div>".Contenu::model()->findByPk($id)->valeur;
}
No issues in the code Contenu::model()->findByPk($id)->valeur; Just check for the row in database with primary key.
According to documentation findByPk return the record found or Null if none is found. So you need to add check before using model values in this way:
public static function displayContenuAsString ($id,$class="traduction survol_video",$div="div") {
$contenu = Contenu::model()->findByPk($id);
$valeur = $contenu !== null ? $contenu->valeur : 'Empty';
return "<$div id=\"contenu_$id\" class=\"$class\"></$div>".$valeur;
}

Categories