0

I have written below code

$setting = Setting::where('id', 1)->first();
return view('posts.setting', compact('setting'));

When I use this variable in my index,

<?php $setting = isset($_POST['setting']) ? $_POST['setting'] : 'NULL'; ?>

<?php print_r($setting); ?>

I am getting NULL. Why is my query not generating any output?

7
  • Why do you reassigning $setting in your blade file? Commented May 17, 2019 at 11:28
  • removing that did not help either :) Commented May 17, 2019 at 11:47
  • Can you confirm if you have a row in your setting table with ID as 1 ? Commented May 17, 2019 at 12:42
  • yes I have a record with Id = 1 Commented May 17, 2019 at 14:07
  • ok, to debug further, can you show your model file? Can you do a dd($setting) before view(.....) and check what you get. Commented May 17, 2019 at 14:08

3 Answers 3

1

why are you using $_POST you don't need

@php 
    $setting = isset($setting) ? $setting : 'NULL'; 
    print_r($setting);
@endphp

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

2 Comments

Still, received NULL as a response.
try to print_r($setting); die; in controller. maybe $setting is null. means data for id 1 not exist
0

If a record exists with id 1 then return a view with data otherwise dump and die in the controller just for your testing.

In controller:

     $setting = Setting::find(1);

     if($setting){
         return view('posts.setting', compact('setting'));
     } else {
         dd('No record found!');
     }    

Comments

0

For the benefit of all users, It had seeded database tables. I ran the following command

php artisan db:seed

and it started working. Not sure why it could not fetch manually created record but took it once I ran db seed

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.