Php laravel雄辩->;不是一对一的工作
现在我的产品模型中有了这种方法Php laravel雄辩->;不是一对一的工作,php,laravel,Php,Laravel,现在我的产品模型中有了这种方法 public static function product_lines($request) { $keyword = Helper::clean_keyword($request['keyword']); $data = Product::where ('number','like','%'.$keyword.'%')-> orWhere('name','like','%'.$keyword.'%')-> wi
public static function product_lines($request)
{
$keyword = Helper::clean_keyword($request['keyword']);
$data = Product::where
('number','like','%'.$keyword.'%')->
orWhere('name','like','%'.$keyword.'%')->
with('get_unit_relations')-> // this is one to many
limit(30)->
get();
return $data;
}
这是get\u unit\u关系
public function get_unit_relations()
{
return $this->hasMany('App\Models\Product_unit','product_id','id');
}
像这样,它的工作和返回数据没有任何问题
{
"id": 1,
"number": "1",
"name": "test",
"foreign_name": null,
"get_unit_relations": [
{
"id": 3,
"unit_id": 2,
"product_id": 1,
"barcode": "455",
"smallest_unit_relation": 12,
"price": 12,
"cost": null,
"created_by": null,
"deleted_by": null,
"created_at": "2020-05-17T21:46:26.000000Z",
"updated_at": "2020-05-17T21:46:26.000000Z",
"deleted_at": null
}
]
}
但是如果我像这样修改代码
public static function product_lines($request)
{
$keyword = Helper::clean_keyword($request['keyword']);
$data = Product::where
('number','like','%'.$keyword.'%')->
orWhere('name','like','%'.$keyword.'%')->
with('get_smallest_unit_id')-> // this is one to one
limit(30)->
get();
return $data;
}
{
"id": 1,
"number": "1",
"name": "test",
"foreign_name": null,
"get_smallest_unit_id": null // it always return null in any one to one relaiton ..
}
这是最小的单位id
public function get_smallest_unit_id()
{
return $this->hasOne('App\Models\Unit','id','smallest_unit_id');
}
我得到的回报是这样的
public static function product_lines($request)
{
$keyword = Helper::clean_keyword($request['keyword']);
$data = Product::where
('number','like','%'.$keyword.'%')->
orWhere('name','like','%'.$keyword.'%')->
with('get_smallest_unit_id')-> // this is one to one
limit(30)->
get();
return $data;
}
{
"id": 1,
"number": "1",
"name": "test",
"foreign_name": null,
"get_smallest_unit_id": null // it always return null in any one to one relaiton ..
}
如何使用带有一对一关系的->with()
方法
谢谢能否在问题中添加产品和单位的表格结构。将单位链接到产品的列名是什么?名称编号所有名称您可以在问题中添加产品和单位的表格结构吗。将单元链接到产品的列名是什么?所有单元的名称和编号