gpt4 book ai didi

php - Laravel 查询不返回任何数据

转载 作者:行者123 更新时间:2023-12-03 00:57:08 27 4
gpt4 key购买 nike

我有一个数据库表“里程”,其中包含 3 列:

start_location 
end_location
miles

我的查询是使用 Mileage 模型中的范围选择器构建的(start_location 和 end_location 已正确传入):

//Get miles for the start_location -> end_location

public function scopeMileageDistance($query)
{
return $query->where('start_location', '=', 'start_location')->where('end_location', '=', 'end_location');
}

//THIS ONE WORKS
//Get a list of the locations from the DB
public function scopeMileageLocations($query)
{
return $query->select('start_location')->groupBy('start_location');
}

我在 Controller 中访问它的方式与访问位置查询的方式相同(有效):

//WORKS
$locations = Mileage::MileageLocations()->get();
//DOES NOT WORK
$distance = Mileage::MileageDistance()->get();

当我倾倒时:

dd($distance);

它显示没有返回任何内容。但是如果我在数据库上运行这个 SQL 查询 - 它会返回适当的对象:

select * from mileages where start_location = 'pointA' and end_location = 'pointB'

我通过 Ajax 调用访问它,当用户进行下拉选择时会触发该调用:

        $.ajax({
url: 'created',
type: 'POST',
data: {_token: CSRF_TOKEN, start_location:start_location.value, end_location:end_location.value},
dataType: 'JSON',
success: function (data) {
console.log(data);
$("#result").append(JSON.stringify(data));
return data;
}
});

只是想知道我的范围选择器和/或查询是否做错了什么。

最佳答案

是的,因为您需要向函数传递一些参数:

public function scopeMileageDistance($query, $start, $end)
{
$query->where('start_location', '=', $start)->where('end_location', '=', $end)->get();
}

所以,你可以将它与这样的东西一起使用:

$start = $request->get($start_location);
$end = $request->get($end_location);
...
->scopeMileageDistance($start, $end);
...

希望对您有所帮助。

关于php - Laravel 查询不返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36084958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com