Lucien Dubois
Lucien Dubois

Reputation: 1684

How to get the id of the current model in Laravel when saving

I am working in a Laravel 5 application. I try to save a comment for a projet and I don't know how to get the $comment->project_id value.

Here is my simplified controller

public function store( CommentsFormRequest $request )
{
    $comment = new Note;
    $comment->message              = Input::get('message');
    $comment->project_id           = $note->project->id;
    $comment->user_id              = Auth::id();
    $comment->save();

    return Redirect::back();
}

and here is my simplified form

  {!! Form::open(array('route' => 'notes.store')) !!}

    {!! Form::textarea('message', '', array('placeholder'=>'Message')) !!}

  {!! Form::submit('Ajouter la note') !!}
  {!! Form::close() !!}

When I try to save, I get this error:

Trying to get property of non-object

I guess it's because it tries to get the sollicitation_id of the new object wich is null. How should I get the current project_id value?

Update

Conclusion: I used an hidden field and followed @tommy 's recommendation. My controller now uses

$note->project_id  = $request->input('project_id');

and my hidden field is

{!! Form::hidden('project_id', $project->id ) !!}

Upvotes: 6

Views: 43343

Answers (4)

shrimpwagon
shrimpwagon

Reputation: 905

Only IF the table primary column name is 'id':

$model->id;

Regardless of primary column name:

$model->getKey();

Upvotes: 8

MH Raihan
MH Raihan

Reputation: 31

You can get last id by using insertGetId() Query Builder method

If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:

$id = DB::table('users')->insertGetId(
    ['email' => '[email protected]', 'votes' => 0]
);

for more info check document

Upvotes: 0

Daniel Waters
Daniel Waters

Reputation: 431

I feel the above answer is far from perfect as you're not only exposing Unique ID's to users but it's also long winded, and would fail if two users were to load the same page at the same time, instead you should do

public function store(CommentsFormRequest $request )
{
    $comment = new Note([
       // your fields here
    ]};
    $comment->save();
    //$comment now contains a unique ID!
    return redirect($comment->id);
}

Upvotes: 2

tommy
tommy

Reputation: 3615

In the store method, you try to get the property project of the variable $note, which does not exist. You should pass the project ID to the store method by either adding it to the route or adding a hidden field project_id to your form.

Then, your store method should look something like this:

public function store($project_id, CommentsFormRequest $request )
{
    $project = Project::find($project_id); // $project_id is transmitted over the URL

    $comment = new Note; // I'd alias Note as 'Comment', or rename '$comment' to '$note' because this can be confusing in the future
    $comment->project_id = $project->id;
    $comment->save();

    return Redirect::back();
}

If you want to add a hidden field with the project ID to the form, you can access its value by calling $request->input('project_id');

Upvotes: 2

Related Questions