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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记