1

我正在尝试迁移我的外键并以公司形式显示员工列表。为了那个原因:

1.My CompaniesController

    public function index(){
    $cat = Company::all();
    return view(view:'company/index')->with ('cat', $cat);
}
  1. 这是我的公司迁移节目:

     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();
     });
    

    }

这是我的下拉菜单代码

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

这是公司的管理路线:

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

`

请帮帮我

4

2 回答 2

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
于 2021-10-04T03:17:55.443 回答
0
    return view('company/index')->with('cat', $cat);

你可以试试。在括号中删除view并删除空格with('cat', $cat);

于 2021-10-04T04:00:01.447 回答