将数据从视图传递到Laravel中的控制器 [英] Passing data from view to controller in Laravel

查看:240
本文介绍了将数据从视图传递到Laravel中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过URL传递记录ID通常不是一个好主意,但我想知道如何避免这种情况:

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:

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

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

userController.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);
    }

这部分效果很好,我没有在路线中使用记录ID.我遍历仪表板视图中的jobInterviews.根据数据库表中列出的状态,为用户提供

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

查看文件:dashboard.blade.php(摘要)

view file: dashboard.blade.php (snippet)

@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>
         ...

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

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:

仍然在dashboard.blade.php中

Still in dashboard.blade.php

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

然后将其路由到:

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

一切都按我的意愿进行,但是从安全的角度来看,我认为我做的不对.从经过迭代的数组中获取数据时,除了在路由中使用记录ID之外,还有没有更好的方法来调用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?

预先感谢您的帮助.

推荐答案

您可以尝试以下方法:

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

这样,您无需手动添加csrf/_method输入,默认情况下,METHOD将为POST,因此您可以省略.

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.

这篇关于将数据从视图传递到Laravel中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆