Laravel Chained Select 加载资源失败:服务器响应状态为 500
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22256066/
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
Laravel Chained Select Failed to load resource: the server responded with a status of 500
提问by 001221
Complete Edit
完成编辑
I've edited my original question as I've refined my code which has put me in a much better position to define a better error
我已经编辑了我的原始问题,因为我已经改进了我的代码,这使我处于更好的位置来定义更好的错误
Hi I'm creating a chained select box that will once a client is selected find the clients projects.
嗨,我正在创建一个链式选择框,一旦选择了客户,它就会找到客户项目。
The ajax is doing its job it knows which client has been selected and my console tells me the following:
ajax 正在做它的工作,它知道选择了哪个客户端,我的控制台告诉我以下内容:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://itempus.dev/task/clientsprojects?option=5
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://itempus.dev/task/clientsprojects?option=5
The above option value refers to the client id which I want to pass into the projects db and find the clients projects. I'm not sure what I am doing wrong and would appreciate some help in a somewhat complex task for a newbie.
上面的选项值是指我想传递到项目数据库中并找到客户端项目的客户端 ID。我不确定我做错了什么,如果对新手来说有点复杂的任务,我将不胜感激。
TaskController
任务控制器
public function create()
{
$tasks = Auth::user()->tasks;
$client_options = DB::table('clients')->orderBy('client_name', 'asc')->lists('client_name','id');
$team_options = DB::table('teams')->orderBy('team_member_name', 'asc')->lists('team_member_name','id', 'team_member_category');
return View::make('tasks.create', array('project_options' => $project_options, 'team_options' => $team_options, 'client_options' => $client_options));
}
public function clientsprojects() {
$input = Input::get('option');
$client_id = Project::find($input);
$projects = DB::table('projects')->where('client_id', $client_id->id)
->orderBy('project_name')
->lists('id','project_name');
$models = $project->projects();
return Response::eloquent($models->get(array('id','project_name')));
}
views/tasks/create.blade.php
视图/任务/create.blade.php
{{ Form::open(array('action' => 'TaskController@store', 'id' => 'createuser')) }}
<div class="form-group">
@if(count($client_options)>0)
{{ Form::label('select_client', 'Assign to Client', array('class' => 'awesome client_option')); }}
{{ Form::select('client', $client_options , Input::old('client'), array('class' => 'tempus_select client_option', 'id' => 'select_client')) }}
@endif
</div>
<div class="form-group deletegates">
{{ Form::label('select_client', 'Assign to Project', array('class' => 'awesome')); }}
{{ Form::select('project', array_merge(array('default' => 'Please Select')), 'default', array('class' => 'tempus_select', 'id' => 'project_select')) }}
</div>
{{ Form::submit('Create the task!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
<script>
$(document).ready(function($){
$('#select_client').change(function(){
$.get("{{ url('task/clientsprojects')}}",
{ option: $(this).val() },
function(data) {
var model = $('#project_select');
model.empty();
$.each(data, function(index, element) {
model.append("<option value='"+ element.id +"'>" + element.name + "</option>");
});
});
});
});
</script>
Route.php
路由.php
I've also defined my route as so:
我也这样定义了我的路线:
Route::get('task/clientsprojects', function(){
$input = Input::get('option');
$client_id = Project::find($input);
$projects = DB::table('projects')->where('client_id', $client_id->id)
->orderBy('project_name')
->lists('id','project_name');
$models = $project->projects();
return Response::eloquent($models->get(array('id','project_name')));
});
Update
更新
jquery console errorGET http://itempus.dev/task/clientsprojects?option=7 500 (Internal Server Error) jquery.js:8475
send jquery.js:8475
st.extend.ajax jquery.js:7930
st.(anonymous function) jquery.js:7569
(anonymous function) create:210
st.event.dispatch jquery.js:3045
y.handle jquery.js:2721
app/storage/logs
应用程序/存储/日志
[2014-03-12 17:01:00] log.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\Router.php:1429
Stack trace:
#0 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1050): Illuminate\Routing\Router->handleRoutingException(Object(Symfony\Component\Routing\Exception\ResourceNotFoundException))
#1 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1014): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(574): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#3 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(550): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#4 C:\xampp\htdocs\iTempus\public\index.php(49): Illuminate\Foundation\Application->run()
#5 {main} [] []
[2014-03-12 17:01:00] log.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\Router.php:1429
Stack trace:
#0 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1050): Illuminate\Routing\Router->handleRoutingException(Object(Symfony\Component\Routing\Exception\ResourceNotFoundException))
#1 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1014): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(574): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#3 C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(550): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#4 C:\xampp\htdocs\iTempus\public\index.php(49): Illuminate\Foundation\Application->run()
#5 {main} [] []
Update 2
更新 2
Revised code and still the same error.
修改了代码,还是一样的错误。
TaskController.php
任务控制器.php
public function create()
{
$tasks = Auth::user()->tasks;
$client_options = DB::table('clients')->orderBy('client_name', 'asc')->lists('client_name','id');
$project_options = DB::table('projects')->orderBy('project_name', 'asc')->lists('project_name','id');
$team_options = DB::table('teams')->orderBy('team_member_name', 'asc')->lists('team_member_name','id', 'team_member_category');
return View::make('tasks.create', array('project_options' => $project_options, 'team_options' => $team_options, 'client_options' => $client_options));
}
routes.php
路由文件
Route::get('task/clientsprojects', function(){
$input = Input::get('option');
$client = Client::find($input);
$projects = DB::table('projects')->where('client_id', $client->id)
->orderBy('project_name')
->lists('id','project_name');
$response = array(array());
$i = 0;
foreach($projects as $project){
$response[$i]['id'] = $project->id;
$response[$i]['name'] = $project->name;
$i++;
}
return json_encode($response);
});
create.blade.php
创建.blade.php
{{ Form::open(array('action' => 'TaskController@store', 'id' => 'createuser')) }}
<div class="form-group">
@if(count($client_options)>0)
{{ Form::label('select_client', 'Assign to Client', array('class' => 'awesome client_option')); }}
{{ Form::select('client', $client_options , Input::old('client'), array('class' => 'tempus_select client_option', 'id' => 'select_client')) }}
@endif
</div>
<div class="form-group deletegates">
{{ Form::label('project_select', 'Assign to Project', array('class' => 'awesome')); }}
{{ Form::select('project', array_merge(array('default' => 'Please Select')), 'default', array('class' => 'tempus_select', 'id' => 'project_select')) }}
</div>
{{ Form::submit('Create the task!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
<script>
$(document).ready(function($){
$('#select_client').change(function(){
$.get("{{ url('task/clientsprojects')}}",{
option: $(this).val()
}, function(data) {
var model = $('#project_select');
model.empty();
$.each(data, function() {
model.append('' + data.name + '');
});
}, 'json');
});
});
</script>
Update 3 ##
更新 3 ##
采纳答案by Antonio Carlos Ribeiro
If you are still having those errors in your log:
如果您的日志中仍然有这些错误:
[2014-03-12 17:01:00] log.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\Router.php:1429
[2014-03-12 17:01:00] log.ERROR: 异常 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\xampp\htdocs\iTempus\vendor\laravel\framework\src\Illuminate\Routing\路由器.php:1429
This is Laravel saying "I was not able to find a route with these charateristics".
这是 Laravel 所说的“我无法找到具有这些特性的路线”。
Proably your problem is simpler: your javascript is not being able to open that route, because, maybe, the URL is not well formed.
可能您的问题更简单:您的 javascript 无法打开该路由,因为 URL 可能格式不正确。
$.get("{{ url('task/clientsprojects')}}", ...
1) Open your HTML
1) 打开你的 HTML
2) Get the generated URL from the above lineProbably something like:
2)从上面那行获取生成的 URL大概是这样的:
http://itempus.dev/task/clientsprojects?option=7
or just
要不就
http://itempus.dev/task/clientsprojects
3) On Chrome, hit F12 to open dev tools
3) 在 Chrome 上,按 F12 打开开发工具
4) Use this URL to build a command like the one below and paste the dev tools command line (and hit enter):
4) 使用此 URL 构建如下所示的命令并粘贴开发工具命令行(并按 Enter):
$.get('http://itempus.dev/task/clientsprojects');
5) Check the error and also try
5)检查错误并尝试
$.get('http://itempus.dev/');
You should get some data, lots of it sometimes, but no errors. Data could even be wrong to process in javascript, but you should not get errors at this point.
你应该得到一些数据,有时很多,但没有错误。在 javascript 中处理数据甚至可能是错误的,但此时您不应该出错。
6) Get this same URL and try to paste it in your browser address bar and check if you don't get errors
6) 获取相同的 URL 并尝试将其粘贴到您的浏览器地址栏中并检查您是否没有收到错误
If you get no errors, your routes are good, otherwise you will have to check your routes.php
file again, something there is not letting javascript (or even you in via browser URL) hit your route.
如果你没有错误,你的路由是好的,否则你将不得不routes.php
再次检查你的文件,有些东西不会让 javascript(甚至你通过浏览器 URL 进入)命中你的路由。
Another possibility is an exception occuring inside your controller, but by hitting that route manually you should also be able to see it. But, again, if you're still having those NotFoundHttpException errors, probably is a routing problem.
另一种可能性是控制器内部发生异常,但通过手动点击该路由,您也应该能够看到它。但是,同样,如果您仍然遇到那些 NotFoundHttpException 错误,则可能是路由问题。
To be sure those errors are related. Execute:
确保这些错误是相关的。执行:
php artisan tail
And check if the error happen at the exact same time javascript hit that route.
并检查错误是否在 javascript 到达该路线的同时发生。
EDIT 1
编辑 1
From the comments Trying to get property of non-object
is your real error. You probably are selecting some records from your table and at least one of them is not returning as object, is probably null.
从评论Trying to get property of non-object
是你真正的错误。您可能正在从表中选择一些记录,并且其中至少一个没有作为对象返回,可能为空。
EDIT 2
编辑 2
"Trying to get property of non-object" means that you are trying to access a variable which is not an object as an object. This line is responsible for the error:
“试图获取非对象的属性”意味着您正在尝试访问一个不是对象的变量作为对象。这一行是造成错误的原因:
$response[$i]['id'] = $project->id;
Changing your query to
将您的查询更改为
$projects = Project::where('client_id', $client->id)
->orderBy('project_name')
->get(array('id','project_name'));
Should help make it work.
应该有助于使它工作。
EDIT 3
编辑 3
Wrapping up:
包起来:
This should work as your route:
这应该作为您的路线:
Route::get('task/clientsprojects', function(){
$input = Input::get('option');
$client = Client::find($input);
$projects = DB::table('projects')->where('client_id', $client->id)
->orderBy('project_name')
->lists('id','project_name');
$response = array();
foreach($projects as $project){
$response[$project->id] = $project->name;
}
return Response::json($response);
});
And this javascript:
这个javascript:
<script>
$(document).ready(function($){
$('#select_client').change(function(){
$.get("{{ url('task/clientsprojects') }}", { option: $(this).val() }, function(data) {
$.each(data, function(key, value) {
$('#project_select').append("<option value='"+key+"'>"+value+"</option>'");
});
});
});
});
</script>
回答by Ivan
I'm not sure how laravel works but obviusly $client_id
is undefined in create
method. Try passing it as parameter.
我不确定 laravel$client_id
是如何工作的,但显然在create
方法中是未定义的。尝试将其作为参数传递。
About javascript: 'itempus.dev/task/create' + clients.val()
will produce something like
itempus.dev/task/create1
(assuming value in select is numeric). You miss slash after create. Maybe that's why you can't get routing works.
关于 javascript:'itempus.dev/task/create' + clients.val()
会产生类似的东西
itempus.dev/task/create1
(假设 select 中的值是数字)。创建后您会错过斜线。也许这就是为什么你不能让路由工作的原因。
Cheers
干杯
回答by The Alpha
In your code (read the comments about $project
and Response::eloquent()
):
在您的代码中(阅读关于$project
和的评论Response::eloquent()
):
Route::get('task/clientsprojects', function(){
$input = Input::get('option');
$client_id = Project::find($input);
$projects = DB::table('projects')->where('client_id', $client_id->id)
->orderBy('project_name')
->lists('id','project_name');
// You don't have $project variable in the scope, instead it $projects
$models = $project->projects(); // <------
// I don't know about any method available as eloquent();
return Response::eloquent($models->get(array('id','project_name'))); // <----
});
This may doesn't solve the problem but provide more information, what is $project->projects()
for and what you are trying to do.
这可能无法解决问题,但会提供更多信息,包括您要做什么$project->projects()
以及您要做什么。
回答by clod986
OK, let's try to fix what you have so far.
好的,让我们尝试修复您目前所拥有的。
Your new route
你的新路线
Route::get('task/clientsprojects', function(){ $input = Input::get('option'); $client = Client::find($input); $projects = DB::table('projects')->where('client_id', $client->id) ->orderBy('project_name') ->lists('id','project_name'); $response = array(array()); $i = 0; foreach($projects as $project){ $response[$i]['id'] = $project->id; $response[$i]['name'] = $project->name; $i++; } return json_encode($response); });
And your script
还有你的剧本
$(document).ready(function($){ $('#select_client').change(function(){ $.get("{{ URL::to('/task/clientsprojects')}}",{ option: $(this).val() }, function(data) { var model = $('#project_select'); model.empty(); $.each(data, function() { model.append('' + data.name + ''); }); }, 'json'); }); });
Copy-paste and tell me how it works, and the errors you get
复制粘贴并告诉我它是如何工作的,以及你得到的错误
One last thing: fix the label for your #project_select
最后一件事:固定标签 #project_select