dongzhang0243 2014-03-07 16:57
浏览 82
已采纳

Laravel Chained Select无法加载资源:服务器响应状态为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:

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.

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

{{ 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

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

Revised code and still the same error.

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

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

{{ 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 ##

enter image description here

  • 写回答

4条回答 默认 最新

  • dove2199 2014-03-12 22:33
    关注

    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

    This is Laravel saying "I was not able to find a route with these charateristics".

    Proably your problem is simpler: your javascript is not being able to open that route, because, maybe, the URL is not well formed.

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

    1) Open your HTML

    2) Get the generated URL from the above line Probably something like:

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

    or just

    http://itempus.dev/task/clientsprojects
    

    3) On Chrome, hit F12 to open dev tools

    4) Use this URL to build a command like the one below and paste the dev tools command line (and hit enter):

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

    5) Check the error and also try

    $.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.

    6) Get this same URL and try to paste it in your browser address bar and check if you don't get errors

    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.

    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.

    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.

    EDIT 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.

    EDIT 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

    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:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?