简体   繁体   English

Laravel Chained Select加载资源失败:服务器响应状态为500

[英]Laravel Chained Select Failed to load resource: the server responded with a status of 500

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

The above option value refers to the client id which I want to pass into the projects db and find the clients projects. 上面的选项值引用了我要传递到项目数据库并查找客户项目的客户ID。 I'm not sure what I am doing wrong and would appreciate some help in a somewhat complex task for a newbie. 我不确定自己做错了什么,对于新手来说,在一些复杂的任务中会有所帮助,将不胜感激。

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

routes.php 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 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('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 ##

在此处输入图片说明

I'm not sure how laravel works but obviusly $client_id is undefined in create method. 我不确定laravel如何工作,但是在create方法中显然$client_id是未定义的。 Try passing it as parameter. 尝试将其作为参数传递。

About javascript: 'itempus.dev/task/create' + clients.val() will produce something like itempus.dev/task/create1 (assuming value in select is numeric). 关于javascript: 'itempus.dev/task/create' + clients.val()将产生类似itempus.dev/task/create1 (假设select中的值为数字)。 You miss slash after create. 创建后,您会错过斜线。 Maybe that's why you can't get routing works. 也许这就是为什么您无法进行路由工作的原因。

Cheers 干杯

In your code (read the comments about $project and Response::eloquent() ): 在您的代码中(阅读有关$projectResponse::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()的作用以及您要尝试的操作。

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:C:\\ xampp \\ htdocs \\ iTempus \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\中的异常'Symfony \\ Component \\ HttpKernel \\ Exception \\ NotFoundHttpException' Router.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 line Probably 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. 从注释中Trying to get property of non-object是您的真正错误。 You probably are selecting some records from your table and at least one of them is not returning as object, is probably null. 您可能正在从表中选择一些记录,并且其中至少有一个未作为对象返回,可能为null。

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>

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的标签

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel链接/链接选择框无法加载资源状态500内部服务器错误 - Laravel chained/linked select box failed to load resource status 500 internal server error 加载资源失败:服务器响应状态为500(); 在当地环境中工作 - Failed to load resource: the server responded with a status of 500 (); works in local environment 无法加载资源:服务器响应状态为500,Codeigniter项目 - Failed to load resource: the server responded with a status of 500, Codeigniter project Laravel 7 - 加载资源失败:服务器响应状态为 400(错误请求) - Laravel 7 - Failed to load resource: the server responded with a status of 400 (Bad Request) 加载资源失败:服务器在 Heroku 上响应状态为 404(未找到)Laravel - Failed to load resource: the server responded with a status of 404 (Not Found) Laravel on Heroku 无法加载资源:服务器在AJAX调用中以500(内部服务器错误)状态响应 - Failed to load resource: the server responded with a status of 500 (Internal Server Error) on AJAX call 加载资源失败:服务器响应状态为500(内部服务器错误)-Angularjs和Codeigniter - Failed to load resource: the server responded with a status of 500 (Internal Server Error) - Angularjs and Codeigniter Ajax错误:无法加载资源:服务器响应状态为500(内部服务器错误) - Ajax error : Failed to load resource: the server responded with a status of 500 (Internal Server Error) 电子邮件表格不会发送电子邮件 无法加载资源:服务器响应状态为500(内部服务器错误) - email form will not send email. Failed to load resource: the server responded with a status of 500 (Internal Server Error) CodeIgniter:无法加载资源:服务器以状态500(内部服务器错误)响应 - CodeIgniter : Failed to load resource: the server responded with a status of 500 (Internal Server Error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM