gpt4 book ai didi

php - Laravel Chained Select 无法加载资源 : the server responded with a status of 500

转载 作者:行者123 更新时间:2023-12-01 03:40:17 25 4
gpt4 key购买 nike

完成编辑

我编辑了原来的问题,因为我改进了代码,这使我能够更好地定义更好的错误

您好,我正在创建一个链接选择框,一旦选择了客户,就会找到客户项目。

ajax 正在执行其工作,它知道已选择哪个客户端,并且我的控制台告诉我以下内容:

无法加载资源:服务器响应状态为 500(内部服务器错误)http://itempus.dev/task/clientsprojects?option=5

上面的选项值是指我想要传递到项目数据库并查找客户端项目的客户端 ID。我不确定我做错了什么,并且希望为新手提供一些帮助来完成有些复杂的任务。

任务 Controller

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')));
}

View /任务/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>

路线.php

我也这样定义了我的路线:

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')));
});

更新

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

应用程序/存储/日志

[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} [] []

更新2

修改了代码,仍然是同样的错误。

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

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

创建.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>

更新 3 ##

enter image description here

最佳答案

如果您的日志中仍然存在这些错误:

[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

这是 Laravel 的说法“我无法找到具有这些特性的路线”

您的问题可能更简单:您的 JavaScript 无法打开该路由,因为 URL 的格式可能不正确。

$.get("{{ url('task/clientsprojects')}}", ...

1) 打开 HTML

2) 从上面的行获取生成的 URL 可能类似于:

http://itempus.dev/task/clientsprojects?option=7

或者只是

http://itempus.dev/task/clientsprojects

3) 在 Chrome 上,按 F12 打开开发工具

4) 使用此 URL 构建如下所示的命令,然后粘贴开发工具命令行(然后按 Enter 键):

$.get('http://itempus.dev/task/clientsprojects');

5)检查错误并尝试

$.get('http://itempus.dev/');

您应该获得一些数据,有时很多,但没有错误。在 JavaScript 中处理数据甚至可能是错误的,但此时您不应该出现错误。

6) 获取相同的 URL 并尝试将其粘贴到浏览器地址栏中,然后检查是否没有收到错误

如果你没有收到错误,你的路由是好的,否则你将不得不再次检查你的routes.php文件,那里的东西不会让javascript(甚至你通过浏览器URL)命中你的路线。

另一种可能性是 Controller 内部发生异常,但通过手动点击该路由,您也应该能够看到它。但是,如果您仍然遇到这些 NotFoundHttpException 错误,则可能是路由问题。

为了确保这些错误是相关的。执行:

php artisan tail

并检查错误是否在 javascript 命中该路由的同一时间发生。

编辑 1

从评论中尝试获取非对象的属性是你真正的错误。您可能正在从表中选择一些记录,并且至少其中一条没有作为对象返回,可能为 null。

编辑2

“尝试获取非对象的属性”意味着您正在尝试将不是对象的变量作为对象进行访问。此行导致错误:

 $response[$i]['id'] = $project->id;

将查询更改为

$projects = Project::where('client_id', $client->id)
->orderBy('project_name')
->get(array('id','project_name'));

应该有助于使其发挥作用。

编辑3

总结:

这应该作为您的路线:

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

还有这个 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>

关于php - Laravel Chained Select 无法加载资源 : the server responded with a status of 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22256066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com