0

I am trying to pass data from my controller to my view, but am getting an undefined variable error. usersID is a column in my MySQL table.

Here is the code in my controller

$arrayWithCount = DB :: table("users_has_activities")
-> where("usersID", "=", 19)
-> pluck("usersID");

$countNumber = sizeof($arrayWithCount);

return view('pages.progress', ['countNumber' => $countNumber]);

I have also tried the following return statement without any success

return view::make('pages.progress') -> with('countNumber', $countNumber);

I have also tried reversing the puck and where clauses without any success, I didn't have high hopes that reversing them would fix the problem but thought I would try it any way. Below is the relevant code in the blade file.

 <?php echo $countNumber; ?> 

This is the error I am currently getting

Undefined variable: countNumber
5
  • 1
    Try to dd($countNumber); before return view clause. What does it show? Commented Mar 21, 2016 at 13:55
  • I added a dd($countNumber); statement in my controller before I return the view but I still get the same error. Is there a special place I need to go to where I can see the result of dd($countNumber); ? Commented Mar 21, 2016 at 14:00
  • 2
    Are you sure you're executing right method? Because dd() should display something and stop execution. Double check your routes and controllers. Commented Mar 21, 2016 at 14:04
  • You were right I had all the controller code in a method I wasn't calling, so the variable was never passed to the blade file. The method was set up to be called on a button press, after fixing that everything works. Thanks for your help Commented Mar 21, 2016 at 14:33
  • glad I was able to help. Please choose my answer as best one and upvote if you want to thank me for my time. ) Commented Mar 21, 2016 at 14:44

3 Answers 3

1

You code looks fine, if dd() doesn't stop execution of the controller, then another controller is executing. So double check your routes and controllers.

Sign up to request clarification or add additional context in comments.

Comments

0

First sizeof should be sizeOf, and is simply an alias for count(). Most people would prefer count over sizeOf as sizeOf (in many languages) would indicate something related to size on disk.

Anywho, being that pluck returns a collection, you have access to count() directly from the collection.

You can probably simply do something like:

$countNumber = $arrayWithCount->count();

Sidenote: Unless there is a particular reason why you are using <?php ?>, in blade, it would be preferred to use {{ and }}.

1 Comment

I tried using count() before but changed it to sizeOf() hoping it would fix the errors it did not. I corrected sizeOf() and also tried using $countNumber = $arrayWithCount->count(); but still get the same error.
0

I had all the controller code in a method I wasn't calling, so the variable was never passed to the blade file. The method was set up to be called on a button press, after fixing that everything works. Thanks Alexey for the help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.