简体   繁体   中英

Laravel 5.2 display single record

I have downloaded a script written in Laravel i guess 5.2. I am having trouble showing single data in a row and column. It only shows the id but not the rest of the row. I have this code so far:

search.blade.php

<option value="">Search for...</option>
   @foreach(\App\SubCategories::orderBy('sub_category_name')->get() as $search_keyword) 
     <option value="{{$search_keyword->sub_category_slug}}">&nbsp;{{$search_keyword->sub_category_name}}</option> 
   @endforeach

<select id="location" name="location" class="form-control" required>
     <option value="">Select Location</option>
        @foreach(\App\Location::orderBy('location_name')->get() as $location) 
            <option value="{{$location->id}}">{{$location->location_name}}</option> 
        @endforeach
</select>

ListingsUserController.php

public function search_listings(Request $request)    
{ 

   $inputs = $request->all();

   $keyword = $inputs['search_keyword'];
   $location = $inputs['location'];

   $listings = Listings::SearchByKeyword($keyword,$location)->get();

   $total_res=count($listings);  

    return view('pages.search',compact('listings','total_res','keyword','location'));
}

search.blade.php

{{$total_res}} results for {{$keyword}}


{{$total_res}} results for {{$keyword}} in {{$location}}

I have added {{$location}} on the last line so I will display:

5 results for Keyword in Location

but it shows the id instead of location_name

5 results for Keyword in 10

I get an error if I put like this {{$location->location_name}}

I know this is so basic in Laravel but I am new to PHP Framework.

Anyone please?

$location = $inputs['location']; - This is just an ID being sent over from the server on form submission.

You need to load the model first, something like this should work:

$location = Location::find($inputs['location']);

$listings = Listings::SearchByKeyword($keyword,$location->id)->get();

Now things will work correctly for you as $location is an object with properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM