Code in the view, this is being copied straight from the docs and is not working.
// app -> Cells -> DropdownCell.php
namespace AppCells;
use CodeIgniterViewCellsCell;
class DropdownCell extends Cell
{
protected $surveys;
public function courses($params): string
{
$this->surveys = model('SurveyModel');
$courses = $this->surveys->getUserCourses(auth()->user());
$data['items'] = [];
foreach ($courses as $c) {
$data['items'][] = ['id' => $c->id, 'title' => $c->title];
}
return view('components/dropdown',$data);
}
}
View:
<?= view_cell('DropdownCell::courses', ['type' => 'test']) ?>
I am getting an error:
Too few arguments to function AppCellsDropdown::courses(), 0 passed
I can’t see what is going wrong with this? The dropdown cell has been made with Spark as per the doc guidelines. This is being served by a controller extending BaseController.
I should add that the view cell works fine if I don’t use parameters, I would just like to use them…
2
Answers
I was trying to use the logic behind a 'simple cell' however the method I found that worked was following the instructions for 'computed cell'
Here’s how you should modify your
show
method in your view cell:By adding the
$params = []
in the method signature, you provide default values to the$params
array. This allows the method to be called without passing any parameters, and it will work with the default values you’ve set. However, you can still pass parameters to customize the view cell as needed, as you were trying to do in your view.Your view code should work as expected:
Make sure you have saved your view cell with the updated show method signature, and you should no longer encounter the "Too few arguments" error.
In your code, you’re using the namespace
AppCellsDropdown
, but your view cell is namedDropdownCell
. Ensure that the namespace and class name match