laravel的依赖性下降

时间:2016-06-13 09:01:19

标签: php jquery ajax laravel-5.2

我的laravel项目需要一个依赖的下拉列表,所以我尝试使用下面的代码,它给了我 500内部服务器错误。在第一个选择框中,值显示在数据库中,但是当我选择一个时,第二个选择框不显示任何值。

路线中的

Route::get('/', function () {
$categories = Category::all();

return view('welcome')->with('categories', $categories);
});

Route::get('ajax-subcat/{id}', 'Controller@ajaxcall');

在我的控制器中

<?php

 namespace App\Http\Controllers;

 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Routing\Controller as BaseController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 use Illuminate\Foundation\Auth\Access\AuthorizesResources;

 class Controller extends BaseController
 {
   //use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;

    public function ajaxcall($id){
    $subcategories = Subcategory::where('category_id', '=', $id)->get();
    return Response::json($subcategories);
     }
 }

在模型类别

<?php
 namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Category extends Model
 {

    protected $fillable = ["name"]; 
  }

在模型子类别

<?php

namespace App;



use Illuminate\Database\Eloquent\Model;

class Subcategory extends Model
{
   protected $fillable = ["name", "category_id" ];

}

在视图中:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://fonts.googleapis.com/css?family=Lato:100"        rel="stylesheet" type="text/css">
    <script> src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

   <!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>-->
       </head>

<body>
    <div class="container">

    {{Form::open(array('url'=>' ', 'files'=>true))}}
        <label>Categories</label>
        <select class="form-control input-sm" name="category" id="category">

            @foreach ($categories as $category)
                <option value="{{$category->id}}"> {{$category->name}}     </option>
            @endforeach

        </select>


    <div class="form-group">
    <label>SubCategories</label>
    <select class="form-control input-sm" name="subcategory"    id="subcategory">
            <option value="  "></option>
        </select>
        </div>

        <script>
   $.ajaxSetup({
   headers: {
     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
   }
   });
    $('#category').on('change', function(e){
        console.log(e);

        var category_id = e.target.value;

        //ajax
        $.get('ajax-subcat/' + category_id, function(data){
            console.log(data);

                $('#subcategory').empty();

            $.each(data, function(index, subcatObj){

                $('#subcategory').append('<option value ="'+ subcatObj.id +'">'+subcatObj.name+'</option>');

            });
        });
    });
</script>


        {{Form::close()}}
        </div>



</body>

</html>

1 个答案:

答案 0 :(得分:1)

在控制器中引用您的Subcategory模型

use App\Http\Controllers\Subcategory