控制器没有得到ID从laravel路线 [英] Controller not get id from the route in laravel
问题描述
正在开发使用laravel一个叫做PHP应用程序。在这当我试图通过值从视图中使用Ajax调用控制器。我在这个路由有一个问题,就是我的code路由
am developing one php application using laravel. in that when i trying to pass value from view to controller using ajax call. am having a issue in routing this is my code in routing
Route::post('AssetdetailsController/postPositions/{id}', array('as'=>'post_form','uses' => 'Controllers\Admin\AssetdetailsController@postPosition'));
我试图传递路线值在JavaScript警告我得到一个空的警告框。
am trying to pass the route value in javascript alert am getting an empty alert box.
这是我的ajax调用
$(document).ready(function() {
$('#asset_type_id').change(function() {
// alert($('#asset_type_id option:selected').val());
var id = $('#asset_type_id option:selected').val();
$.ajax({
type: 'POST',
url: '{{ URL::route('post_form') }}',
data: id,
cache: false,
success: function(data)
{
alert(data);
check(data);
},
error: function(xhr, textStatus, thrownError) {
alert('Something went to wrong.Please Try again later...');
}
});
});
});
这是我的控制器
public function postPosition($id) {
$positions = DB::table('asset_types')->where('id', '=', $id )->pluck('is_nesd');
return $positions;
}
我想这样的检查,我是通过警告框
I tried like this to check the value that i am getting in id through alert box
public function postPosition($id) {
return $id;
}
我在警告框了输出{ID}。
I got output as {id} in alert box.
我也试过在查询,而不是$ {ID}和IAM得到正确的输出
I also tried passing a value manually in query instead of ${id} and iam getting the correct output
public function postPosition($id) {
$positions = DB::table('asset_types')->where('id', '=', 1 )->pluck('is_nesd');
return $positions;
}
所以,现在我的问题是如何获得$ {ID}正确地从视图中的价值。
So now my question is how get the value of ${id} correctly from view.
推荐答案
我终于得到了答案,我所做的是
Hi i finally get the answer what i did is
在AJAX功能,我的老code是
In ajax function my old code is
url: '{{ URL::route('post_form') }}',
和我改成
url: '{{ action("postPositions", " ") }}'+id,
这一次正常工作
This one working correctly
这篇关于控制器没有得到ID从laravel路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!