【问题标题】:How to merge all functions into a single function?如何将所有功能合并为一个功能?
【发布时间】:2021-09-20 06:05:35
【问题描述】:

我想将所有三个函数合并为一个搜索函数。如果用户使用名称搜索,它应该只调用该名称字段行,并且应该禁用按作者搜索和价格功能等其余内容。我正在分别编写所有三个函数和三个不同的 API 路由。现在我想要一个 API 路由,如果用户按姓名、作者或价格搜索,并且所有三个函数都在一个函数中。

文件控制器.php

class FileController extends Controller
{
    public function searchBooksByAuthor($author)
    {
        $books = Books::all();
        if (User::find($books->user_id = auth()->id())->books) {
            $searchBooks = Books::where("author", "like", "%".$author."%")->get();
        }
    }

    public function searchbooks($name)
    {
        $books = Books::all();
        if (User::find($books->user_id = auth()->id())->books) {
            return Books::where("name", "like", "%".$name."%")->get();
        }
    }

    public function searchBooksbyPrice($price)
    {
        $books = Books::all();
        if (User::find($books->user_id = auth()->id())->books) {
            return Books::where("price", $price)->get();
        }
    }
}

api.php

Route::group(["middleware" => ['auth.jwt']], function () {
    Route::get('searchBooksbyName/{name}', [FileController::class, 'searchbooks']);
    Route::get('searchBookbyAuthor/{author}', 
        [FileController::class, 'searchBooksByAuthor']);
    Route::get('searchBookbyprice/{price}', 
        [FileController::class, 'searchBooksbyPrice']);
});

【问题讨论】:

  • 这很简单..用查询字符串创建一个搜索路径。然后按他们搜索..你在这里有什么困惑??

标签: laravel laravel-8


【解决方案1】:
Route::middleware('auth.jwt')->prefix('searchBooksby')->group(function (){
  Route::get('{name}',[FileController::class,'searchbooks']);
  Route::get('{author}',[FileController::class,'searchBooksByAuthor']);
  Route::get('{price}',[FileController::class,'searchBooksbyPrice']);
});

以上就是您要查找的内容。然而,有更通用的方法来解决这个问题,而无需为每种情况使用函数。看看下面。只需调用一个 api 并传递两个参数。首先是 filterBy,它是列的名称。在我们的例子中 nameauthorprice ,第二个参数是要过滤的值。

Route::middleware('auth.jwt')->prefix('searchBooksby')->group(function (){
    Route::get('{searchBy}/{searchByValue}'[FileController::class,'searchbooks']);
});

现在只有一个函数可以处理所有这些逻辑。

public function searchBooks(Request $request , $searchBy, $searchByValue){
    /*
     * Prepare your query without searchBy Parameter

     */
    $db = DB::table('books');

    /*
     * searchBy parameter should be the same as the name of column (more generic)
     * e.g
     * search by book name then column name in database table should be name
     * search by value should be the corrosponding value of that column
     */

    $whereClause = [];
    $whereClause [$searchBy] = $searchByValue;
    /*
     * [name => 'abc']
     * [author => 'xyz']
     * ['price' => 1234]
     */

    /*
     * Now you can filter in more generic way without using conditions and seperate functions
     */

    $results = $db->where($whereClause)->get();
    return response()->json($results);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多