如何在laravel 5

时间:2016-11-08 05:33:46

标签: php mysql laravel laravel-5 laravel-5.1

我在laravel 5.1中编写此查询时遇到了困难,

select * from `host_requests` where `host_id` = 5 and (`email` LIKE "%v%" or `request_url` LIKE "%v%")

此查询的目的是获取与host_id = 5关联的记录,如果设置了电子邮件,则request_url是可选的,则应使用LIKE子句。 我在这样的laravel中写了这个查询

$searchname = "v";
$hostid = "5";
$data = HostRequest::where('email', 'LIKE', '%' . $searchname . '%')
                ->where('host_id', $hostid)
                ->orwhere('request_url', 'LIKE', '%' . $searchname . '%')
                ->latest('id')
                ->paginate($size);

它正在获取与host_id = 5无关的所有记录。

非常感谢任何帮助。谢谢

2 个答案:

答案 0 :(得分:4)

您应该使用where()这样的闭包:

$data = HostRequest::where('host_id', $hostid)
               ->where(function($q) use($searchname) {
                   $q->where('email', 'like', '%'.$searchname.'%')
                     ->orWhere('request_url', 'like', '%'.$searchname.'%');
               })->orderBy('id', 'desc')
               ->paginate($size);

答案 1 :(得分:0)

试试这段代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="contact" status="closed">
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-12">
        <div class="contact-nav"></div>
        <div class="contact-form-trigger open" id="test">
          <img src="<?php echo base_url('assets/images/dugh1.png')?>" class="doug img-resfponsive hidden-xs" alt="Doug the man!">
        </div>
      </div>
    </div>
  </div>
  <div class="color-div"></div>
  <div class="contact-form " style="display: none;">

    <div class="container-fluid">
      <div class="row">
        <div class="col-md-6">
          <div class="address">
            <div class="address-div">
              <h3>Address</h3>
              <h5><?php echo $company[1]->content; ?>
                            </h5>
              <h5>Phone: <?php echo $company[2]->content; ?></h5>
              <a href="#">
                <?php echo $company[3]->content; ?></a>
            </div>
            <div class="social-icons">
              <h4>follow us</h4>
              <ul class="list-inline">
                <li><a class="social-icon" href="<?php echo $company[7]->content; ?>"><i class="fa fa-facebook"></i></a>
                </li>
                <li><a class="social-icon" href="<?php echo $company[8]->content; ?>"><i class="fa fa-twitter"></i></a>
                </li>
                <li><a class="social-icon" href="<?php echo $company[9]->content; ?>"><i class="fa fa-linkedin"></i></a>
                </li>
                <li><a class="social-icon" href="<?php echo $company[10]->content; ?>"><i class="fa fa-dribbble"></i></a>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>