I try to make selection on id in the project on the documentation (costs laravel 5.3).

routes / web.php file:

Route::get('/search/propertyprofile/{id}', [ 'uses' => 'PropertyProfileController@getPropertyProfile', 'as' => 'sh.search.propertyprofile' ]); 

In this route, I wrote the address as the first parameter, then I use the controller and the action and it should go to the specified view (here it’s ALL good if the view has the Hello world output code and no parameters)

Next, I create an action (with the parameters $ id and the variable $ request) in which I enter the data from the model in $ properties (where the id parameter corresponds to the $ id variable) and return to vhu (where the view parameter and the second parameter are the data from the database table $ properties)

PropertyProfileController:

 use App\Property; use Illuminate\Http\Request; class PropertyProfileController extends Controller { public function getPropertyProfile($id,Request $request) { $properties = Property::where('id', $id); return view('sh.propertyprofile', ['properties' => $properties]); } } 

Property model (everything seems to be clear here):

 class Property extends Model { protected $fillable = ['id','imagePath', 'title', 'description', ]; } 

In the view, I call the title and imagePath data from the $ properties variable (into which they are listed from the database table):

 @extends('layouts.master') @section('content') <img src="{{ $properties->imagePath }}" alt="..." class="img-responsive"> <h3>{{ $properties->title }}</h3> @endsection 

Gives an error message :

 Undefined property: Illuminate\Database\Eloquent\Builder::$imagePath (View: /var/www/projects/auth.laravel.com/resources/views/sh/propertyprofile.blade.php) 

Apache logs:

 "GET /search/propertyprofile/8 HTTP/1.1" 500 43913 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0" 

Why does he say the variable variable imagePath ?? Please tell me where I was wrong or what is wrong with my judgment .... (did according to the documentation https://laravel.com/docs/5.4/routing#route-parameters )

  • What's this? $properties = Property::where('id', $id) - vp_arth
  • @vp_arth I think it's too lazy to read the dock, from beginning to end - Orange_shadow

2 answers 2

In the PropertyProfileController file, you wrote a condition, but did not make a selection, it will be correct:

 $properties = Property::where('id', $id)->first(); 

If id is your PK, then you can simply do:

 $properties = Property::find($id); 
  • laravel lazy-download can not what? - teran
  • one
    He knows how but this is the option, here the author set the sampling condition, but did not say what to do with the request, check if there is such a record in the database (exists method) or select one record with this parameter (first method) or select all records with this parameter ( get method), etc. that's why the builder swears, not the model - Yaroslav Molchan
  • @teran, where builder returns, lazy loading has nothing to do with it. Where to add it here? - vp_arth
  • Yaroslav Molchan, as you understood, this is mine all the same moment, I just get to something specific and post it in a new question! Thank you so much, everything works, you figure out what it is and yes, the method is> first (); it was just not enough! Thank you once again very much to you, as well as to all the participants in this discussion! - Alexander
  • one
    @ Alexander, yes, I remember this code, this time I’ve got a good question with all the outgoing data :) - Yaroslav Molchan

Why do you use Request $request in this function if it is not even used?

 public function getPropertyProfile($id,Request $request) { $properties = Property::where('id', $id); return view('sh.propertyprofile', ['properties' => $properties]); } 

For correctness, of course, you should remove it, but if you intend to use it in the future, it is better to switch places otherwise the function will not work.

 public function getPropertyProfile(Request $request, $id) { $properties = Property::find($id); return view('sh.propertyprofile', ['properties' => $properties]); } 
  • iAxel, thanks for the comment and clarification) PS: even in my case, it worked, I would be very grateful if you explain to me your clarification (this nuance), otherwise I didn’t even understand Request-even ... .. - Alexander
  • @iAxel "Pizdesh and provocations" :) sorry, I could not resist :) :) The trick is that Laravel is parsing the parameters! and determines their type! her drum on the turn !!! - Orange_shadow
  • @iAxel Even here I didn’t hold back the article I found :) :) the headline of Eco-friendly dependency injection, and there’s a little lower - Orange_shadow
  • one
    @ Alexander, I just walked through the documentation, and there were moments when my “star” really didn't work ... - iAxel