0

I'm trying to pass variable $cMonth to my view. I tried looping it using foreach even if its a single valued data and error still says undefined variable .. I tried dd($cMonth) and the correct output is shown.

This is a snippet of my controller.

$cMonth = date("F", mktime(0, 0, 0, $month, 1));

return view('admin.request',compact('showData','cMonth', 'year'));

In view, I am calling cMonth like this --

{{ ($cMonth) }}
5
  • It's a bit of a guess without the whole context. Are you setting $cMonth inside of an if statement or other loop/conditional? Try sharing your entire controller and view code. Also, you don't need the parentheses around the variable when displaying it. {{ $cMonth }} is just fine. Commented May 29, 2019 at 0:39
  • hello, have you tried reassuring that you have coded the {{ ($cMonth) }} in the proper blade view file? I mean you're trying to access the view admin.request, kindly double check if you have placed that code inside this blade file Commented May 29, 2019 at 1:47
  • Thanks for the reply. Yes I am sure that its in the proper blade file..And I removed the parenthesis in cMonth and the error still shows. No its not in a loop or if statement. I just want to get the value of cMonth.. Commented May 29, 2019 at 2:16
  • @wpd_monk, what is the error message you get when you're trying to display {{ $cMonth }}? Commented May 29, 2019 at 2:23
  • I'm sorry I can't post my view file. But I did get an idea of my error thanks to JueViole17 .. It is pointing to the right blade file. but the index function does not pass $cMonth to my blade because it wasn't declared there .. etc. etc. Thank you for the help. I'm not getting the error anymore.. Commented May 29, 2019 at 3:33

1 Answer 1

0

With this route code

Route::get('/test', function() {
    $month = 3;
    $showData = true;
    $year = 2019;
    $cMonth = date("F", mktime(0, 0, 0, $month, 1));
    return view('test', compact('cMonth', 'showData', 'year'));
});

And this view

{{ $cMonth }}

It works as expected. Have you tried using just that code for the view and see if it works?

You should post your complete view code, because it seems that somewhere/somehow the variable get unset.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.