我已经编辑了我的原始问题,因为我已经改进了我的代码,这使我处于更好的位置来定义更好的错误
您好我正在创建一个链式选择框,一旦选择了客户端,就会找到客户项目。
ajax正在完成它的工作,它知道选择了哪个客户端,我的控制台告诉我以下内容:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://itempus.dev/task/clientsprojects?option=5
上面的选项值是指我想要传递给项目数据库并找到客户端项目的客户端ID。我不确定我做错了什么,并希望在一个有点复杂的新手任务中得到一些帮助。
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
我也定义了我的路线:
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
应用程序/存储/日志
[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} [] []
修改后的代码仍然是同样的错误。
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>
答案 0 :(得分:1)
我不确定laravel是如何工作的,但$client_id
方法中未明确create
。尝试将其作为参数传递。
关于javascript:'itempus.dev/task/create' + clients.val()
会产生类似的内容
itempus.dev/task/create1
(假设select中的值是数字)。你创造后想念斜线。也许这就是为什么你不能让路由工作。
干杯
答案 1 :(得分:1)
在您的代码中(阅读有关$project
和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'))); // <----
});
这可能无法解决问题,但提供了更多信息,$project->projects()
是什么以及您要做什么。
答案 2 :(得分:1)
如果您的日志中仍有这些错误:
[2014-03-12 17:01:00] log.ERROR:异常'Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException'在C:\ xampp \ htdocs \ iTempus \ vendor \ laravel \ framework \ src \ Illuminate \路由\ Router.php:1429
这是Laravel说“我无法找到具有这些特征的路线”。
可能你的问题更简单:你的javascript无法打开那条路线,因为,也许,网址格式不正确。
$.get("{{ url('task/clientsprojects')}}", ...
1)打开HTML
2)从上面的行中获取生成的网址可能类似于:
http://itempus.dev/task/clientsprojects?option=7
或只是
http://itempus.dev/task/clientsprojects
3)在Chrome上,按F12打开开发工具
4)使用此URL构建如下所示的命令并粘贴开发工具命令行(并按Enter键):
$.get('http://itempus.dev/task/clientsprojects');
5)检查错误并尝试
$.get('http://itempus.dev/');
你应该得到一些数据,有时候很多,但没有错误。在javascript中处理数据甚至可能是错误的,但此时你不应该得到错误。
6)获取相同的网址,然后尝试将其粘贴到您的浏览器地址栏中,并检查您是否收到错误
如果你没有错误,你的路线很好,否则你将不得不再次检查你的routes.php
文件,不要让javascript(甚至你通过浏览器URL)点击你的路线。
另一种可能性是控制器内部发生异常,但通过手动点击该路线,您也应该能够看到它。但是,再次,如果你仍然有这些NotFoundHttpException错误,可能是路由问题。
确保这些错误是相关的。执行:
php artisan tail
并检查错误是否发生在javascript命中该路径的同一时间。
编辑1
来自评论Trying to get property of non-object
是您真正的错误。您可能正在从表中选择一些记录,并且其中至少有一个未作为对象返回,可能为空。
编辑2
“尝试获取非对象的属性”意味着您正在尝试访问不是对象的变量作为对象。该行负责错误:
$response[$i]['id'] = $project->id;
将您的查询更改为
$projects = Project::where('client_id', $client->id)
->orderBy('project_name')
->get(array('id','project_name'));
应该有助于它的运作。
编辑3
结束:
这可以作为你的路线:
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);
});
这个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>
答案 3 :(得分:0)
好的,让我们试着解决你到目前为止所做的事情。
您的新路线
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); });
和你的剧本
$(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'); }); });
复制粘贴并告诉我它是如何工作的,以及你得到的错误
最后一件事:修复#project_select