dongyanju1094 2018-05-18 13:40
浏览 121
已采纳

在控制器中创建数组并传递给外部函数

I'm trying to take existing/working code from a laravel controller and create an array of customers from an existing query function in that controller.

Here is the controller code portion that works:

CONTROLLER

$d->createdAt = new \DateTime($d->created_at);
$deadline = Carbon::now()->subMonths(2);
if($d->nextDate <= $now && $d->createdAt > $deadline && $d->status != 'exempt'){
    $d->status = 'priority';
}

That block of code works by taking an existing query in the controller, querying the 'created_at' column in the database, and if it's within the last 6 months it applys a status of 'priority' which is used as a css class to highlight.

That works perfectly.

Now, I want to take any affected customer from that query ($d->custNum) with a status of 'priority', put it in an array and then pass the array to my function called findActive in my activeCust.php file

activeCust.php

public function findActive($custNum){

    $sql ="
        SELECT c.customer
          FROM customers C
        WHERE c.customer NOT IN ($custNum)
    ";
}

Basically, after that code block in the controller, I want to create an array of all returned customer numbers (i.e. the status of 'priority') and pass that array to the function so that I can say "Run this findActive query on any customer that isn't in this array".

The idea here is extending the working code into a new file that does the same highlighting but works off of different logic. I'm just not sure the best way to create the array so that I can pass it into the function properly.

How can I create an array from the returned values in the controller code block and use the array for my 'NOT IN' in the function query in activeCust.php?

  • 写回答

1条回答 默认 最新

  • dsn1327 2018-05-18 14:56
    关注

    Because your question is not clear, there's an assumption I have. If it is not correct, I'll delete the answer so you can restructure your question:

    • In that logic, i.e if statement, you want to populate an array that would keep the ids of the $d record (better if you give it more explanatory variable name):

    If my assumption is correct, you can initialize an array, then populate it with other ids in the condition block as this:

    $ids = [];
    
    if($d->nextDate <= $now && $d->createdAt > $deadline && $d->status != 'exempt'){
        $d->status = 'priority';
        $ids[] = $d->custNum
    }
    ....
    SomeClass::findActive($ids); //calls the findActive()
    

    An observation is why you are using Query String when you can use Eloquent or even QueryBuilders' whereNotIn('something', [1,2,3]); for example?

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能