【问题标题】:Issue with a Laravel complex where condition. | Issue with CakePHP join与 Laravel 复合体 where 条件有关的问题。 | CakePHP 加入的问题
【发布时间】:2018-05-21 11:47:52
【问题描述】:

我有两张桌子:

表A,表B。

TableA 有以下列:

id、用户名、密码、类型、TableB_id

TableB 有以下列:

id、类型、some_column、some_other_column

现在我需要根据 TableB 的 id 和 'type' 使用 TableB 的 TableA 的记录。

我试图在我的模型中定义类似的东西

TableA 模型:

$this->belongsTo('TableB', [
    'foreignKey' => 'TableB_id',
]);

TableB 模型:

$this->belongsTo('TableA', [
    'foreignKey' => 'id',
]);

当我打印 SQL 查询时,它会为我提供以下结果:

开启 TableA.id = (TableB.id)

但我需要这个:

开启 TableA.TableB_id = (TableB.id)

================== 问题2 ==================

我正在尝试在 Laravel

中动态获取以下查询

select * from products where (category like '%categoryOne%' OR category like '%categoryTwo%') and (brand like '%brandOne%' OR brand like '%brandTwo ')

我的列在数组中。我试过这样:

$query = DB::table('products');
foreach($all as $category => $subCategory){
    foreach($subCategory as $key => $value){
        $val = '%' .$key .'%';
        $users->orWhere($category, 'like', $val);
    }
}

此查询的问题在于,由于 orWhere,它正在生成具有 OR 的所有条件。

有谁知道如何在 Laravel 中获取复杂的 AND、OR 条件查询?

【问题讨论】:

  • 哦,你需要雇一个程序员。或者至少尝试您自己的代码并在此处发布搜索帮助
  • “我需要 jQuery 代码”。 Stackoverflow 不是免费的代码编写服务
  • @Roy,我是 Stackoverflow 的新手,不知道如何提出一个好问题。现在堆栈已阻止我的帐户提出进一步的问题。我会尝试修改这个问题。让我们看看禁令是否被取消。无论如何感谢您的回复。

标签: laravel-5 join cakephp-3.0


【解决方案1】:

我想这就是你想要做的。请忽略 CSS 问题。

$(document).ready(function(){
$('.classToChangeDisable').hide();
  $('.classToChange').change(function(){
      $('.classToChangeDisable').hide();
      $(this).siblings('.classToChangeDisable').show();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<input type="radio" name="gender" class="classToChange" /> 1st
<br />
<div class="classToChangeDisable">
<input type="radio" /> 1st Sub
</div>
<br />
</td>
<td>
<input type="radio" name="gender" class="classToChange" /> 2st
<br />
<div class="classToChangeDisable">
<input type="radio" /> 2nd Sub
</div>
<br />
</td>
<td>
<input type="radio" name="gender" class="classToChange" /> 3st
<br />
<div class="classToChangeDisable">
<input type="radio" /> 3rd Sub
</div>
<br />
</td>
</tr>
</tbody>
</table>

【讨论】:

  • 子单选按钮应该隐藏而不是禁用。休息是完美的。
  • 检查编辑。忽略 CSS 问题……我无能为力……:P
猜你喜欢
  • 2014-08-11
  • 2020-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
  • 2011-07-07
相关资源
最近更新 更多