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.
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')));
}
{{ 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')));
});
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} [] []
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>
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>
I'm not sure how laravel works but obviusly $client_id
is undefined in create
method. Try passing it as parameter.
About javascript: 'itempus.dev/task/create' + clients.val()
will produce something like itempus.dev/task/create1
(assuming value in select is numeric). You miss slash after create. Maybe that's why you can't get routing works.
Cheers
OK, let's try to fix what you have so far.
Your new 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(array()); $i = 0; foreach($projects as $project){ $response[$i]['id'] = $project->id; $response[$i]['name'] = $project->name; $i++; } return json_encode($response); });
And your script
$(document).ready(function($){ $('#select_client').change(function(){ $.get("{{ URL::to('/task/clientsprojects')}}",{ option: $(this).val() }, function(data) { var model = $('#project_select'); model.empty(); $.each(data, function() { model.append('' + data.name + ''); }); }, 'json'); }); });
Copy-paste and tell me how it works, and the errors you get
One last thing: fix the label for your #project_select
In your code (read the comments about $project
and Response::eloquent()
):
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');
// You don't have $project variable in the scope, instead it $projects
$models = $project->projects(); // <------
// I don't know about any method available as eloquent();
return Response::eloquent($models->get(array('id','project_name'))); // <----
});
This may doesn't solve the problem but provide more information, what is $project->projects()
for and what you are trying to do.