laravel 保存时如何在Laravel中获取当前模型的id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30465374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:38:28  来源:igfitidea点击:

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

phplaravellaravel-5

提问by Lucien Dubois

I am working in a Laravel 5 application. I try to save a commentfor a projetand I don't know how to get the $comment->project_idvalue.

我在 Laravel 5 应用程序中工作。我尝试保存注释,我不知道如何获得$ comment-> PROJECT_ID值。

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?

我猜这是因为它试图获取为空的新对象的 sollicitation_id。我应该如何获取当前的 project_id 值?

Update

更新

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

结论:我使用了一个隐藏字段并遵循了@tommy 的建议。我的控制器现在使用

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

and my hidden field is

我的隐藏领域是

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

采纳答案by tommy

In the store method, you try to get the property projectof 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_idto your form.

在 store 方法中,您尝试获取不存在project的变量的属性$note。您应该通过将项目 ID 添加到路由或向project_id表单添加隐藏字段来将项目 ID 传递给 store 方法。

Then, your store method should look something like this:

然后,您的 store 方法应如下所示:

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');

如果要在表单中添加带有项目 ID 的隐藏字段,可以通过调用访问其值 $request->input('project_id');

回答by Daniel Waters

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

我觉得上面的答案远非完美,因为您不仅向用户公开唯一 ID,而且冗长乏味,如果两个用户同时加载同一页面,则会失败,而您应该这样做

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

回答by MH Raihan

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

您可以使用 insertGetId() Query Builder 方法获取最后一个 id

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

如果表有自动递增的 id,则使用 insertGetId 方法插入一条记录,然后检索该 ID:

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

for more info check document

欲了解更多信息,请查看文档