简体   繁体   English

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

[英]Passing data from view to controller in Laravel

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.这部分效果很好,我不在路线中使用记录 ID。 I iterate through the jobInterviews in the dashboard view.我遍历仪表板视图中的 jobInterviews。 Depending up on the status listed in the DB table, I give the user options根据数据库表中列出的状态,我给用户选项

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.在此先感谢您的帮助。

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/_method input manually and by default it's METHOD would be POST so you can omit that.这样你就不需要手动添加csrf/_method输入,默认情况下它的METHOD将是POST所以你可以省略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM