0 レビュー
1 回答
php-laravelクエリ関数をルートからリポジトリに移動
こんにちは私はlaravelの世界にかなり慣れていません。また、dbへの呼び出しを何度も繰り返しているため、リポジトリを設定しているので、すべてを1か所にまとめて参照するのが理にかなっています。とにかく、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));
}
}
このselectをこの関数にルーティングして、ajax経由でデータを取得するにはどうすればよいですか?誰かが私がどのように進むことができるか知っていますか?上記の例では、私はタスクコントローラーにいて、create関数でクライアントをフェッチしています。現在の実装では、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', '[email protected]');
しかし、コンソールでこのエラーが発生します:
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ログ:
[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
レビュー
答え :
解決策:
最初に、リポジトリから関連データのみを返す必要があります:
public function getClientsProjects($clientId)
{
$client = Client::find($clientId);
...
return $response;
}
次に、コントローラーを指すようにルートを変更します。
Route::get('task/clientsprojects', '[email protected]');
次に、コントローラーで次のことを行います:
use Input; /// before your class declaration
...
public function clientsProjects()
{
$clientId = Input::get('option');
return Response::json($this->project->getClientsProjects($clientId));
}
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。