Undefined variable:

Published 4 months ago by nic_ttr

Hello! I'm trying to build a website with Laravel. In one of my views I have a table with all distributors, and when I click on the name I want to get a page with all products selled by that distributor. The problem is that the blade with products is not displayed. I get this error: ErrorException in c26a3804cedf0d4134e42beb4acea95e88137cde.php line 3: Undefined variable: distribuitor (View: C:\xampp\htdocs\farmacie\resources\views\preferences.blade.php) So for displaying all distributors I have this blade (about.blade) :

<tbody>
  <?php 
  $i=0;
  ?>
  @foreach($distribuitors as $distribuitor)
  <tr>
  <td>{{++$i}}</td>
  <td>{{ $distribuitor->denumire}}</td>
  <td ><a href="https://{{ $distribuitor->url }}">{{ $distribuitor->url }}></a></td>
  <td>{{$distribuitor->adresa}}</td>
  <td><a href="/preferences/{{$distribuitor->id_distribuitor}}"> Produse distribuite </a> </td>

  </tr>
  @endforeach
 
</tbody>
 

This is the view for distributed products (preference.blade) :

<h1> Produse distribuite de {{ $distribuitor->denumire}} </h1>
<table class="table table-hover table-bordered">
<thead>
  <tr>
  <th >Id </th>
  <th>Denumire</th>
  <th> URL</th>
  <th>Adresa </th>
  
</thead>
<tbody>
  <?php 
  $i=0;
  ?>
  @foreach($distribuitors as $key => $distribuitor)
  <tr>
  <td>{{++$i}}</td>
  <td>{{ $distribuitor->denumire}}</td>
  
  </tr>
  @endforeach
 
</tbody>
</table>

And this is the asociated controller (for both ):

class AboutController extends Controller
{
    public function distribAbout()
    {

        $distribuitors = Distribuitor::all();
        
        return view('about', ['distribuitors' => $distribuitors]);


}

    public function getPreference($id_distribuitor){

        $distribuitors=DB::table('distribuitor')->where('id_distribuitor',$id_distribuitor)->get();

        $prod=DB::table('pret')
            ->join('distribuitor', 'pret.id_distribuitor', '=', 'distribuitor.id_distribuitor')
            ->join('med', 'pret.id_med', '=', 'med.id_med')
            ->select('pret.*', 'distribuitor.id_distribuitor' , 'distribuitor.denumire' , 'distribuitor.url' , 'distribuitor.adresa')
            ->get();


        
        return view('preferences', compact('Distribuitor'));

    }

Did I declare variables in a wrong way? Can you please help me?

Best Answer (As Selected By nic_ttr)
Cronix

Several things wrong in there.

  1. return view('preferences', compact('Distribuitor')); Your variable is $distributor, so it should be compact('distribuitor') (small d)

  2. You are getting a single distributor now, so there is nothing to loop over in the view. Just use $distributor directly like $distributor->denumire

  3. even if there were more than one distributor to loop over, @foreach($distribuitor as $key => $distribuitor) is wrong since you are calling the current loop $distribuitor which is the same as the original object.

  4. Why are you querying $prod=DB::table('pret')... when it's not used at all?

public function getPreference($id_distribuitor)
{
    $distribuitor=DB::table('distribuitor')->where('id_distribuitor',$id_distribuitor)->first();

    return view('preferences', compact('distribuitor')); //small d like your $distribuitor
}
@extends('layouts.app')
@section('content')

 <h1></h1>
 
<hr>
<table class="table table-hover table-bordered">
<thead>
  <tr>
    <th>Denumire</th>
  </tr>   <-- You were missing the /tr
</thead>
<tbody>
  <tr>
    <td>{{ $distribuitor->denumire}}</td>
  </tr>
</tbody>
</table>
@endsection

run php artisan view:clear after making changes.

Snapey
Snapey
4 months ago (897,275 XP)

Its probably not in the table code you are showing.

What is on line 3 of the view.

The view is compiled and stored in storage/framework/views look for the filename mentioned (just look for the first 3 or 4 characters)

My guess is its in the html title element or a page heading.

arthurvillar

Take a look at the getPreference() method. You passed into the view method a compact('Distribuitor') but your variable was called $distribuitors. Just remove the s and change it to $distribuitor and the compact method to compact('distribuitor'). Here are the changes

    public function getPreference($id_distribuitor){

        // Remove the 's' here
        $distribuitor=DB::table('distribuitor')->where('id_distribuitor',$id_distribuitor)->get();

        // Pass the correct value in the compact method here
        return view('preferences', compact('distribuitor'));

    }
nic_ttr

@arthurvillar Hey. Thanks for trying to help..but unfortunatelly nothing has changed

nic_ttr

@Snapey

<?php 
  $i=0;
  ?>
  <?php $__currentLoopData = $distribuitor; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $distribuitor): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
  <tr>
  <td><?php echo e(++$i); ?></td>
  <td><?php echo e($distribuitor->denumire); ?></td>
  

I checked on the mentioned file and that was the mentioned section.... I still can't get it (sorry )

Snapey
Snapey
4 months ago (897,275 XP)

This

$distribuitor=DB::table('distribuitor')->where('id_distribuitor',$id_distribuitor)->get();

returns a collection because it ends in get(). Because it is a collection you cannot access the attributes of the model

Change to

 $distribuitor=DB::table('distribuitor')->where('id_distribuitor',$id_distribuitor)->firstOrFail();

nic_ttr

@Snapey It says that method firstOrFail is undefined : Call to undefined method Illuminate\Database\Query\Builder::firstOrFail() ; (I mention that my version of Laravel is not the newest - Laravel 5.4 )

Cronix
Cronix
4 months ago (482,290 XP)

Ah, it's because you're not using Eloquent. Try just ->first(). You'll have to check whether it found the result manually.

nic_ttr

@Cronix I get the same error as in the beginning (with Undefined variable: distribuitor)...

Cronix
Cronix
4 months ago (482,290 XP)

Did you clear your view cache after making the changes by @arthurvillar ?

nic_ttr

@Cronix I did , but nothing was improved

Cronix
Cronix
4 months ago (482,290 XP)

post your current getPreference() method as well as your /resources/views/preferences.blade.php file (not the cached view, the actual view)

nic_ttr
public function getPreference($id_distribuitor){

        $distribuitor=DB::table('distribuitor')->where('id_distribuitor',$id_distribuitor)->first();

        $prod=DB::table('pret')
            ->join('distribuitor', 'pret.id_distribuitor', '=', 'distribuitor.id_distribuitor')
            ->join('med', 'pret.id_med', '=', 'med.id_med')
            ->select('pret.*', 'distribuitor.id_distribuitor' , 'distribuitor.denumire' , 'distribuitor.url' , 'distribuitor.adresa')
            ->get();


        
        return view('preferences', compact('Distribuitor'));

    }

The view

@extends('layouts.app')
@section('content')

 <h1></h1>
 
<hr>
<table class="table table-hover table-bordered">
<thead>
  <tr>
  <th >Id </th>
  <th>Denumire</th>
  <th> URL</th>
  <th>Adresa </th>
  
</thead>
<tbody>
  <?php 
  $i=0;
  ?>
  @foreach($distribuitor as $key => $distribuitor)
  <tr>
  <td>{{++$i}}</td>
  <td>{{ $distribuitor->denumire}}</td>
  
  </tr>
  @endforeach
 
</tbody>
</table>
@endsection 

```
Cronix
Cronix
4 months ago (482,290 XP)

Several things wrong in there.

  1. return view('preferences', compact('Distribuitor')); Your variable is $distributor, so it should be compact('distribuitor') (small d)

  2. You are getting a single distributor now, so there is nothing to loop over in the view. Just use $distributor directly like $distributor->denumire

  3. even if there were more than one distributor to loop over, @foreach($distribuitor as $key => $distribuitor) is wrong since you are calling the current loop $distribuitor which is the same as the original object.

  4. Why are you querying $prod=DB::table('pret')... when it's not used at all?

public function getPreference($id_distribuitor)
{
    $distribuitor=DB::table('distribuitor')->where('id_distribuitor',$id_distribuitor)->first();

    return view('preferences', compact('distribuitor')); //small d like your $distribuitor
}
@extends('layouts.app')
@section('content')

 <h1></h1>
 
<hr>
<table class="table table-hover table-bordered">
<thead>
  <tr>
    <th>Denumire</th>
  </tr>   <-- You were missing the /tr
</thead>
<tbody>
  <tr>
    <td>{{ $distribuitor->denumire}}</td>
  </tr>
</tbody>
</table>
@endsection

run php artisan view:clear after making changes.

nic_ttr

@Cronix I corrected and now it works . Thanks a lot. The Distribuitor it's from another blade; I just copied and didn't paid attention to all changes .. And the query with 'pret' is from something else that I have in my mind and I want to see if it works or not, so I did't want to delete the query(still working..)

Have a great evening!

Please sign in or create an account to participate in this conversation.