Hello again.
You did a great job.
Your mistake is in routes.php. Only 'pretty' urls have the expected parameters in their definition. The parameters after '?' are not listed.
Route::get('/getStreets', function() {
return Street::whereSuburb(Input::get('suburb'))->get();
});
I use jQuery.ajax instead of get.
Hope this helps.
Thanks Firtzberg,
That gets rid of my console error, when I modify routes.php. But when I edit my view to below I don't get it injecting any <option> into the form.
<div class="form-group">
{{ Form::label('street_names', 'Street:') }}
</div>```
I altered Street.php
use Illuminate\Support\Facades\DB; public static function whereSuburb($id){ return DB::table('streets') ->select('streetName', 'id') ->where('suburbsId', '=', $id) ->get(); }
and can ```dd(whereSuburb(1));``` from my controller and get the results that I'm expecting
I've altered routes.php from
Route::get('/getStreets', function($id) { return Street::whereSuburb($id); });
to
Route::get('/getStreets', function($id) { dd(Street::whereSuburb($id)); });
to test and see if I can get a dump of the results but to no avail. I'm not sure how to fault find what is happening, or why this is not working at present.
As an aside. As I'm a beginner should try to use jQuery.ajax instead too? Why do you use that? Is it because it can perform asynchronous HTTP (Ajax) requests that you use it instead? I would prefer to learn what others are doing as it may save me a long learning curve.
How would I replace my jQuery with jQuery.ajax as you said you would do?
A little bit of reading and researching to use jQuery.ajax would I do something along these lines?
<label>
<select name="suburb" id="suburb">
<option value="">Please Select</option>
@foreach ($suburbs as $suburb)
<option value="{{ $suburb->id }}">{{ $suburb->suburbName }}</option>
@endforeach
</select>
</label>
<div class="cascade" id="street"></div>
<script>
$(document).ready(function(){
$("select#suburb").change(function(){
var suburb_id = $("select#suburb option:selected").attr('value');
$("#street").html("");
if (suburb_id.length > 0) {
$.ajax({
type: "POST",
url: "/getStreets",
data: "suburb_id="+suburb_id,
cache: false,
success: function(html) {
$("#state").html( html );
}
});
}
})
});
</script>
Hello
I'm experienced in database management, so what I tell you is my personal practice. There are much better solutions and practices.
Ajax is more flexible than $.get() or $.post(). That's all. As far as I know they are just simplifications of $.ajax(). I like it beause I can easily change the method without worrying that some of the additional options is not suported in $.get() or $.post().
Inside your ajax this is wrong
data: "suburb_id="+suburb_id,
change it to
data: {suburb_id: suburb_id},
you can add other things to data.
data: {
suburb_id: suburb_id,
otherstuff: "other"
},
You are not passing any id to the route. You are passing a suburb_id. Check theese out.
Route::get('/getStreets', function() { return Input::all(); });
Route::get('/getStreets1', function($suburb_id) { return $id; });
Route::get('/getStreets2', function() { return $suburb_id; });
Route::get('/getStreets3', function() { return Input::get('suburb_id'); });
Route::get('/getStreets3', function() { return Street::whereSuburb(Input::get('suburb_id')); });
If the 'variable' is not in the pretty url, you can't pass it to a closure. You have to access it with Input::get()
Make your own experiments.
I always make ajay use POST requests. Only during implementation I define them as GET to have the ability to check the output
Thanks once again for your reply Firtzberg, that will give me something else to be able to at least try and fault find what's happening and where is the problem occurring. I've found it a little trickier as I don't know what is happening once the jQuery has executed. I'll give it a play around now.
Appreciate your help.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community