我应该在 Laravel 的什么地方放置复杂的查询?

所以我在互联网上搜索了类似的案例,但我只是从所有相互矛盾的答案和不相关的场景中迷失了方向。所以我想提出我的案例,希望得到一些具体的答案。


我是 Laravel 的新手,正在创建小型应用程序。在应用程序中,我必须搜索要约并在刀片视图中显示结果。由于查询很复杂,并且搜索的输出不属于特定模型,我只想将其保留为原始查询。


我已将查询放在控制器中,但我觉得它不是正确的地方。特别是如果我需要在很多地方重用查询。


这是来自 OfferController 的方法:


public function search(Request $request)

{

    $area =  $request->area;

    $size =  $request->size;


    $sql = "SELECT distinct product_name,product_offer.quantity, product_offer.price

            FROM product

            inner join brand on product.brand_id = brand.brand_id

            inner join brand_area on brand_area.brand_id = brand.brand_id

            inner join area on area.area_id = brand_area.area_id

            inner join product_offer on product_offer.product_id = product.product_id

            where area.area_id = :area

            and  product.size_id = :size ";


    $params = array(

        'area'=>$area,

        'size'=>$size

    );


    $offers = DB::select( DB::raw($sql), $params);


    return view('searchresult')->with($offers);


}

简而言之:我应该将查询移至模型,创建 DAL 类,还是将其保留在这里?请记住,该项目规模较小。


凤凰求蛊
浏览 107回答 3
3回答

Helenr

在我看来,如果您要重用它,请创建一个服务来执行该查询并返回结果,如下所示SearchService:<?phpclass SearchService{&nbsp; public static function perform(array $params){&nbsp; &nbsp; &nbsp;$sql = "SELECT distinct product_name,product_offer.quantity, product_offer.price&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM product&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inner join brand on product.brand_id = brand.brand_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inner join brand_area on brand_area.brand_id = brand.brand_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inner join area on area.area_id = brand_area.area_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inner join product_offer on product_offer.product_id = product.product_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where area.area_id = :area&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and&nbsp; product.size_id = :size ";&nbsp; &nbsp; return DB::select( DB::raw($sql), $params);&nbsp; }}?>通过这样做,你可以打电话SearchService::perform([...]);得到结果。显然这是 version1.0,你可以通过很多方式改进它,例如让它不是静态的以使其可测试,还允许 getter 和 setter 存在,还有很多其他的东西,可能是有用的

慕森卡

你有一个公平的观点,说在控制器中放置查询看起来不正确。此外,我认为您可以使用 LaravelDB进行此类查询,而无需将其编写为原始查询。我认为这里没有必要进行原始查询。Laravel DB table()、select()和where()其他方法应该足够了。实际上,您可以使用模型及其关系来编写此代码,但如果查询速度很慢,最好使用查询构建器来提高效率。编辑:对于一些不属于任何我记得看到使用过的自定义特征的特定查询,也可能是一个解决方案。

一只名叫tom的猫

我正在写我之前的评论作为答案,因为我认为它确实是您正在寻找的东西:我建议你使用一个 Trait 来做到这一点。原因是它会让您的代码保持干净,并且您以后可以重用此代码用于其他用途。Trait 是为可重用代码而创建的。您将创建一个特征:<?phpnamespace App\Traits;    // Optional, but you could create a namespace for all your Traits if you need otherstrait MyCustomTrait{    public function perform(array $params) {         $sql = "SELECT distinct product_name,product_offer.quantity, product_offer.price                FROM product                inner join brand on product.brand_id = brand.brand_id                inner join brand_area on brand_area.brand_id = brand.brand_id                inner join area on area.area_id = brand_area.area_id                inner join product_offer on product_offer.product_id = product.product_id                where area.area_id = :area                and product.size_id = :size ";         return DB::select( DB::raw($sql), $params);    }}use MyCustomTrait要使用它,只需在您的控制器范围内编写:并像这样调用您的函数: $this->perform([...])。
打开App,查看更多内容
随时随地看视频慕课网APP