Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JOHNMAC's avatar

Undefined variable: data

hello m working in a project in which an admin and user have their own home pages, admin adds the files and assigns the user id to the file, I wants that when user login then those file in which his id assigned he get that file or that file shown to him ,,, but in my code it shows error that: Undefined variable: data (View: D:\xampp\htdocs\laravel\webpro\resources\views\home.blade.php) ,,,, code of homecontroller:

  class HomeController extends Controller
  {
   public function __construct()
   {
      $this->middleware('auth');
   }

   public function index()
   {
     return view('home');
   }

  public function show($id)
  {
    $data = File::findOrFail($id);

    $files = \DB::table('files')->get();

    return view('home', compact('data', 'files'));
  }

and this is home.blade.php(where to show the file):

  <div class="container">
    <div class="row justify-content-center">
     <div class="col-md-8">
       <div class="card">
        <div class="card-header">User's View</div>

        <div class="card-body">
            @if (session('status'))
                <div class="alert alert-success" role="alert">
                    {{ session('status') }}
                </div>
            @endif

            You are logged in!
         </div>
        <!---->
              <div class="form-group">
          <label for="ShowFile">File</label>
          <input type="file" name="file" class="form-control-file">
          <img src="{{ URL::to('/') }}/files/{{ $data->file }}" width="100" />
          <input type="hidden" name="hidden_file" value="{{ $data->file }}" />
        </div>
            <!---->
        </div>
      </div>
    </div>
  </div>

how to resolve it?

0 likes
6 replies
bobbybouwmann's avatar

The problem is that you use the same view for both the index and show method. However you don't pass any $data to the view in the index method and therefore you get an error

So the solution is simple. Either use data in both methods or use different views ;)

JOHNMAC's avatar

@BOBBYBOUWMANN - I tried to use data in both methods but still same error

  public function index()
  {
    $data = File::findOrFail($id);
    return view('home');
  }
bobbybouwmann's avatar

You need to pass it to the view, otherwise the view doesn't know about that variable!

public function index()
{
    $data = File::findOrFail($id);

    return view('home', compact('data'));
}
jlrdw's avatar

Why not take Jeffrey's free video training, it would clear up this stuff.

I.e., do you have a route set up.

But those videos would prevent "pages" of Q and A on such simple stuff.

still same error bro

Did you clear temp views, did you clear browser cache after making a program change, etc.

Snapey's avatar
Snapey
Best Answer
Level 122

Use two views.

or

pass null $data to the view from the index controller method

controller;

   public function index()
   {
     return view('home')->withData(null);
   }

home.blade.php

@if($data)

              <div class="form-group">
          <label for="ShowFile">File</label>
          <input type="file" name="file" class="form-control-file">
          <img src="{{ URL::to('/') }}/files/{{ $data->file }}" width="100" />
          <input type="hidden" name="hidden_file" value="{{ $data->file }}" />
        </div>

@endif

Please or to participate in this conversation.