Demo

After you log in to the program, you can create your first data.

wood_prog_1

The result of executing this data set:

App\Models\User:

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = ["name", "email", "password", "country_id"];

    /**
     * The attributes that should be cast.
     * @var array
     */
    protected $casts = [
        "name" => "string",
        "email" => "string",
        "password" => "string",
        "country_id" => "int",
    ];

    /**
     * The reverse relation for "users->countries".
     */
    public function country(): HasOne
    {
        return $this->hasOne(Country::class, "id", "country_id");
    }

    /**
     * The opposite relation for "users->books".
     */
    public function books(): BelongsToMany
    {
        return $this->belongsToMany(
            Book::class,
            "users_books",
            "user_id",
            "book_id",
        );
    }
}
                

App\Models\Country:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Country extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = ["name"];

    /**
     * The attributes that should be cast.
     * @var array
     */
    protected $casts = ["name" => "string"];

    /**
     * The opposite relation for "countries->users".
     */
    public function users(): HasMany
    {
        return $this->hasMany(User::class, "country_id", "id");
    }
}
                

App\Models\Book:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Book extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = ["name", "content"];

    /**
     * The attributes that should be cast.
     * @var array
     */
    protected $casts = ["name" => "string", "content" => "string"];

    /**
     * The reverse relation for "books->users".
     */
    public function authors(): BelongsToMany
    {
        return $this->belongsToMany(
            User::class,
            "users_books",
            "book_id",
            "user_id",
        );
    }
}
                

/database/migrations/2022_12_01_000000_create_users_table.php:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create("users", function (Blueprint $table) {
            $table->id();
            $table->string("name");
            $table->string("email");
            $table->string("password");
            $table
                ->foreignId("country_id")
                ->nullable()
                ->constrained("countries")
                ->cascadeOnUpdate()
                ->cascadeOnDelete()
                ->nullOnDelete();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists("users");
    }
};
                

/database/migrations/2022_12_01_000001_create_countries_table.php:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create("countries", function (Blueprint $table) {
            $table->id();
            $table->string("name");
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists("countries");
    }
};
                

/database/migrations/2022_12_01_000002_create_books_table.php:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create("books", function (Blueprint $table) {
            $table->id();
            $table->string("name");
            $table->string("content");
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists("books");
    }
};
                

/database/migrations/2022_12_01_999999_create_users_books_table.php:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create("users_books", function (Blueprint $table) {
            $table->foreignId("user_id")->constrained("users");
            $table->foreignId("book_id")->constrained("books");
        });
    }

    /**
     * Reverse the migrations.
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists("users_books");
    }
};