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?
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:
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?