我目前有一个令人震惊的架构,我已经继承了,由于雄辩不能过滤联合模型,我需要对返回的模型+相关模型应用一些过滤。基本上我有一个电影对象,其中包含电影当前显示的商场集合。我需要过滤那个" endDate"是> =今天。无论我如何尝试这样做,模型似乎永远不会改变。我可以使用array_filter成功过滤集合,但我无法以任何方式将其分配回模型?我将整个模型作为Web服务中的json对象返回(Response :: json ...)
我只是想知道这是否可能,或者我是否应该吠叫另一棵树?以下是返回的json数据的示例
{
"error": false,
"message": "Success",
"data": {
"movieID": "####",
"name": "The Grand Seduction",
"description": "The residents of a once-thriving Newfoundland coastal town are having trouble finding a way to make a living since the collapse of the fishing industry. They're thrilled when a plastics manufacturer proposes to set up shop until they find out the contract requires a resident doctor. \r\n\r\nThe villagers decide to woo Dr. Lewis (Taylor Kitsch), an ethically suspect cosmetic surgeon temporarily banished to the physician-starved seaside. Without revealing their plan, they take up the doctor's beloved sport of cricket, falling all over themselves in an effort to persuade him that their sleepy hamlet is loaded with cosmopolitan sophistication.",
"smallImage": "grand_seduction_small.jpg",
"length": "TBA",
"cast": "Anna Hopkins,BRENDAN GLEESON,Gordon Pinsent,Liane Balaban,Mark Critch,Matt Watts,Taylor Kitsch",
"director": "Don McKellar",
"producer": "Barbara Doran,Roger Frappier",
"ageRestriction": "TBA",
"genre": "Comedy",
"linkToSite": "",
"display": "Y",
"bigImage": "grand_seduction_large.jpg",
"threeD": "N",
"dateAdded": "2014-09-16 23:59:59",
"largeImage": "",
"YouTubeID": "",
"whatsshowing": [
{
"whatsShowingID": "####",
"mallID": "###",
"startDate": "2014-12-06 00:00:00",
"endDate": "2014-12-06 23:59:59",
"pivot": {
"movieID": "####",
"whatsShowingID": "#####",
"times": "09:00; 11:30; 14:00; 22:45; ",
"featured": "",
"finalWeek": "N"
}
},
{
"whatsShowingID": "####",
"mallID": "###",
"startDate": "2014-11-29 00:00:00",
"endDate": "2014-11-29 23:59:59",
"pivot": {
"movieID": "####",
"whatsShowingID": "####",
"times": "09:00; 14:15; 19:30; ",
"featured": "",
"finalWeek": "N"
}
},...........
正如你所看到的,我回来了一部电影,然后收集一些" whatsshowing"对象,并希望删除任何endDate比今天少的人。
这是一段控制器代码,用于解释我目前正在做什么
$query = MovieModel::query();
$query = $query->with('whatsshowing');
$data = $query->findOrFail($id);
//filter out what I dont want
$new = array_filter($data->whatsshowing->toArray(), function($obj){
if (isset($obj["endDate"])) {
if ($obj["endDate"] >= '2014-12-14')
return true;
}
return false;
});
//assign the data back to the object
data->whatsshowing = $new;
//?????? nothing changes??
该财产是否受到保护?我还没准备好把这台电脑扔到墙上......
请记住,我是一名C#开发人员,总共有4个月的PHP经验,所以请保持温和......
答案 0 :(得分:2)
雄辩不能过滤联合模型 - 这基本上是错误的。
MovieModel::with(['whatsshowing' => function ($q) {
$q->where('endDate', '>=', DB::raw('curdate()'));
}])->findOrFail($id);
显然DB::raw('curdate()')
件可以用例如替换。 Carbon::today()
,date('Y-m-d')
等等。
答案 1 :(得分:0)
$data->setRelation('whatsshowing', $new);
但是$new
需要是一个集合,所以这样做:
$new = $data->whatsshowing->filter(function($obj){
if($obj->endDate >= '2014-12-14'){
return true;
}
return false;
});
$data->setRelation('whatsshowing', $new);
问题是,结果关系存储在模型上的relations
数组中(而不仅仅是属性)。当您致电Response::json
(在模型上生成->toJson()
)时,Laravel会忽略您添加的属性并使用relations[]
中的值。
setRelation()
的作用是什么?它只是将值存储在relations
数组中。