I want to resolve Undefined variable $snows

Asked 2 years ago, Updated 2 years ago, 45 views

My version laravel8
problem
Undefined variable $snows

Goals
I want to define the $snows function

Problems
I pasted my own balade on the dashboard.blade.php on the initial login screen of laravel.
It is clear that $snows is not routed to dashboard.blade.php.
Therefore, navigation.blade.php gets an error when changing the snows line to '/dashboard'.
$dashboard and $snows must be established in dashboard.php.
How should I write it on the web.php?

web.php

Route::get('/dashboard', function(){
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');

require__DIR__.'/auth.php';

// View Post List
// Route::get('/', 'SnowController@showList') - > name('snows');
// Route::get('/list', 'SnowController@showList');
Route::get('/', 'SnowController@showDash') - > name('snows');
// Route::get('/dashboard', function() {return view('dashboard');})->middleware(['auth'])->name('snows');

// Post Screen Display
Route::get('/snow/create', 'SnowController@showCreate') ->name('create');
// Post Registration
Route::post('/snow/store', 'SnowController@store') ->name('store');

// View Post Details
Route::get('/snow/{id}', 'SnowController@showDetail') ->name('show');

// View Post Edit
Route::get('/snow/edit/{id}', 'SnowController@showEdit') ->name('edit');
Route::post('/snow/update', 'SnowController@UpdateSnow') ->name('update');

// Delete Posts
Route::post('/snow/delete/{id}', 'SnowController@exeDelete') ->name('delete');

dashboard.blade.php (undefined)

@foreach($snows as$snow)
        <tr>
          <td>{{$snow->id}}</td>
          <td><a href="/snow/{{$snow->id}}">{{$snow->title}}</a></td>;;
          <td>{{$snow->created_at}}</td>
          <td><button type="button" class="btnbtn-primary" onclick="location.href='/snow/edit/{{$snow->id}}'>Edit</button></td>
          <form method="POST" action="{{route('delete', $snow->id)}}"onSubmit="return checkDelete()">
            @csrf
            <td><Button type="submit" class="btnbtn-primary" onclick=>Delete</button></td>;
        </tr>
 @endforeach

Controller

// Post List
    public function showDash(){
        $snows=$this->snow->findAllSnows();
        
        return view('dashboard', ['snows'=>$snows]);
    }

php laravel

2022-09-30 19:17

1 Answers

view('dashboard', ['snows'=>$snows'); indicates that $shows exists, so there is no problem, but web.php's view('dashboard'); does not exist, so you want to resolve the error.

I don't know what I want to do in the end, but if it's this pattern of error,

PHP:New - Manual/Null Combined Operator
https://www.php.net/manual/ja/migration70.new-features.php#migration70.new-features.null-coalesce-op
This replaces the undefined value with another value, so

Before Modification

@foreach($snows as$snow)

Corrected

@foreach($snows??[]as$snow)

For example, I think you can solve the error in this way.


2022-09-30 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.