这是已经在mysql数据库中运行的代码,但我想将其转换为 Laravel 5.6
SELECT *
FROM `listings`
WHERE (
country_id=1
AND (state=32 or city=8)
AND (listing_id LIKE "%6562%" OR title LIKE "%6562%"))
答案 0 :(得分:0)
假设您有一个名为autoIncrement.initialize(mongoose);
var districtSchema=new Schema({
_id : {type: Schema.Types.Number, unique: true},
district_name : {type: String, unique: true},
cities : [{type: Schema.Types.Number, ref:'City'}]
},
{timestamps:{createdAt:'created_at',updatedAt:'updated_at'}});
districtSchema.plugin(autoIncrement.plugin, {model:'District',field:'_id',startAt:100,incrementBy:1});
var District=mongoose.model('District',districtSchema);
的模型,该模型负责处理Listing
表。您可以这样编写查询:
listings
$listings = App\Listing::where('field1', 1)
->where(function ($query) {
$query->where('field2', 32);
$query->orWhere('field3', 8);
})
->where(function ($query) {
$query->where('field4', 'LIKE', '%6562%');
$query->orWhere('field5', 'LIKE', '%6562%');
})
->get();
方法的第一个参数可以是可以实现这种分组方式(field2 = 32或field3 = 8)的回调