Search code examples
phplaravelmodel-view-controllerviewlaravel-5.3

passing data from view to view laravel 5.3


i have a page called drivers.blade.php in which i'm displaying only few fields from database and i have a view-more-link on clicking that the user should go to driver-details

where i'm displaying all the details of the driver, it's some thing like product and product-detail page

My drivers.blade.php

@foreach($drivers as $driver)

<div class="col s12 m6 l3">
<div class="card small card-driver">
    <div class="card-image">
        <img src="assets/images/profile-image-2.png" alt="">
    </div>
    <div class="card-content">
        <div class="card-details">
            <p>Name:</p><span>{{ $driver->first_name }} &nbsp; {{ $driver->last_name }}</span>
        </div>
        <div class="card-details">
            <p>Phone:</p><span>{{ $driver->phone_number }}</span>
        </div>
        <div class="card-details">
            <p>Address:</p>
            <span>
                {{ $driver->address_city_village }} &nbsp; {{ $driver->address_state }}
            </span>
        </div>
    </div>
    <div class="card-action">
        <a href="driver-details" class="waves-effect waves-light btn green">View Details</a> //onclick go to that perticular driver details by passinf id
    </div>
</div>
</div>

@endforeach

My Controller is DriverController.php

public function getDrivers() 
{
    $drivers = Driver::all();

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

public function getDriverDetails($id) 
{
    $driver = Driver::find($id);

    if(! $driver) {
        return $this->respondNotFound();
    }

    return view('drivers-details', compact('driver'));

}

if i go to the http://localhost:8000/driver-details page i'm getting the following error

Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 161:

My Routes are

Route::resource('drivers', 'DriversController@getDrivers');
Route::resource('drivers-details', 'DriversController@getDriverDetails');

Looking forward for much needed help

Thank you


Solution

  • Route::resource is for CRUD only. if you are not using [index, create, store, show, edit, update, destroy] methods then you should not use Route::resource instead there are many other ways like Route::get, Route::post, etc.

    It is because you are using --

    Route::get('drivers-details', 'DriversController@getDriverDetails');
    

    and requesting for http://localhost:8000/driver-details which is not registered inside the routes, if you visit http://localhost:8000/drivers-details you will not get the error.

    In your controller, getDriverDetails($id), you gave a parameter, to use it you need to register it to your routes to use it like this --

    Route::get('drivers-details/{id}', 'DriversController@getDriverDetails');
    

    you also need to send the id to the url norder to access it --

    <a href="/drivers-details/{{ $driver->id }}" class="waves-effect waves-light btn green">View Details</a>