首页 \ 问答 \ 什么时候在laravel中使用雄辩的模型(When to use an eloquent model in laravel)

什么时候在laravel中使用雄辩的模型(When to use an eloquent model in laravel)

我正在开发一组后端API,将由android / iphone应用程序使用。 有些情况下我必须使用超过2或3个表的连接。

例如:

我有一个与其喜欢,SKU,评论和类别表相关联的产品表。 如果我使用预先加载来获取与products表关联的所有值,那么它将执行以下查询:

select * from `products` where `products`.`products_id` = '1'
select * from `skus` where `skus`.`products_id` in ('1')
select * from `likes` where `likes`.`products_id` in ('1')
select * from `comments` where `comments`.`products_id` in ('1')

这已经运行4个查询与所有'*'。 如果我需要选择特定的列,我将不得不在模型关系函数中对其进行硬编码,它将不是动态的。 这将破坏我的应用程序的性能和可伸缩性。

雄辩的模型在哪里发挥作用? 它是否只是从查询中获得了更快开发和抽象层的优势? 或者是否有任何特定的地方我可以使用雄辩的模型而不影响性能? 我的理解是,如果我有更多的表,我最好使用连接而不是雄辩?


I am developing a set of backend APIs which will be used by an android/iphone app. There are instances where I have to use joins of more than 2 or 3 tables.

For example:

I have a products table that is associated with its likes, SKU's, comments, and category tables. If I am using eager loading to get all the values associated with the products table, it's going to execute the following queries:

select * from `products` where `products`.`products_id` = '1'
select * from `skus` where `skus`.`products_id` in ('1')
select * from `likes` where `likes`.`products_id` in ('1')
select * from `comments` where `comments`.`products_id` in ('1')

This has run 4 queries with all '*'. If I need to have specific columns to be selected, I will have to hardcode it in the model relationship function and it will not be dynamic. This will kill the performance and scalability of my application.

Where does the eloquent model come into play? Does it just give the advantage of faster development and an abstraction layer from the queries? Or are there any specific places where I can use eloquent model without compromising performance? What I understand is, if I have more tables, I better be using joins rather than eloquent?


原文:https://stackoverflow.com/questions/34149995
更新时间:2024-04-19 09:04

最满意答案

Angular自动修剪输入值。 在输入标签上设置ng-trim="false"以禁用默认修剪。

ngTrim

如果设置为false,Angular将不会自动修剪输入。 输入[type = password]控件将忽略此参数,这些控件永远不会修剪输入。

(默认值:true)

angular.module('myapp', [])
  .controller('MainCtrl', function($scope) {
    $scope.foo = {
      txt: ''
    };

    $scope.kd = function(e) {

      if (e.keyCode === 9) {
        var ta = angular.element(e.target);
        var val = ta.val();
        var st = ta[0].selectionStart,
          ed = ta[0].selectionEnd;
        ta.val(val.substring(0, st) + '\t' + val.substring(ed));
        ta[0].selectionStart = ta[0].selectionEnd = st + 1;
        //console.log(ta.triggerHandler);
        ta.triggerHandler('change');
        e.preventDefault();
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
  <div ng-controller="MainCtrl">
    <textarea ng-model="foo.txt" rows="10" cols="75" ng-trim="false" ng-keydown="kd($event)"></textarea>
    <br>
    <br>
    <pre>
      {{ foo | json }}
    </pre>
  </div>
</div>


Angular automatically trims the input value. Set ng-trim="false" on input tag to disable default trimming.

ngTrim

If set to false Angular will not automatically trim the input. This parameter is ignored for input[type=password] controls, which will never trim the input.

(default: true)

angular.module('myapp', [])
  .controller('MainCtrl', function($scope) {
    $scope.foo = {
      txt: ''
    };

    $scope.kd = function(e) {

      if (e.keyCode === 9) {
        var ta = angular.element(e.target);
        var val = ta.val();
        var st = ta[0].selectionStart,
          ed = ta[0].selectionEnd;
        ta.val(val.substring(0, st) + '\t' + val.substring(ed));
        ta[0].selectionStart = ta[0].selectionEnd = st + 1;
        //console.log(ta.triggerHandler);
        ta.triggerHandler('change');
        e.preventDefault();
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
  <div ng-controller="MainCtrl">
    <textarea ng-model="foo.txt" rows="10" cols="75" ng-trim="false" ng-keydown="kd($event)"></textarea>
    <br>
    <br>
    <pre>
      {{ foo | json }}
    </pre>
  </div>
</div>

相关问答

更多
  • Angular自动修剪输入值。 在输入标签上设置ng-trim="false"以禁用默认修剪。 ngTrim 如果设置为false,Angular将不会自动修剪输入。 输入[type = password]控件将忽略此参数,这些控件永远不会修剪输入。 (默认值:true) angular.module('myapp', []) .controller('MainCtrl', function($scope) { $scope.foo = { txt: '' }; ...
  • 一种方法是使用Get-Content的-Encoding参数,例如: Get-Content foo.txt -Encoding byte | % {"0x{0:X2}" -f $_} 如果您有PowerShell社区扩展 ,则可以使用Format-Hex命令: Format-Hex foo.txt Address: 0 1 2 3 4 5 6 7 8 9 A B C D E F ASCII -------- ------------------------------- ...
  • 看起来你有类似的问题。 如果我正确阅读,请尝试: