我得到了以下代码:
public function Locate($name){
$locate= Products::where('name', 'like', "$name%") ->get();
return response()->json($locate, 200);
}
我要通过此$ name访问belongsTo并将其放入json
这是我的模特:
class Products extends Model
{
protected $fillable = ['name', 'code', 'price'];
protected $hidden = ['id'];
public function Group()
{
return $this->belongsTo('App\pGroup', 'product_id', 'id');
}
}
答案 0 :(得分:2)
如果您想建立关系:
$locate= Products::where('name', 'like', "$name%")->with('group')->get();