0
votes

I'm trying to get the subcategories on the base of a category in laravel 5.1 but first it was giving me an error of '500 internal server error' but I include

RewriteBase /

in my .htaccess file and nos it is giving me en error of 'not found 404 error'. I'm using a virtual host and Form facade which automatically includes 'csrf token', so I think it's not the problem of 'csrf' token problem. Can any one help in this regard?????

This is my route file:

// Admins Tasks

Route::group(['middleware'=>'admin'], function() {

//Route::resource('/showcategoreis/{$id}', 'AdminHelperContooler@filterSubcat');
Route::get('/ajax-subcat/{id}', function(){
    return "mkj";
   $cat_id = Input::get('cat_id');
   $subcategories = Subcategory::where('category_id', '=', $cat_id)->get();

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

Route::resource('/admin', 'Admin\AdminController');
Route::resource('/user', 'UserController');
Route::resource('/province', 'ProvinceController');
Route::resource('/city', 'CityController');

Route::resource('/category', 'CategoryController');


Route::resource('/subcategory', 'SubcategoryController');
Route::resource('/brand', 'BrandController');
Route::resource('/model', 'BModelController');
});

view.blade file

@extends('admin.layout.master')

@section('title')
Add New Brand
@endsection

@section('content')
<div class="col-md-6 col-md-offset-3">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Add New Brand</h3>
        </div>
        <div class="panel-body">
            {!! Form::open(['files'=>true, 'url' => '']) !!}

                <div class="form-group">
                    {!! Form::label('brand', 'Brand Title') !!}
                    {!! Form::text('brand', null, ['class' => 'form-control']) !!}
                    <small class="text-danger">{{ $errors->first('brand') }}</small>
                </div> {{-- brand --}}

                <div class="form-group">
                     {!! Form::label('category_id', 'Category') !!}
                     {!! Form::select('category_id', $categories, $options = ['id'=>'category_id', 'class' => 'form-control']) !!}
                     <small class="text-danger">{{ $errors->first('category_id') }}</small>
                 </div> {{-- category_id --}}

                 <div class="form-group">
                     {{-- {!! Form::label('subcategory_id', 'Subcategory') !!} --}}
                     {{-- {!! Form::select('subcategory_id', $subcategories, $options = [ 'id'=>'subcategory', 'class' => 'form-control']) !!} --}}
                     {{-- <small class="text-danger">{{ $errors->first('subcategory_id') }}</small> --}}
                     <label for="">Subcategories</label>
                     <select name="subcategory" id="subcategory">
                        <option value=""></option>
                     </select>  
                 </div> {{-- subcategory_id --}}

                <hr>
                <div class="btn-group pull-left">
                    {!! Form::submit('Add', ['class' => 'btn btn-primary', 'style'=>'margin-right:5px']) !!}
                    {!! HTML::link(action('BrandController@index'), 'Cancel', ['class' => 'btn btn-warning']) !!}
                </div>

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

@endsection 

{{-- JS for AJAX --}}

    <script type="text/javascript">
        $(document).ready(function(){
            $('#category_id').on('change', function(e){
                console.log(e);

                var cat_id = e.target.value;

                //Ajax
                $.ajax({
                    type: 'get',
                    // url: 'http://localhost/property-project/public/index.php/ajax-subcat?cat_id'+cat_id,
                    // url: 'http://www.laravelproperty.com/brand/create/ajax-subcat?cat_id='+cat_id,
                    url: '/ajax-subcat?cat_id='+cat_id,
                    scuccess: function(data){
                        console.log(data);
                    }
                });
                // $.get('/ajax-subcat?cat_id='+cat_id, function(data) {
                //  //scuccess data
                //  console.log(data);
                // });
            });
        });
    </script>

.htaccess file

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

virtual host file

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName laravelproperty.com
    ServerAlias www.laravelproperty.com

    DocumentRoot /var/www/property-project/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/property-project/public/>
        AllowOverride All
    </Directory>
</VirtualHost>

I've removed the changes in '.htaccess' and still getting the error and this is the error stack:

Sorry, the page you are looking for could not be found.

1/1
NotFoundHttpException in RouteCollection.php line 143:
in RouteCollection.php line 143
at RouteCollection->match(object(Request)) in Router.php line 746
at Router->findRoute(object(Request)) in Router.php line 655
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
1
I don't think you need to modify .htaccess file. Remove your modification of the .htaccess file, run your code and if you get 500 internal server error, then copy the error stacktrace and paste it here!Ymartin
Hello Ymartin, thanks for the response. As you've suggested I've removed the modification in '.htacecss' and still getting the error, I've still getting the error and I've pasted it in my question at bottom. Can you help me in this regard????MKJ

1 Answers

1
votes

The route you've defined is '/ajax-subcat/{id}' but the you're trying to call '/ajax-subcat?cat_id='+cat_id in your AJAX. Change your route to '/ajax-subcat' and that should at least fix your 404 error.

Alternately, you could change your AJAX to call '/ajax-subcat/'+cat_id