constrained()
The constrained()
method is a shorthand in Laravel's migration schema builder that helps you quickly define a foreign key constraint for a column.
$table->foreignId('column_name')->constrained('table_name');
- Automatically creates a foreign key constraint on the specified column.
- Infers the referenced table if not explicitly provided. For example,
user_id
would refer to theusers
table by default. - Assumes the foreign key references the
id
column of the referenced table.
Example
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained(); // References `users.id` by default
$table->timestamps();
});
Equivalent to:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Status: #idea
Tags: web-programmingWeb Programming (Laravel)here are the mid exam materials
I understand it NOW
Final Exam Materials
Mid Exam Materials
Initialization
composer create-project laravel/laravel newProject
not necessary since no wifi and project might be already given
Database
Setup Database
1. Open .env file and look for DB_.. and make sure its the same as this
DB_CONNECTION=mysql // XAMPP runs mysql database engine
DB_HOST=127.0.0.1
DB_PORT=3306 // port for database
DB_DATABASE=laravelproject // make a new database name
DB_USERNA