の柔軟性を得るためにどのように私は私に時間がかかるコーディングする文字列を追加しようとすると、PHPコードはlaravelのSQLクエリ
$datas = $this->model->ADD HERE->orderBy('created_at', 'DESC')->paginate(15);
にSQLフィルタwhere('comment_id', '=', 1)
を追加したいです。それを作る方法?文字列パラメータとしてSQLフィルタを渡す
CommentResource.php:
は、ここに私のコードです。
<?php
class CommentResource extends BaseResource
{
public function index()
{
$filter = "where('comment_id', '=', 1)";
return parent::index_filter($filter);
}
CommentResource.php
<?php
class BaseResource extends Controller
{
protected function index_filter($filter)
{
$datas = $this->model->ADD HERE->orderBy('created_at', 'DESC')->paginate(15);
return view($this->resourceView.'.index')->with('datas', $datas);
}
}
それは動作し、それは私が欲しいものです。ありがとうございました。タイトルをより正確に変更することを検討します。あなたのアドバイスがありますか? –
数日後に、@Andrejの回答を改善しました。要するに、$ filterは配列を入れ子にするために成長し、 '... $ args'は$ argsに変更されます。私はこれらのコードを私の答えに掲示し、私が '... $ args'を変更する理由を説明しようとします。 –