is it possible to pass variables like this

Published 1 week ago by deladels

    Route::get('schedules', function(App\Schedule $schedules) {
        return view('schedules')->with($schedules);
    });

is this possible?

topvillas
topvillas
1 week ago (153,445 XP)

Try it and see.

deladels

i just did.

i get this error when trying to view the schedules.blade.php file.

Undefined variable: schedules (View: C:\xampp\htdocs\BusTravel\resources\views\schedules.blade.php)

tykus
tykus
1 week ago (432,930 XP)

You need to specify the name the variable will have in your view:

return view('schedules')->with('schedules', $schedules);

or

return view('schedules')->withSchedules( $schedules);

or

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

or

return view('schedules', compact('schedules'));
deladels

so all 4 alternatives are not working unfortunately

tykus
tykus
1 week ago (432,930 XP)

so all 4 alternatives are not working unfortunately

Not working with the same error as before, or a new issue?

What are you expecting this to do function(App\Schedule $schedules); because I don't think it will do what your think it will do:

Route::get('schedules', function() {
    $schedules = App\Schedule::all(); // or whatever your query is...
    return view('schedules', compact('schedules'));
});
deladels

yes not working with the same error as before.

tried this: not working.

Route::get('schedules', function() {
    $schedules = App\Schedule::all(); // or whatever your query is...
    return view('schedules', compact('schedules'));
});

i am trying to route a user the schedules page which will display all scheduled in the database. i have a loop in the blade file with the $schedules variable. that's where the error is coming form.

    <table class="striped center responsive-table">
        <thead>
                <th>FROM</th>
                <th>TO</th>
                <th>DEPARTURE DATE</th>
                <th>DEPARTURE TIME</th>
                <th>BUS NO.</th>
                <th>PRICE</th>              
        </thead>
        <tbody>
            @foreach ($schedules as $schedule)
                <tr>
                    <td>{{ $schedule->departure_location }}</td>
                    <td>{{ $schedule->destination_location }}</td>
                    <td>{{ \Carbon\Carbon::parse($schedule->departure_date)->format('M d Y') }}</td>
                    <td>{{ date("h:i a", strtotime($schedule->departure_time)) }}</td>
                    <td>{{ $schedule->bus}}</td>
                    <td>{{ $schedule->price}}</td>
                    <td> <a href="schedule/{{$schedule->id }}" class="btn waves-effect red darken-1">BOOK</a> </td>
                </tr>
            @endforeach
        </tbody>
    </table>
jorpedito

@deladels very very stange. when u have an error like that is because your table schedules have no records . Just do


Route::get('schedules', function() {
    $schedules = App\Schedule::all(); // or whatever your query is...

   dd($schedules);

    return view('schedules', compact('schedules'));
});


deladels

yes @jorpedito . but there are records in there. a lot.

Route::get('schedules', function() {
    $schedules = App\Schedule::all(); // or whatever your query is...

   dd($schedules);

    return view('schedules', compact('schedules'));
});

same error for this dd isnt working.

tykus
tykus
1 week ago (432,930 XP)

same error for this dd is working.

That is very strange; do you have another route defined that is being matched instead? I suspect you are not hitting the route you are showing us.

For a sanity check, can you just dd(__FILE__) to see if we are hitting this route at all?

Route::get('schedules', function() {
    dd(__FILE__);

    // $schedules = App\Schedule::all(); // or whatever your query is...

    // return view('schedules', compact('schedules'));
});
deladels

so i have another route like that being accessed by another controller. could that be the error?

my route file


Route::get('/schedules', '[email protected]');

Route::get('schedule/{schedule}', '[email protected]');

Route::get('/schedules', '[email protected]');

Route::get('/braintree/token','[email protected]');

Route::post('/login/custom', [
    'uses' => '[email protected]',
    'as'   => 'login.custom'
]);
Route::group(['middleware' => 'auth'], function() {

Route::get('schedules', function() {
    dd(__FILE__);

});


    Route::get('dashboard', function() {
        return view('dashboard');
    });

});

tykus
tykus
1 week ago (432,930 XP)

You have two Route::get('/schedules' definitions before you get to the closure route!!!!! How can you expect that to work?

deladels

UPDATED

Route::get('/schedules', '[email protected]'); was duplicated during copy and paste

so i have another route like that being accessed by another controller. could that be the error?

my route file


Route::get('/schedules', '[email protected]');

Route::get('schedule/{schedule}', '[email protected]');


Route::get('/braintree/token','[email protected]');

Route::post('/login/custom', [
    'uses' => '[email protected]',
    'as'   => 'login.custom'
]);
Route::group(['middleware' => 'auth'], function() {

Route::get('schedules', function() {
    dd(__FILE__);

});


    Route::get('dashboard', function() {
        return view('dashboard');
    });

});

tykus
tykus
1 week ago (432,930 XP)

The point was that this:

Route::get('/schedules', '[email protected]');

will get matched before you ever get to this:

Route::get('schedules', function() {
    dd(__FILE__);
});

because they are the same URI.

deladels

very much understood. any suggestions?

tykus
tykus
1 week ago (432,930 XP)

What does '[email protected]' do that your closure route does not?

Why do you need another schedules URI in your app?

Please sign in or create an account to participate in this conversation.