简体   繁体   中英

ERROR : how to get information to foreign key laravel 8

I cannot retrieve the username from the foreign key, please help me. The user table contains a name field I think with the foreign key I could retrieve that. But it does not work.

My migration table

public function up()
{
    Schema::create('bookings', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');
        $table->integer('doctor_id');
        $table->unsignedBigInteger('campagne_id');
        $table->string('time');
        $table->integer('status')->default(0);
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('campagne_id')->references('id')->on('campagne')->onDelete('cascade');
    });
}

My booking Model

public function utilisateur()
{
    return $this->belongsTo(User::class, 'user_id', 'id');
}
public function Campagne()
{
    return $this->belongsTo(Campagne::class, 'campagne_id', 'id');
}

}

my controller

public function liste()
{
    $bookings = DB::table('bookings')->paginate(6);
    return view('listeAppointement', compact('bookings', 'doctors'));
}

My erreur enter image description here i dont know happen. Thanks for advance.

Your setup is good to go, Did you verified that all reference 'user_id' from table bookings exist on users table? It happens sometimes when relation not find required field with foreign key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM