Home > Software engineering >  Why is my code showing this error: Undefined Variable
Why is my code showing this error: Undefined Variable

Time:10-04

I am trying to migrate my foreign key and display the list of employees in company form. for that:

1.My CompaniesController

    public function index(){
    $cat = Company::all();
    return view(view:'company/index')->with ('cat', $cat);
}
  1. This my company migration show:

     public function up() {
       Schema::create('company', function (Blueprint $table) {
         $table->id();
         $table->unsignedBigInteger('emp_id');
         $table->foreign('emp_id')->references('id')->on('employeee')->onUpdate('cascade')->onDelete('cascade');
         $table->string('companyname')->nullable();
         $table->string('connumber')->nullable();
         $table->string('addressline1')->nullable();
         $table->string('addressline2')->nullable();
         $table->string('contactnumber')->nullable();
         $table->string('suburb')->nullable();
         $table->string('state')->nullable();
         $table->string('postcode')->nullable();
         $table->string('image');
         $table->timestamps();
     });
    

    }

This is my code for the dropdown menu

    `<div class="col-md-6">
      <select name="">
      @foreach ($cat as $row )
     <option value="{{$row->id}}">{{$row->companyname}}</option>
        @endforeach
               </select>
                    </div>`

This is admin route for the company:

      Route::resource('/admin/companies', 'Admin\CompaniesController',['as'=>'admin']);

`

Please Help me out

CodePudding user response:

    // this is how to send the variable to the view
    public function index(){
       $cat = Company::all();
       return view('company/index', ['cat'=>$cat]);
    }

@foreach ($cat as $row)
   <option value="{{$row->id}}">{{$row->companyname}}</option>
@endforeach

CodePudding user response:

    return view('company/index')->with('cat', $cat);

you can try it. remove view in brackets and remove space with('cat', $cat);.

  • Related