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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?