在 Laravel 中将数据从视图传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24500020/
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 09:44:13  来源:igfitidea点击:

Passing data from view to controller in Laravel

phplaravellaravel-4

提问by retrograde

I understand that passing record ids through the url isn't usually a good idea, but I am wondering how I can avoid it in my case:

我知道通过 url 传递记录 ID 通常不是一个好主意,但我想知道在我的情况下如何避免它:

My objective is to list job statuses on a user dashboard and allow users to adjust the status.

我的目标是在用户仪表板上列出工作状态并允许用户调整状态。

I create my view and pass variables to it using the session:

我创建我的视图并使用会话将变量传递给它:

userController.php

用户控制器.php

public function getdashboard()
    {
       //reading the user information
    $arrPageData['user'] = Sentry::getUser();
       //reading the job interviews
    $arrPageData['jobInterviews'] = JobInterview::readCurrentInterviews($this->userID);

    return View::make('clients.dashboard', $arrPageData);
    }

This part works great and I don't use the record id in the route. I iterate through the jobInterviews in the dashboard view. Depending up on the status listed in the DB table, I give the user options

这部分效果很好,我不在路线中使用记录 ID。我在仪表板视图中遍历 jobInterviews。根据数据库表中列出的状态,我给用户选项

view file: dashboard.blade.php (snippet)

查看文件:dashboard.blade.php(片段)

@foreach ($jobInterviews as $interviews)
    @if ($interviews->j == $job->id)
        <tbody>
        <tr>
        <td>
        {{$interviews->contact_name}}
        @if ($interviews->status == 'interview request accepted')

    <a href="#"  class="btn btn-danger btn-small" data-toggle="modal" data-target=".mymodal{{ $interviews->interview_id }}">Hire</a>
       @elseif ($interviews->status == 'hired')
        <button id="complete" class="btn btn-info btn-small">Mark Project Complete</button>
        @endif
        </td>
        <td>{{$interviews->status}} </td>
        </tr>
        </tbody>
         ...

The problem that I am having is that to complete the job status change, I am calling the method and passing in the record id:

我遇到的问题是要完成作业状态更改,我正在调用该方法并传入记录 ID:

Still in dashboard.blade.php

仍然在dashboard.blade.php

<form action="../jobs/offer/{{$interviews->interview_id}}" method="post">

This is then routed through:

然后通过以下方式路由:

Route::post('/jobs/offer/{id}','JobController@jobOffer');

Everything works as I want it to but I don't think I am doing it right from a security stand point. Is there a better way to call the jobOffer method and change the status besides using the record id in the route when getting the data from an array i've iterated through?

一切都如我所愿,但从安全的角度来看,我认为我做得不对。当从我迭代的数组中获取数据时,除了使用路由中的记录 ID 之外,是否有更好的方法来调用 jobOffer 方法并更改状态?

Thanks in advance for the help.

在此先感谢您的帮助。

回答by The Alpha

You may try this:

你可以试试这个:

{{ Form::open(array('action' => array('JobController@jobOffer', $interviews->interview_id))) }}
    <!-- Rest of the form fields -->
{{ Form::close() }}

This way you don't need to add csrf/_methodinput manually and by default it's METHODwould be POSTso you can omit that.

这样,您就不需要添加csrf/_method人工输入,默认情况下它METHOD会是POST这样你就可以省略。