PHP Snippet 1:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->text('caption');
$table->string('image');
$table->timestamps();
// $table->index('user_id');
});
}
PHP Snippet 2:
$table->foreignId('user_id')->constrained();
PHP Snippet 3:
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
PHP Snippet 4:
class Post extends Model
{
protected $guarded = [];
Public function user()
{
return $this->belongsTo(User::class?'user_id', 'id');
}
}
PHP Snippet 5:
php artisan migrate:refresh
PHP Snippet 6:
php artisan migrate:refresh