Laravel: 如何将href id 存储在新变量中并在输入字段中显示或使用?

Laravel: How to store href id in the new variable and display or use in the input field?

我想从 href 存储当前用户 $emp->id ,并像在下面编写的代码中一样在输入值中使用。有可能吗?或者如果可能的话请帮助我?如果这个问题不是一个大问题,那么我提前为此感到抱歉。

<a href="{{'/employee'}}?id={{$emp->id}}" type="button" name="user_id" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Apply Attribute
</a>

<form action="{{'/rating'}}" method="post">

   {{csrf_field()}} 

   <input type="hidden" name="user_id" value="{{store here current user}}">    

</form>

看来您只需要将当前用户粘贴到 INPUT 中:

<form action="{{'/rating'}}" method="post">

   {{csrf_field()}} 

    <input type="hidden" name="user_id" value="{{ $emp->id }}">    

</form>
.....

如果您想使用来自 url 的 ID,您可以:

Route::get('/url/{emp}', 'YourController@method');

在你的控制器中:

public function method(Employee $emp) {
    //Your code
    return view('youre.view', compact('emp'))
}

现在在您看来您拥有 $emp 并且可以像 $emp->id 这样访问 ID。 当然,你可以随意命名,但一定要在你的路由、控制器和视图中使用相同的名称。

现在您不需要 forEach 循环,因为您已经有了来自 url

的员工的绑定

p.s: Employee 模型只是一个假设..不管你的模型叫什么都可以命名它。

为此,您可以使用 blade 中的路由功能。

在 web.php 上试试这个:

Route::get('/employee/{id}', 'YourController@YourMethod')->name('routename');

在你的控制器上,你的方法需要有一个参数

public function YourMethod($id){
   // Code here
}

然后在您的 blade 上使用路由

创建您的 href
<a href=" {{ route('routename', ['id' = $emp->id]) }}">Link</a>

像这样的 Href:

<a href="{{$emp->id}}" type="button"  id="uu_id" class="btn btn-primary uu" 
    data-toggle="modal" data-target="#myModal"> Apply Attribute </a>

使用此脚本从 Href 获取值:

<script type="text/javascript">
    $(document).ready(function() {
        $(".uu").click(function(event) {
            var u_id = $(this).attr('href');
            event.preventDefault();
            document.getElementById("hiddenVal").value = u_id;
        });

    });
</script>

在你的表单中这样:

<form action="{{'/rating'}}" method="post">
 {{csrf_field()}} 

 <input type="submit"  style="margin-bottom: 10px;" class="btn btn-success 
 pull-right" name="apply" value="Apply"/>
   <input type="hidden" name="hiddenVal" id="hiddenVal" />
</form>

最后如何在控制器中获取此值并保存到数据库:

 public function store(Request $request)
{
    $rates = new Rating;
    $user_id = $_POST['hiddenVal'];
    $rates->user_id = $user_id;    
    $rates->save();
    return redirect('/employee');
}