ErrorException未定义的索引:名称

时间:2019-12-20 21:50:33

标签: laravel-6

任何人都可以帮助我,我在运行代码时收到此错误(ErrorException未定义索引:名称),请任何帮助。当我在Region模型中使用hasOne时,它工作正常,但我只想使用hasMany Relationship但收到错误消息(laravel 6.2)

这是我的控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Test;
use App\Models\Region;
use App\Models\Council;
use App\Models\City;

class testController extends Controller
{

public function relationship(){

        $data= Region::all();

        foreach($data as $region){

            echo $region->councils['name']. '</br>';
        }

}

我的城市模型

class City extends Model
{
    //

    protected $table = 'city';
    protected $primaryKey = 'id';

    protected $fillable = [
        'name', 
        'code',
        'region_id',

    ];

    public function councils(){

        return $this->hasMany('App\Models\Council');
    }
    public function region(){

        return $this->belongsTo('App\Models\Region');
    }
}

区域模型

class Region extends Model
{
    //

    protected $table = 'region';
    protected $primaryKey = 'id';

    protected $fillable = [
        'name', 
        'code',


    ];

    public function councils(){

        return $this->hasMany('App\Models\Council');
    }
    public function cities(){

        return $this->hasMany('App\Models\City');
    }
}

我的理事会模型

class Council extends Model
{
    //
    protected $table = 'council';
    protected $primaryKey = 'id';

    protected $fillable = [
        'name', 
        'code',
        'region_id',
        'city_id', 

    ];

    public function region(){

        return $this->belongsTo('App\Models\Region');
    }
    public function city(){

        return $this->BelongsTo('App\Models\city');
    }

}

迁移文件

class Relationship extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

      //
      Schema::connection('mysql')->create('region', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('code')->unsigned();
        $table->string('name');
        // $table->unsignedBigInteger('council_id');
        // $table->foreign('council_id')->references('id')->on('council')->onDelete('cascade');;
        $table->timestamps();
    });

    DB::connection('mysql')->table('region')->insert([
        [

        'code'=> 46,
        'name'=>'Kigoma',

    ],
                ['code'=> 42,
                'name'=>'Kagera',

                 ]]

);


        //
        Schema::connection('mysql')->create('city', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('code')->unsigned();
            $table->string('name');
            $table->unsignedBigInteger('region_id');
            $table->foreign('region_id')->references('id')->on('region')->onDelete('cascade');

            $table->timestamps();
        });

        DB::connection('mysql')->table('city')->insert([

          [  'code'=> 22,
            'name'=>'buhigwe' ,
            'region_id'=>1,
        ],
                    ['code'=> 65,
                    'name'=>'kasulu',
                    'region_id'=>2 ],
        ]
    );



        //
        Schema::connection('mysql')->create('council', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('code')->unsigned();
            $table->string('name');
            $table->unsignedBigInteger('city_id');
            $table->foreign('city_id')->references('id')->on('city')->onDelete('cascade');
            $table->unsignedBigInteger('region_id');
            $table->foreign('region_id')->references('id')->on('region')->onDelete('cascade');
            $table->timestamps();
        });

        DB::connection('mysql')->table('council')->insert([
        [
            'code'=>55 ,
            'name'=>'buhigwedc',
            'city_id'=>1,
            'region_id'=>1,
        ],
                    ['code'=> 54,
                    'name'=>'kasulu Mji',
                    'city_id'=>2,
                    'region_id'=>2,
                     ]
        ]
    );







    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
        Schema::dropIfExists('region');
        Schema::dropIfExists('council');
        Schema::dropIfExists('city');
    }
}

这是我的路线

Route::get('/relationship', 'testController@relationship');

0 个答案:

没有答案