douchenzhan3050 2017-07-24 10:59
浏览 117
已采纳

Laravel - 在哪里实现算法功能

I have the following tables:

Address
id,

long,

latt,

address_1

postcode

Jobs

pickup (links to id in Address)

dropoff (links to id in the Address)

Quotes

pickup (links to id in Address)

dropoff (links to id in the Address)

Part of the functionality is to calculate the distance between the pickup and dropoff but the issue is that I'm currently calculating this functionality inside of the controllers for both the Jobs and of course the Quotes which is not very good because if the functionality changes or whatever, then this requires changing it in both ways. I want to be able to re-use the functionality.

The idea is that, if the pickup and dropoff is changed the Jobs table is changed then this would re-calculate the distance between the two locations. So essentially the Jobs and Quotes will be able to access the function calculateDistance($pickup, $dropoff)

Anyone suggest where I could store this so I'm not repeating and re-using code?

  • 写回答

1条回答 默认 最新

  • dongwei1263 2017-07-24 11:03
    关注

    The cleanest way to do this would probably be to create a function on your Address model, distanceTo, that takes another address object as a parameter and returns the distance between the current and the parameter address.

    So then, you can do the following:

    $distance = $job->pickup->distanceTo($job->dropoff);
    

    This way, any Address object will be able to access this function. Even Address links that might be added later on in the project.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集