0

I want to get the ID which was passed in the URL but it seems something goes wrong!

This is my routes.php

Route::get('/poste/{idp}',array('before' => 'members_auth',function($id){
//dd($id);

$post =Posts::where('idp','=',$id) -> get();

$titre=$post->titre;

$desc=$post->description;

return View::make('showPost',array('title'=>$titre,'description'=>$desc));
})); 

And this is my View

<a href="/poste/{{$userpost->idp}}">

The error was Undefined property: Illuminate\Database\Eloquent\Collection::$titre

1 Answer 1

2

It is quite common issue when you start with Eloquent ORM. Basically get() method always returns Collection of objects, even if there is only one found. Sure enough Collection object has no titre property. If idp is primary key for your Post model, you should use find() or findOrFail() method instead.

$post = Posts::find($id);

As Laravel documentation states:

Note: Eloquent will also assume that each table has a primary key column named id. You may define a primaryKey property to override this convention. Likewise, you may define a connection property to override the name of the database connection that should be used when utilizing the model.

You are free to override your primary key.

class Posts extends Eloquent 
{
    protected $primaryKey = 'idp';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes idp is a primary key , but when i tried with $post = Posts::find($id); return View::make('showPost')->with('idpost',$post->idp); it shows this error : Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from posts where id = 25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.