1

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

  • Please show a screenshot of the full error page and try the answer down – Mostafa Hana Oct 04 '21 at 03:27
  • 2
    @MostafaHana [please do not encourage ppl to post images of code/error messages](https://meta.stackoverflow.com/a/285557). – Don't Panic Oct 04 '21 at 08:05
  • Does this answer your question? [How to pass data to view in Laravel?](https://stackoverflow.com/questions/18341792/how-to-pass-data-to-view-in-laravel) – miken32 Nov 12 '21 at 20:35

2 Answers2

0
    // 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
Mostafa Hana
  • 115
  • 2
  • 8
0
    return view('company/index')->with('cat', $cat);

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

IQBAL AHMED
  • 61
  • 1
  • 5
  • thank you so much but can you also help me with foreign key. is the code in migration section right? i'm very confues – Barshu Mhzn Oct 04 '21 at 04:03
  • In company model you can use relation like *** public function employees(){ return $this->hasMany(Employee::class, 'id', 'employee_id'); } then write in controller like $cat = Company::with('employees')->get(); return view('company/index')->with('cat', $cat); *** – IQBAL AHMED Oct 04 '21 at 04:15
  • In company model you can use relation like `public function employees(){ return $this->hasMany(Employee::class, 'id', 'employee_id'); }` then write in controller like `$cat = Company::with('employees')->get(); return view('company/index')->with('cat', $cat);` – Mostafa Hana Oct 04 '21 at 04:42
  • @BarshuMhzn Your migration is fine. What I don't understand is why you have your company name column as `nullable`. You are displaying company names on your drop down menu so if you have it as `nullable` then you'll end up displaying blank options. – Alphy Gacheru Oct 04 '21 at 23:49