nic_ttr
40
14
Laravel

Undefined variable:

Posted 9 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?

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.