You spelled subscriptions wrong in the foreach statement (several times)... and...
$month = DateTime::createFromFormat('m', $month)->format('F');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm try to display message for due fee month.Those student who not submitted fee last month then show message in his account please pay fee u have not submitted fee "name of the month.
here is controller
public function fee($reg_no = null)
{
$dt = Carbon::now()->month;
$s = Student::where('reg_no', $reg_no)->with('subscriptions','courses.fees')->first();
$fees = Fee::get();
$messages = array();
foreach ($s->subcriptions as $subcription) {
if ($subcription->tution_fee != null) {
$month = $subcription->month->month();
if ($month == 1) {$month = 'January';}
if ($month == 2) {$month = 'February';}
if ($month == 3) {$month = 'March';}
if ($month == 4) {$month = 'April';}
if ($month == 5) {$month = 'May';}
if ($month == 6) {$month = 'June';}
if ($month == 7) {$month = 'July';}
if ($month == 8) {$month = 'Agust';}
if ($month == 9) {$month = 'September';}
if ($month == 10) {$month = 'October';}
if ($month == 11) {$month = 'Navember';}
if ($month == 12) {$month = 'December';}
$message = 'Your fee are due for '.$month;
array_push($messages, $message);
}
}
return view('fee.fee_form',compact('s','fees','dt','messages'));
}
and here is errors
Whoops, looks like something went wrong.
1/1
ErrorException in SubscriptionController.php line 38:
Invalid argument supplied for foreach()
in SubscriptionController.php line 38
at HandleExceptions->handleError(2, 'Invalid argument supplied for foreach()', 'C:\\laragon\\www\\gpschool\\app\\Http\\Controllers\\SubscriptionController.php', 38, array('reg_no' => 'GRN2017846623', 'dt' => 2, 's' => object(Student), 'fees' => object(Collection), 'messages' => array())) in SubscriptionController.php line 38
at SubscriptionController->fee('GRN2017846623')
at call_user_func_array(array(object(SubscriptionController), 'fee'), array('reg_no' => 'GRN2017846623')) in Controller.php line 55
at Controller->callAction('fee', array('reg_no' => 'GRN2017846623')) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(SubscriptionController), 'fee') in Route.php line 203
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in RevalidateBackHistory.php line 15
at RevalidateBackHistory->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php lin
here is the full discussion http://stackoverflow.com/questions/42119198/how-to-retrieve-due-fee-month-wise-student/42304685?noredirect=1#comment71765168_42304685
Sorry @vipin93 small typo
// change
range(1, 7)
// to
range(1, date('n')) // date('n') will always give you the current month
If your time range spans two years, then you will need to change things slightly.
$subscriptions = $s->subscriptions->keyBy(function ($subscription) {
return $subscription->month->format('F Y');
});
Now, you will also need to handle the fact that the range(1, n) I gave you above is no longer useful; you can set up a DatePeriod to dynamically generate the months:
$start = Carbon\Carbon::parse('September last year'); // or whatever month the academic year starts
$now = Carbon\Carbon::now();
$interval = new DateInterval('P1M');
$months = new DatePeriod($start, $interval, $now);
$messages = collect($months)->map(function ($month) {
return $month->format('F Y');
})->reject(function ($month) use ($subscriptions) {
return $subscriptions->keys()->contains($month) && $subscription[$month]->tuition_fees !== 0;
})->map(function($missingMonth) {
return "Your fees are due for {$missingMonth->format('F')}";
});
Please or to participate in this conversation.