I am creating some code using laravel in controller and i want send array from controller to blade file i face problem 1/1) ErrorException
Undefined variable: addition It runs but now face this error
$get_details=DB::select('SELECT deliver_date,GROUP_CONCAT(orders_qty) as orders_qty FROM `orders` WHERE order_status=? and deliver_date between ? AND ? GROUP BY deliver_date',[6,'2019-02-01','2020-02-05']);
foreach($get_details as $date_wise_details)
{
$array=explode(',',$date_wise_details->orders_qty);
$addition[]=array_sum($array);
}
return view('dashboard',['get_data'=>$get_details])->with('addition',$addition);
You need to define addition array to resolve this issue
$addition = []; // define your array here
foreach($get_details as $date_wise_details)
{
$array=explode(',',$date_wise_details->orders_qty);
$addition[]=array_sum($array);
}
Related
I'm trying to create waitlist to buy product, but change the status of the transaction from 'Confirmed' to 'Waitlist'.
For that I have added a variable 'status' and a function in Transaction Model as follows:
const CONFIRMED_TRANSACTION = 'confirmed';
const WAITLIST_TRANSACTION = 'waitlist'; //waitlist product
public function isConfirmed() {
return $this->status == Transaction::CONFIRMED_TRANSACTION;
}
Default value of Transaction->status is 'CONFIRMED_TRANSACTION'.
Now every time the Product->status changes to 'unavailable' i would like to change the value of the Transaction->status to 'WAITLIST_TRANSACTION' when the transaction is created.
I am trying to achieve it using Event Listeners as:
Transaction::created(function($transaction) {
if(!$product->isAvailable()) {
$transaction->status = Transaction::WAITLIST_TRANSACTION;
$transaction->save();
}
});
But this gives me error :
**ErrorException: Undefined variable: product in file /home/vagrant/restfulapi7/app/Providers/AppServiceProvider.php on line 29**
How can I achieve the same in a better way?
You're using what's called a lambda or anonymous function, which has a different scope to the rest of your code.
function($transaction) does not have access to a variable named $product. So you're trying to call ->isAvailable() on a variable which doesn't exist.
Transaction::created(function($transaction) {
if(!$product->isAvailable()) {
$transaction->status = Transaction::WAITLIST_TRANSACTION;
$transaction->save();
}
});
Assuming it exists in the scope in which you call Transaction::created(), you can pass the $product variable into your lambda function like this:
function($transaction) use ($product) { ... }
For more information on lambda functions, read the php docs here
This solves the issue:
if(!$transaction->product->isAvailable())
I'm currently working on a project. It was all working just fine until i tried to migrate some tables that I edited. I got this error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Function name must be a string
Since I doesn't directly show me where the error is, I couldn't find it. Last things I changed before I tried to migrate tables:
Migrations
Laravel colletive/html forms
Store method in my controller
As I know, migrations and forms shouldn't be a problem with this error, so here's my controller code:
public function store(Request $request)
{
$user = Auth::user();
$input = $request->all();
if ($file = $request->file('photo_id')){
$name = time().$file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$user->posts()->create($input);
return redirect('/userPanel');
}
If the error isn't even in a controller code, where could it be. Any help appreciated.
I have found out it was because of a very silly mistake, i was calling a variable as a function...
$ownedMacs = intval($data()['owned_mac']);
Changed to
$ownedMacs = intval($data['owned_mac']);
This error message usually appears when we are doing something real stupid! :)
Same issue in Laravel can solve
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Function name must be a string
When you checked on storage/logs/laravel.log
local.ERROR: Function name must be a string {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Function name must be a string at E:\\Project\\workspace\\turttyKidsProject\\app\\Http\\Controllers\\SiteContactController.php:52)
[stacktrace]
Solution
Data field requesting another format but you are trying to save an object.
$objectSave= new ObjectClass;
$objectSave->feild_db = $request->post('request_name');
Check whether you access request with your form submit method. it may be POST or GET
SOLVED: Answer below.
I upgraded my Laravel project from 5.3 to 5.4 and then 5.5.
Only thing that is broken at the moment is when I go to a product edit page I get error:
Property [specifications] does not exist on this collection instance.
Exception:
public function __get($key)
{
if (! in_array($key, static::$proxies)) {
throw new Exception("Property [{$key}] does not exist on this collection instance.");
}
return new HigherOrderCollectionProxy($this, $key);
}
Which is caused by this line in the blade template:
#if($categories->specifications->first())
$categories variable is passed to view from ProductController like this:
$categories = Category::with('specifications.attributes')->find($product->getCategoryId());
What has changed in 5.4/5.5 that could have broken this line of code?
For some reason I had to add ->first() to $categories before accessing specifications.
$categories->first()->specifications->first()
On the same view this $product->categories->first() works fine and is returned the same way as categories in controller, but doesn't require another ->first(). No idea why.
Why would $text be providing an Undefined Property error?
If I run dd($mentions) prior to the function it definitely exists and contains the text property.
If I run dd($mention->text) before return and in the function I also get what is expected.
However, the function will not return a value for $text and instead errors out.
$text = $mentions->map(function ($mention) {
return $mention->text;
});
I'd bet that you have 1 object in the $mentions collection that doesn't have the ->text property ?
Try:
if(!isset($mention->text)){
dd($mention))
}
to find out which one.
How to set and retrieve error inside component in model I have:
if (empty($coupon->coupon_id))
{
$this->setError( JText::_( "Invalid Coupon" ) );
return false;
}
How to retrieve this error inside controller? this->getError gives nothing :(
Thanks
Do you want to return the error to the screen?
If so, http://docs.joomla.org/Display_error_messages_and_notices
In your controller you need to invoke your model and use the $model->getError(); function instead of $this->getError();