laravel 一对多在一侧返回 null
Posted
技术标签:
【中文标题】laravel 一对多在一侧返回 null【英文标题】:laravel one to many return null on one side 【发布时间】:2014-11-28 15:44:21 【问题描述】:我有一个一对多的关系,在一侧返回值,在另一侧返回 null!
class Particular extends Eloquent
public function calls()
return $this->hasMany('Call');
这个关系传递返回值
[
"id": 1,
"date": "2014-10-16",
"time": "12:12:12",
"quote_id": 1,
"repairman_id": 17,
"particular_id": 2,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00"
,
"id": 2,
"date": "2014-10-21",
"time": "12:12:12",
"quote_id": 2,
"repairman_id": 30,
"particular_id": 2,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00"
]
但是当我在另一边尝试时
class Call extends Eloquent
public function particularCall()
return $this->belongsTo('Particular');
它什么也不返回???
【问题讨论】:
你怎么称呼那个什么都不返回? Route::get('test', function() $Call = Call::find(2); return $Call->particularCall; ); 【参考方案1】:您应该尝试:
Route::get('test', function()
$Call = Call::find(2);
return $Call->particularCall();
);
或
Route::get('test', function()
$Call = Call::with('particularCall')->find(2);
return $Call->particularCall;
);
【讨论】:
以上是关于laravel 一对多在一侧返回 null的主要内容,如果未能解决你的问题,请参考以下文章