嗨,我对laravel的世界很新,我正在设置一个存储库,因为我正在重复这么多对db的调用,所以将它们保存在一个地方并引用它是有意义的。无论如何,我有一个链式选择,它查看client_id并找到该客户端的匹配项目。我让它在我的routes.php文件中工作如下:
Route::get('task/clientsprojects', function(){
$input = Input::get('option');
$client = Client::find($input);
$projects = $projects = Project::where('client_id', $client->id)
->orderBy('project_name')
->get(array('id','project_name')
);
$response = array();
foreach($projects as $project){
$response[$project->id] = $project->project_name;
}
return Response::json($response);
});
我的create.blade.php文件中有一个:
<!--These two select boxes are linked together --->
@if(count($client_options)>0)
{{ Form::select('client', $client_options, Input::old('client'), array('data-placeholder' => 'Choose a Client...', 'id' => 'select_client', 'class' => 'chosen-select tempus_select', 'tabindex' => '2', )) }}
@endif
{{ Form::select('project', array_merge(array('default' => 'Select client first')), 'default', array('class' => 'tempus_select', 'id' => 'project_select')) }}
<script>
$(document).ready(function($){
$('#select_client').change(function(){
$.get("/task/clientsprojects",{
option: $(this).val()
}, function(data) {
console.log(data);
var model = $('#project_select');
model.empty();
$.each(data, function(key, value) {
$('#project_select').append("<option value='"+key+"'>"+value+"</option>'");
});
$("#project_select").trigger("change");
});
});
});
</script>
我在我的存储库类中创建了这个函数:
//fetch clients
public function getClients()
{
return \Auth::user()->clients()->orderBy('client_name', 'asc')->lists('client_name','id');;
}
//fetch clients projects
public function getClientsProjects() {
$input = Input::get('option');
$client = Client::find($input);
$projects = $projects = \Auth::user()->projects()->where('client_id', $client->id)->orderBy('project_name')->get(array('id','project_name'));
$response = array();
foreach($projects as $project){
$response[$project->id] = $project->project_name;
}
return Response::json($response);
}
我的控制器如下所述引用回购:
<?php
use Acme\Repositories\ProjectRepositoryInterface;
class TaskController extends \BaseController {
public function __construct(ProjectRepositoryInterface $project) {
$this->project = $project;
}
public function create()
{
//
$tasks = Auth::user()->tasks;
$client_options = $this->project->getClients();
return View::make('tasks.create', array( 'client_options' => $client_options, 'status' => $status, 'priority' => $priority));
}
}
我现在如何将此选择路由到此函数以通过ajax检索数据?有谁知道我怎么办?在上面的例子中,我在任务控制器中,我在创建函数中获取我的客户端,在当前实现中,我有routes.php捕获这个并进行查询我想更改它的回购但我不知道如何实现这一点。
我将我的回购更新为以下内容:
public function getClientsProjects() {
$input = Input::get('option'); //line 42
$client = Client::find($input);
$projects = $projects = Project::where('client_id', $client->id)
->orderBy('project_name')
->get(array('id','project_name'));
$response = array();
foreach($projects as $project){
$response[$project->id] = $project->project_name;
}
return $response;
}
并将以下功能插入我的控制器:
public function clientsProjects()
{
return Response::json($this->project->getClientsProjects());
}
这是我的routes.php文件:
Route::get('task/clientsprojects', 'TaskController@clientsProjects');
但我在控制台中收到此错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://tempus.local/task/clientsprojects?option=1
XHR finished loading: GET "http://tempus.local/task/clientsprojects?option=1".
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Laravel Log:
[2014-09-03 20:20:45] production.ERROR: exception
'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class
'Acme\Repositories\Input' not found' in
/media/sf_Sites/tempus/app/Acme/Repositories/DbProjectRepository.php:42
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []
答案 0 :(得分:1)
您必须首先只返回存储库中的相关数据:
public function getClientsProjects($clientId)
{
$client = Client::find($clientId);
...
return $response;
}
然后改变你的路线指向你的控制器:
Route::get('task/clientsprojects', 'TaskController@clientsProjects');
然后在您的控制器中执行:
use Input; /// before your class declaration
...
public function clientsProjects()
{
$clientId = Input::get('option');
return Response::json($this->project->getClientsProjects($clientId));
}