简体   繁体   English

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

[英]Laravel 5 - Where should I write this function, and how?

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. 我有一个模型(Project.php)和一个控制器(ProjectController),在该模型中,我将变量$ project (包括每个项目的所有信息)发送到特定视图。

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. 现在,因为我的视图中有一个foreach函数(以显示每个单独的项目),所以可以在视图中创建该函数。 But the Laravel framework is there for reasons. 但是Laravel框架之所以存在是有原因的。 And writing out a lot of php in a view isn't the right way. 而且在视图中写出很多php并不是正确的方法。

But, where should I make this function? 但是,我应该在哪里执行此功能? And how can I use it in my foreach (in the view). 以及如何在foreach中使用它(在视图中)。 Ofcourse I can make a new foreach in my model or controller and send that variable to my view. 当然,我可以在模型或控制器中创建一个新的foreach并将该变量发送到我的视图中。 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 迪伦

You can create a function in app directory(forexample app/Helpers/), or wherever you want and name it yourHelperFunction.php. 您可以在应用程序目录(例如app / Helpers /)中或任何需要的位置创建函数,并将其命名为yourHelperFunction.php。

After creating that file, Laravel won't recognize the file until it is registered inside composer.json file. 创建该文件后,Laravel将无法识别该文件,除非该文件已在composer.json文件中注册。 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 然后做作曲家转储自动加载,您就可以使用刀片内部的功能了

Use Blade engine, not Core PHP loops. 使用Blade引擎,而不是Core PHP循环。 It is wise to use blade engine then core php codes. 明智的做法是先使用刀片引擎,再使用核心php代码。

You can see the documentation Laravel 5 Blade Template 您可以查看文档Laravel 5 Blade Template

Thanks for the help. 谢谢您的帮助。 I've figured it out (I guess?). 我已经弄清楚了(我猜是吗?)。

I have this function in my Project Model. 我的项目模型中有此功能。

    public function getTaskDonePercentage($id) {
    $tasks = Task::where('project_id', $id)->count();

    $tasks_done = Task::where([
      ['project_id', $id],
      ['status', 'done']
    ])->count();

    if ($tasks_done > 0) {
      $calc = ($tasks_done / $tasks) * 100;
    }
    else {
      $calc = '100';
    }

    return round($calc) . '%';
  }

Then in my view I just call the function in the foreach like: 然后在我看来,我只是在foreach中调用该函数,例如:

@foreach
{{ $project_item->getTaskDonePercentage($project_item->id) }}
@endforeach

Tbh I still wanna know if this approach is a good one. 我仍然想知道这种方法是否是一种好的方法。 (Or, if not, why it isnt?). (或者,如果不是,为什么不是呢?)。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM