dpa55065 2018-10-18 13:58
浏览 23
已采纳

使用Laravel尝试检索ID的其他页面的数据

Quick explain: Retrieving data from DB with search bar. So a couple of data retrieving due to the search. And after the search also I want to display each data in a single page by their ID. For example, lots of estates data are retrieving like: Estate_Name, Estate_Address etc in a html table.I want to put estate_name a link to go another page which just retrieve that ID's data.

I updated the problem with new codes, and getting this error now:

Trying to get property of non-object (View: /var/www/html/laravel/resources/views/show.blade.php)

This is controller:

    public function show($id)
    {

       $estates = allestates::findOrFail($id);
       return view('show')->with(['estates' => $estates]);

    }

and this is my route:

Route::get('/show/{id}', 'PagesController@show');

also this the main.blade;

@foreach($estates as $estate)
    <tr class="even">
        <td>{{$estate->id}}</td>
        <td>{{str_limit($estate->company_name, $limit = 20)}}</td>
        <td><a href="/show/{{$estate->id}}">{{str_limit($estate->building_name, $limit = 20)}}</a></td>
        <td>{{str_limit($estate->address, $limit = 22)}}</td>

        <td>{{str_limit($estate->price, $limit = 20)}}</td>
        <td class="price-hidden">{{$estate->old_price}}</td>

        <td>{{str_limit($estate->extend, $limit = 20)}}</td>
        <td>{{str_limit($estate->rooms, $limit = 20)}}</td>
        <td>{{str_limit($estate->entry, $limit = 20)}}</td>
    </tr>
@endforeach

and this is the show.blade:

@foreach($estates as $estate)
    <tr class="even">
        <td>{{$estate->id}}</td>
        <td>{{str_limit($estate->company_name, $limit = 20)}}</td>
        <td>{{str_limit($estate->building_name, $limit = 20)}}</td>
        <td>{{str_limit($estate->address, $limit = 22)}}</td>

        <td>{{str_limit($estate->price, $limit = 20)}}</td>
        <td class="price-hidden">{{$estate->old_price}}</td>

        <td>{{str_limit($estate->extend, $limit = 20)}}</td>
        <td>{{str_limit($estate->rooms, $limit = 20)}}</td>
        <td>{{str_limit($estate->entry, $limit = 20)}}</td>
    </tr>
@endforeach

what am I doing wrong here? Any idea? Thank you! And also I have show blade that I want to just display one ID's data after click.

  • 写回答

1条回答 默认 最新

  • douhoujun9304 2018-10-19 06:37
    关注

    You must change it like this:

    public function show($id)
    {
    
        $estates = allestates::where('id', $id)->first();
        if($estates){
            return view('pages.show', ['estates' => $estates]);
        }else{
            return 'no records found';
        }
    }
    

    also route:

    Route::get('pages/{id}', 'PagesController@show');
    

    And

    <td><a href="/pages/{{$estate->id}}">{{str_limit($estate->building_name, $limit = 20)}}</a></td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序