doulao2916 2017-01-14 13:04
浏览 89
已采纳

从视图传递数据到视图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

  • 写回答

3条回答 默认 最新

  • doushi3819244 2017-01-14 15:55
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)