Laravel链接/链接选择框无法加载资源状态500内部服务器错误 [英] Laravel chained/linked select box failed to load resource status 500 internal server error

查看:90
本文介绍了Laravel链接/链接选择框无法加载资源状态500内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在完善代码后,我已经编辑了原始问题,使我可以更好地定义更好的错误

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.

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

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

上面的选项值引用了我要传递到项目数据库中并找到客户项目的客户ID.我不确定自己做错了什么,对于新手来说,在一些复杂的任务中会有所帮助,将不胜感激.

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.

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

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

推荐答案

我假定 TaskController 中的 create 函数可以正常工作,并为客户.

I assume that the create function in the TaskController works correctly and creates the first drop down menu for the clients.

当此下拉列表更改值时,Ajax获取请求将发送到服务器,但是您收到 500(内部服务器错误),因为查询有问题.

When this drop down changes value, an ajax get request is sent to the server but you receive a 500 (Internal Server Error) because something is wrong with your queries.

所以让我们尝试解决这个问题.

So lets try to fix that.

Route::get('task/clientsprojects', function(){

    // Get the option value which is the client_id

    $client_id = Input::get('option'); 

    // Get all projects that have client_id = $client_id 

    $projects = DB::table('projects')

    ->where('client_id', $client_id)

    ->orderBy('project_name')

    ->lists('id','project_name');

    //Return the response to the client

    return Response::json($projects);
});

现在,响应将返回给客户端.用以下内容替换您的JavaScript.

Now the response is back to the client. Replace your JavaScript with the following.

$(document).ready(function($){

  $('#select_client').change(function(){

    $.get("{{ url('task/clientsprojects')}}", { option: $(this).val() }, 

      function(data) {

        var projects = $('#project_select');

        projects.empty();

        $.each(data, function(key, value) {   

          projects

          .append($("<option></option>")

          .attr("value",key)

          .text(value)); 
        });

      });

  });

});

你很好.

这篇关于Laravel链接/链接选择框无法加载资源状态500内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆