1

im new to laravel and im getting an error Undefined variable $columnCounts passed to view.

i have a index() method that call 2 other method of the same NumberController, columnCount() and rowCount() for do a little number count.

this is the index() method:

    public function index()
{
    //

    $column1count = $this->countColumn(1);
    $column2count = $this->countColumn(2);
    $column3count = $this->countColumn(3);

    $row1count = $this->countRow(1);
    $row2count = $this->countRow(2);
    $row3count = $this->countRow(3);

    $columnCounts = [['column' => 1, 'count' => $column1count], ['column' => 2, 'count' => $column2count], ['column' => 3, 'count' => $column3count]];
    $rowCounts = [['row' => 1, 'count' => $row1count], ['row' => 2, 'count' => $row2count], ['row' => 3, 'count' => $row3count]];
    $numbers =  Number::all();
    return view('index', ['numbers' => $numbers, 'columnCounts' => $columnCounts, 'rowCounts' => $rowCounts]);
}

this is the columnCount() method:

    public function countColumn($column)
{
    $lastNumber = Number::all()->sortByDesc('created_at')->first();
    $lastColumn = Number::all()->where('colonna', '=', $column)->sortByDesc('created_at')->first();
    $count = $lastNumber->id - $lastColumn->id;
    return $count;
}

this is the rowCount() method:

    public function countRow($row)
{
    $lastNumber = Number::all()->sortByDesc('created_at')->first();
    $lastRow = Number::all()->where('riga', '=', $row)->sortByDesc('created_at')->first();
    $count = $lastNumber->id - $lastRow->id;
    return $count;
}

if i go to index route all work good, it show the coulmn e and row count and all the number in the sequence.

i have a form in the index view for store a new number in the db. that form have and action to index and after store the number to db, i do :

return $this->index();

here is my store() method:

    public function store(StoreNumberRequest $request)
{
    $validated = $request->validated();


    if ($validated) {

        $lastNumber = $request->input('lastNumber');
        $column1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
        $column2 = [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
        $column3 = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36];
        $row1 = [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34];
        $row2 = [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35];
        $row3 = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36];


        if (in_array($lastNumber, $column1) && in_array($lastNumber, $row1)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 1,
                'riga' => 1,
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column1) && in_array($lastNumber, $row2)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 1,
                'riga' => 2,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column1) && in_array($lastNumber, $row3)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 1,
                'riga' => 3,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column2) && in_array($lastNumber, $row1)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 2,
                'riga' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column2) && in_array($lastNumber, $row2)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 2,
                'riga' => 2,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column2) && in_array($lastNumber, $row3)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 2,
                'riga' => 3,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column3) && in_array($lastNumber, $row1)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 3,
                'riga' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column3) && in_array($lastNumber, $row2)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 3,
                'riga' => 2,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif (in_array($lastNumber, $column3) && in_array($lastNumber, $row3)) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 3,
                'riga' => 3,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);
            $numbers =  Number::all();
            return view('index', ['numbers' => $numbers]);
        } elseif ($lastNumber === 0) {
            $number = [
                'number' => $request->input('lastNumber'),
                'colonna' => 0,
                'riga' => 0,
                'created_at' => now(),
                'updated_at' => now(),
            ];
            Number::create($number);

            return $this->index();
        }
    }
}

and here is the index.blade.view :

        <div class="row text-center">
        @foreach ($columnCounts as $column)
            <div class="col border border-2 border-dark p-5 mb-5">

                <p>Column {{ $column['column'] }} Count</p>
                <p>{{ $column['count'] }}</p>
            </div>
        @endforeach
        @foreach ($rowCounts as $row)
            <div class="col border border-2 border-dark p-5 mb-5">
                <p>Row {{ $row['row'] }} Count</p>
                <p>{{ $row['count'] }}</p>
            </div>
        @endforeach

    </div>

but after submit the form when i return $this->index(); i get the "Undefined variable $columnCounts".

i have a delete button too in the view. that button call the destroy() method for delete the last number insert in the sequence.

this is the destroy() method:

    public function destroy()
{

    DB::table("numbers")->orderBy("created_at", "DESC")->take(1)->delete();

    return $this->index();
}

in the destroy() method i return $this->index(); like in the store() method.

when i delete a number whit destroy() the app go back to index and work good, but whit store() i get that error.

someone can explain me what im doing wrong and if there is a better way to send $variables to the view whitout getting this issues?

2 Answers 2

2

The issue happens because in your store() method, you are returning the view manually with return view('index', ['numbers' => $numbers]);. This doesn't include $columnCounts and $rowCounts, which are required by your Blade file.

Fix:

Instead of returning view('index', ['numbers' => $numbers]);, call $this->index() in all cases, like you already do in destroy():

Replace this:

return view('index', ['numbers' => $numbers]);

With this:

return $this->index();

This way, index() will be called properly and pass all necessary data to the view.

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

Comments

0

The answer Frustie Frustie gave you is correct, you are not passing data to view.

PS: sorry, my eyes were bleeding, so I rewrote the code for you :)

public function store(StoreNumberRequest $request)
{
    $validated = $request->validated();

    if (!$validated) {
        return redirect()->back()->withErrors('Invalid input.');
    }

    $lastNumber = (int) $request->input('lastNumber');

    // Special case for 0
    if ($lastNumber === 0) {
        Number::create([
            'number' => 0,
            'colonna' => 0,
            'riga' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $numbers = Number::all();
        return view('index', ['numbers' => $numbers]);
    }

    if ($lastNumber < 1 || $lastNumber > 36) {
        return redirect()->back()->withErrors('Number out of bounds.');
    }

    // Define rows
    $rows = [
        1 => [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34],
        2 => [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35],
        3 => [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
    ];

    // Determine column and row
    $col = (int) ceil($lastNumber / 12);
    $row = collect($rows)
        ->filter(fn($numbers) => in_array($lastNumber, $numbers))
        ->keys()
        ->first();

    // Save to DB
    Number::create([
        'number' => $lastNumber,
        'colonna' => $col,
        'riga' => $row,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $numbers = Number::all();
    return view('index', ['numbers' => $numbers]);
}

Additionally you can move $lastNumber boundaries check into StoreNumberRequest:

public function rules()
{
    return [
         ...
         'lastNumber' => ['required', 'numeric', 'min:0', 'max:36'],
         ...
    ];
}

1 Comment

wow thank you so much, but i didn't undestand so much that collect function. i have to learn about it ^^

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.