duanao6704 2018-08-09 21:57
浏览 85

Laravel 5 - 我应该在哪里写这个函数,以及如何编写?

Goodevening,

I have an overview page where I display all my project items.

I have a model (Project.php) a controller (ProjectController) where I send the variable $project (this includes all the information for each project) to the specific view.

Note: Each project has a own row in the database. (Quit obvious I guess)

Now I also have a table 'tasks' related to a specific project. In my view I wanna display how much of the total tasks are 'done'. (This is doing with the column 'done' (true/false, boolean)).

Now because I have a foreach function in my view (to display each individuele project) I can make the function in the view. But the Laravel framework is there for reasons. And writing out a lot of php in a view isn't the right way.

But, where should I make this function? And how can I use it in my foreach (in the view). Ofcourse I can make a new foreach in my model or controller and send that variable to my view. But then I can't use that one in my view-foreach as things will get mixed up.

I don't know how/where I can set up a function like this on a clean way.

Kinds regards,

Dylan

  • 写回答

3条回答 默认 最新

  • doufan3958 2018-08-10 05:21
    关注

    You can create a function in app directory(forexample app/Helpers/), or wherever you want and name it yourHelperFunction.php.

    After creating that file, Laravel won’t recognize the file until it is registered inside composer.json file. Add files array inside autoload section.

    .
    .
    .
    "autoload": {
       "files":[
         "app/Helpers/yourHelperFunction.php"
       ]
    }
    .
    .
    .
    

    then do composer dump autoload and you are ready to use the function inside the blade

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题