Mysql 如何在laravel查询中使用或在何处
我是拉威尔的新手,我需要在拉威尔的查询中得到帮助 我的自定义查询Mysql 如何在laravel查询中使用或在何处,mysql,sql,laravel,laravel-5.4,Mysql,Sql,Laravel,Laravel 5.4,我是拉威尔的新手,我需要在拉威尔的查询中得到帮助 我的自定义查询 $sql1="SELECT * FROM blogs WHERE ('$title'='' OR title='$title') AND ('$body'='' OR body='$body')"; 我创建了一个laravel构建查询,但不知道如何将其放在WHERE和put括号中 $posts = Blog::where('title','LIKE',"%{$title}%")
$sql1="SELECT * FROM blogs
WHERE
('$title'='' OR title='$title')
AND
('$body'='' OR body='$body')";
我创建了一个laravel构建查询,但不知道如何将其放在WHERE和put括号中
$posts = Blog::where('title','LIKE',"%{$title}%")
->Where('body', 'LIKE',"%{$body}%")
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
我想你要找的东西在哪里?基于自定义查询,我想你需要的是
=
而不是之类的,除非这是你要找的,否则我想应该是这样的:
->where('title', '=', $title)
->orWhere('title', '=', '');
只需使用或where()
希望这能给你一个想法:)
未测试…您的查询应该如下所示:
$posts = Blog::where(function($q) use($title){
$q->where('title','')->orWhere('title',$title);
})->where(function($q) use($body){
$q->where('body','')->orWhere('body',$body);
})->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
$result = Blog::where(function($query) use ($title, $body){
$query->where(
["title" => $title, "body"=>$body]
)->orWhere(
["title"=>"", "body"=>""]
);
})
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
使用->或where()
代码
注意:您还可以使用魔法方法
->whereAgeOrPhone(18, 123456789);
使用orWhere()
如需更多查询,您可以漂亮的答案,但您错过了使用($title)
和使用($body)
谢谢@WahyuKristianto,更新;)
$result = Blog::where(function($query) use ($title, $body){
$query->where(
["title" => $title, "body"=>$body]
)->orWhere(
["title"=>"", "body"=>""]
);
})
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
->whereAgeOrPhone(18, 123456789);
$blogs= DB::table('Blog')
->where('title', 'LIKE', '%{$title}%')
->orWhere('body','LIKE', '%{$body}%')
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();