如何在laravel查询构建器上的同一个表上的单个查询中进行计数和搜索?

时间:2016-07-10 19:21:10

标签: php mysql laravel

<!DOCTYPE html>
<meta charset="utf-8">
<style>

path {
  fill: none;
  stroke: #000;
  stroke-width: 1.5px;
}

line {
  fill: none;
  stroke: red;
  stroke-width: 1.5px;
}

circle {
  fill: red;
}

</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>

var points = [[600,276],[586,393],[378,388],[589,148],[346,227],[365,108]];

var width = 960,
    height = 500;

var line = d3.svg.line()
    .interpolate("cardinal");
    
var drag = d3.behavior.drag()
    .on("drag", dragged);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var path = svg.append("path")
    .datum(points)
    .attr("d", line);

var line = svg.append("line");

var circle = svg.append("circle")
  .attr("transform", "translate(" + points[0] + ")")
  .attr("r", 7)
  .call(drag);
    
svg.append("circle")
  .attr("transform", "translate(" + points[5] + ")")
  .attr("r", 7)
  .call(drag);

function dragged(d) {
  var m = d3.mouse(svg.node()),
    p = closestPoint(path.node(), m);

  d3.select(this)
    .attr("transform", "translate(" + p[0] + "," + p[1] + ")")
}

function closestPoint(pathNode, point) {
  var pathLength = pathNode.getTotalLength(),
      precision = 8,
      best,
      bestLength,
      bestDistance = Infinity;

  // linear scan for coarse approximation
  for (var scan, scanLength = 0, scanDistance; scanLength <= pathLength; scanLength += precision) {
    if ((scanDistance = distance2(scan = pathNode.getPointAtLength(scanLength))) < bestDistance) {
      best = scan, bestLength = scanLength, bestDistance = scanDistance;
    }
  }

  // binary search for precise estimate
  precision /= 2;
  while (precision > 0.5) {
    var before,
        after,
        beforeLength,
        afterLength,
        beforeDistance,
        afterDistance;
    if ((beforeLength = bestLength - precision) >= 0 && (beforeDistance = distance2(before = pathNode.getPointAtLength(beforeLength))) < bestDistance) {
      best = before, bestLength = beforeLength, bestDistance = beforeDistance;
    } else if ((afterLength = bestLength + precision) <= pathLength && (afterDistance = distance2(after = pathNode.getPointAtLength(afterLength))) < bestDistance) {
      best = after, bestLength = afterLength, bestDistance = afterDistance;
    } else {
      precision /= 2;
    }
  }

  best = [best.x, best.y];
  best.distance = Math.sqrt(bestDistance);
  return best;

  function distance2(p) {
    var dx = p.x - point[0],
        dy = p.y - point[1];
    return dx * dx + dy * dy;
  }
}

</script>

在前三行中,我在同一个表上使用两个搜索,一个用于查找quote_id,另一个用于计算同一作者的引用量。但我想只做一个查询。我怎样才能做到这一点 ?

2 个答案:

答案 0 :(得分:0)

会尝试查看是否可行:

这是与作者关系的引用模型:

class Quote extends Model
{
    /**
     * get the associated author for the quote
     */
    public function authors(){
       return $this->hasOne(Author::class);
    }

}

与作者等发现引用:

public function getDeleteQuote($quote_id)
{
    $quote = Quote::findOrFail($quote_id)->with('authors');

    if($quote->authors->count() > 0){
      $author_deleted = $quote->authors->delete();
    }

    $quote->delete();

    return route('home')->with('success' => ($author_deleted) ? 'Quote And Author Deleted !' : 'Quote Deleted !');
}

没有什么可以测试,所以不确定它是否会显示依赖于您的设置等的问题,但查找关系等但希望可能有所帮助。

答案 1 :(得分:0)

假设您的引用模型引用了“引用”表和与“作者”表关联的作者模型,请在引用类中指定关系。

class Quote{

    /*... other code here */

    public function authors(){
        return $this->hasMany('App\Author');
    }
}

然后,您可以将查询构建器存储在调用关系函数的变量中并使用它。

public function getDeleteQuote($quote_id)
{
    //Get The Quote Object
    $quote = Quote::find($quote_id);

    //Get the query builder from relationship and store in a variable
    $quote_query = $quote->authors();

    //Delete authors with condition
    $author_deleted = ($quote_query->count() === 1) ? $quote_query->delete() : false;

    //Delete the Quote
    $quote->delete();

    $msg = $author_deleted ? 'Quote And Author Deleted !' : 'Quote Deleted !' ;
    return redirect()->route('home')->with([
        'success' => $msg 
    ]);
}

P.S。如果关系是一对一集合$this->hasOne('App\Author');