PHP (Laravel) issue - php

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;

Related

Trying to get property 'staff 'of non-object when test REST API from Postman

I'm trying to understand the reason of this error message from postman when test API.
When I am testing my REST API from postman, it gives me error
ErrorException (E_NOTICE)
Trying to get property 'staff' of non-object
I try to find the problem but i can't find it. I kept searching for this but couldn't find an answer that will make this clear.
Anyone can help me on this?
Thanks!
This my code snippet
public function updatestatus($request, $leave, $is_api=0)
{
$status = $request->get('status');
$user = $is_api? JWTAuth::parseToken()->authenticate():Auth::user();
// $user = Auth::user();
$staff= $user->ref_user;
$applying_staff = $leave->staff;
$applying_user = $applying_staff->main_user;
//Approved
if($status==2 && $leave->status==1)
{
$leave->status =2;
$leave->approved_by_staff_id = $staff->staff_id;
$leave->approved_date = new Carbon('today');
$leave->save();
if($user->centre_id)
Helper::ClearObjective(10,$user->centre_id);
dispatch(new EmailJob($applying_user->email,new LeaveNotification(route('staff.leave.show',$leave->id), $leave->statusStr)))
->onConnection('database')
->onQueue('emails');
return response()->json(['Success'=>'Success']);
//send email
}
// Rejected
else if($status==3 && $leave->status==1)
{
$leave->status =3;
$leave->status_rejected_reason = $request->get('reason',null);
$leave->save();
if($user->centre_id)
Helper::ClearObjective(10,$user->centre_id);
$leave_cat = LeaveCategory::find($leave->leave_type);
if($leave_cat->leave_status!=0)
{
$leave_ent = $applying_staff->leaves()->where('leave_type',$leave->leave_type)->first();
if($leave_ent)
{
$leave_ent->leave_remaining += $leave->leave_days;
$leave_ent->save();
}
}
dispatch(new EmailJob($applying_user->email,new LeaveNotification(route('staff.leave.show',$leave->id), $leave->statusStr, $leave->status_rejected_reason)))
->onConnection('database')
->onQueue('emails');
//send email
}
//Cancelled
else if(($status==4 && $leave->status==2) || ($status==4 && $leave->staff_id == $staff->staff_id))
{
$leave->status =4;
$leave->status_rejected_reason = $request->get('reason',null);
$leave->save();
$leave_cat = LeaveCategory::find($leave->leave_type);
if($leave_cat->leave_status!=0)
{
$leave_ent = $applying_staff->leaves()->where('leave_type',$leave->leave_type)->first();
if($leave_ent)
{
$leave_ent->leave_remaining += $leave->leave_days;
$leave_ent->save();
}
}
dispatch(new EmailJob($applying_user->email,new LeaveNotification(route('staff.leave.show',$leave->id), $leave->statusStr,$leave->status_rejected_reason)))
->onConnection('database')
->onQueue('emails');
//send email
}
// return reponse()->toJson(compact('leave'));
return $leave;
}
Calling API
public function update(Request $request)
{
return $this->leaveApplicationRepository->updatestatus($request,1);
}
your updatestatus() need 3 parameter and your update() function pass only 2 paramenter;
public function update(Request $request)
{
// please provide you leave data
$leave = "your data is here";
return $this->leaveApplicationRepository->updatestatus($request,$leave, 1);
}
From the context that I can see, it is trying to reference the staff variable in:
$applying_staff = $leave->staff;
The non-object message will mean that $leave itself is the problem. Since you are passing in 1 to the call:
return $this->leaveApplicationRepository->updatestatus($request,1);
the 1 becomes the $leave parameter, and that is not an object, hence the error.
Maybe you think that by passing in 1, it will find the correct model automatically? This is not the case. You need to load that model explicitly.

SYMFONY4 : "Notice: Undefined variable: parentClass"

I've got this error message when i try to go to a page which calls an entity. (For example the contact page, or articles page)
"Notice: Undefined variable: parentClass" which refer me to this file
vendor\doctrine\persistence\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php line 176
public function getManagerForClass($class)
{
// Check for namespace alias
if (strpos($class, ':') !== false) {
[$namespaceAlias, $simpleClassName] = explode(':', $class, 2);
$class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
}
$proxyClass = new ReflectionClass($class);
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
$parentClass = $proxyClass->getParentClass();
if (! $parentClass) {
return null;
}
}
$class = $parentClass->getName();
foreach ($this->managers as $id) {
$manager = $this->getService($id);
if (! $manager->getMetadataFactory()->isTransient($class)) {
return $manager;
}
}
}
I really dont know what to do.
If someone got an idea or already had this error :)
Thank you !
your correctly indented code looks like this:
$proxyClass = new ReflectionClass($class);
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
$parentClass = $proxyClass->getParentClass();
if (! $parentClass) {
return null;
}
}
$class = $parentClass->getName();
now, in the case that $proxyClass->implementsInterface(...) is FALSE there is no $parentClass defined, hence the error message.
since the code appears to be in doctrine, maybe it's a bug. Maybe it was fixed and you use an old version for some reason. maybe update?
in the current version the line is inside the if-block
you're absolutely right, i don't know why $class = $parentClass->getName(); was out the 'if', the problem was effectively this ! Thank you !

Always getting error when cart is empty, Laravel 5

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.

Laravel fill returns null

I want to update a product in laravel, but it does not work properly,
my Controller's update method looks like this:
public function updateProduct(Request $request)
{
# Get input values
$data = $request->all();
$productID = $data['id'];
$product = Product::find($productID);
$product->fill($data);
# Validate input
$validator = Validator::make($request->all(), Product::$rules);
if ($product->save())
{
# save language selection
$lsCounter = 0;
$langSelecName = $request->input('language');
$langSelecFile = $request->file('language');
if ($langSelecName)
{
$projectPath = $dProjectPath . "languages";
foreach ($langSelecName as $langSelecNameKey => $langSelecNameValue)
{
if ($langSelecFile[$lsCounter]['input_vid_lang'] != null)
{
$langVidFileName = $langSelecFile[$lsCounter]['input_vid_lang']->getClientOriginalName();
$languages = new Language();
$languages['short_name'] = $langSelecNameValue;
$languages['input_video'] = $projectLangPath . '\\' . $langVidFileName;
$languages->product()->associate($product);
$langData = [
'languagesShortName' => $languages['short_name'],
'languagesInputVideo' => $languages['input_video']
];
$intProductID = intval($productID);
$findLangId = $languages->find($intProductID);
$findLangId->fill($langData);
if ($languages->save())
{
$langSelecFile[$lsCounter]['input_vid_lang']->move($projectLangPath, $langVidFileName);
}
}
$lsCounter++;
}
}
} else {
return redirect()->route('editProduct', $productID)->with('message', 'Error')->withInput();
}
I get the following error after I try to update it, the error looks like this:
Call to a member function fill() on null
And it points to this line:
$findLangId->fill($langData);
I appreciate some help, thank you.
Edit
Ok people said that $intProductRomID is null, but I get the correct product id if I dd($intProductRomID).
As per the comments, the following line is return null:
$findLangId = $languages->find($intProductRomID);
Meaning this won't be valid:
$findLangId->fill($langData);
In other words, if you were to var_dump out $languages->all(), you will not find $intProductRomID in there. If you are unsure, swap out ->find with ->findOrFail() (which, considering you aren't doing any error catching or checking, you probably should be using it instead).
Edit
After some conversation in the comments, it has been established that the wrong field was being used for reference. Use a where instead:
$languages->where('product_rom_id', $intProductRomID);

PHP: fatal error: call to a member function get() on non object

I got an error while running my code, it says call to a member function getBallparkDetailsStartDate() on a non-object.
if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
$projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
$projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
$projectDetails["projectid"] = $projectId;
$projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
$projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE;
}
I got the error in this line: $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
Here is my other code:
public function __construct($ballparkDetailsId, $project,
$ballparkDetailsBookingRef,
$ballparkDetailsStartDate, $ballparkDetailsEndDate,
$ballparkDetailsExpiryDate, $ballparkDetailsDescription,
$ballparkDetailsNotes) {
$this->ballparkDetailsId = $ballparkDetailsId;
$this->project = $project;
$this->ballparkDetailsBookingRef = $ballparkDetailsBookingRef;
$this->ballparkDetailsStartDate = $ballparkDetailsStartDate;
$this->ballparkDetailsEndDate = $ballparkDetailsEndDate;
$this->ballparkDetailsExpiryDate = $ballparkDetailsExpiryDate;
$this->ballparkDetailsDescription = $ballparkDetailsDescription;
$this->ballparkDetailsNotes = $ballparkDetailsNotes;
}
public function getBallparkDetailsId() {
return $this->ballparkDetailsId;
}
public function getProject() {
return $this->project;
}
public function getBankName() {
return $this->getProject()->getBankName();
}
public function getBankRef() {
return $this->getProject()->getBankRef();
}
public function getRegionName() {
return $this->getProject()->getRegionName();
}
public function getProjectStatusName() {
return $this->getProject()->getProjectStatusName();
}
public function getBallparkDetailsBookingRef() {
return $this->ballparkDetailsBookingRef;
}
public function getBallparkDetailsStartDate() {
return $this->ballparkDetailsStartDate;
}
public function getBallparkDetailsEndDate() {
return $this->ballparkDetailsEndDate;
}
public function getBallparkDetailsExpiryDate() {
return $this->ballparkDetailsExpiryDate;
}
public function getBallparkDetailsDescription() {
return $this->ballparkDetailsDescription;
}
public function getBallparkDetailsNotes() {
return $this->ballparkDetailsNotes;
}
public function getProjectId() {
return $this->getProject()->getProjectId();
}
public function getProjectStatusId() {
return $this->getProject()->getProjectStatusId();
}
}
?>
The last time I check this it ran well. But now I don't know what's wrong with this? Please help me find the error. Thanks.
Apparently
$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
is not returning a "ballpark" at all. Probably it is returning an error, or something like an empty array.
Try var_dump()'ing $ballpark immediately before the line that raises the error, and see what it contains (probably False, NULL, array() or something equally un-ballparky.
Then, inspect the ballparkDetailsByProjectId() function in the BallparkDetailsHandler.php file. At a guess, you might be passing an invalid (i.e. nonexistent, removed, etc.) $projectId.
Then you might rewrite the code with error checking:
if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
if (!is_object($ballpark))
trigger_error("Error: bad project ID: '$projectId': $ballpark",
E_USER_ERROR);
$projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
$projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
$projectDetails["projectid"] = $projectId;
$projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
$projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE;
}
Then in the BallparkDetailsHandler.php file you could modify this code:
// Prepare query or die
if (!($stmt = $this->mysqli->prepare($query))
return "Error in PREPARE: $query";
$stmt->bind_param("i", $projectId);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ballparkDetailsBookingRef, $bankRef, $regionName,
$projectStatusId, $projectStatusName, $ballparkDetailsDescription,
$ballparkDetailsNotes, $ballparkDetailsStartDate, $ballparkDetailsEndDate,
$ballparkDetailsExpiryDate);
$stmt->fetch();
// If no data, then die
if(!$stmt->num_rows)
return "No data in DB for projectID '$projectId': $query";
// Should be clear sailing from here on. Actually I ought to check
// whether all these new() here do return anything sensible, or not
$bank = new Bank("", "", $bankRef, "");
$region = new Region("", $regionName, "");
$projectStatus = new ProjectStatus($projectStatusId, $projectStatusName);
$project = new Project($projectId, $bank, $region, $projectStatus);
return new BallparkDetails("", $project,
$ballparkDetailsBookingRef, $ballparkDetailsStartDate,
$ballparkDetailsEndDate, $ballparkDetailsExpiryDate,
$ballparkDetailsDescription, $ballparkDetailsNotes);
$ballpark clearly doesn't contain the object you think it does on the line with the error. In fact, it obviously doesn't contain an object at all.
This implies that the preceding line (which sets $ballpark) isn't working properly. It would appear that it's returning a value that is is not an object.
I can't tell what that value is -- it could be null, or it could be an integer, string, array, etc. But whatever it is, it isn't a ballpark object.
I suggest you look at your getBallparkDetailsByProjectId() method to find the source of this problem.

Categories