Reputation: 933
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
Upvotes: 0
Views: 789
Reputation: 163748
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.
Upvotes: 1
Reputation: 933
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.
Upvotes: 0
Reputation: 58142
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 }}
.
Upvotes: 0