🤣Dissociate full hay nói cách khác là xóa mềm soft delete (ok)

https://github.com/laravel/framework/issues/25250

One to One

C:\xampp\htdocs\datvietcoconut\database\migrations\2022_12_16_060919_create_phones_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePhonesTable extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('phones', function (Blueprint $table) {
      $table->increments('id');
      $table->string('phone');
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('phones');
  }
}

C:\xampp\htdocs\datvietcoconut\database\migrations\2022_12_16_060944_create_screens_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateScreensTable extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('screens', function (Blueprint $table) {
      $table->increments('id');
      $table->string('name');
      $table->unsignedInteger('phone_id')->nullable();
      $table->foreign('phone_id')->references('id')->on('phones');
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('screens');
  }
}

C:\xampp\htdocs\datvietcoconut\app\Models\Phone.php

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
  use HasFactory;
  public $timestamps = false;
  protected $fillables = ['phone'];
  public function screen()
  {
    return $this->hasOne(Screen::class);
  }
}

C:\xampp\htdocs\datvietcoconut\app\Models\Screen.php

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Screen extends Model
{
  use HasFactory;
  public $timestamps = false;
  protected $fillables = ['name','phone_id'];
  public function phone()
  {
    return $this->belongsTo(Phone::class);
  }
}

C:\xampp\htdocs\datvietcoconut\database\factories\PhoneFactory.php

<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class PhoneFactory extends Factory
{
  /**
   * Define the model's default state.
   *
   * @return array
   */
  public function definition()
  {
    return [
      "phone" => $this->faker->phoneNumber(),
    ];
  }
}

C:\xampp\htdocs\datvietcoconut\database\factories\ScreenFactory.php

<?php
namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class ScreenFactory extends Factory
{
  private static $order = 1;
  /**
   * Define the model's default state.
   *
   * @return array
   */
  public function definition()
  {
    return [
      "name" => $this->faker->userName(),
      "phone_id" => self::$order++
    ];
  }
}

C:\xampp\htdocs\datvietcoconut\database\seeders\DatabaseSeeder.php

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Phone;
use App\Models\Screen;
class DatabaseSeeder extends Seeder
{
  /**
   * Seed the application's database.
   *
   * @return void
   */
  public function run()
  {
    Phone::factory(10)->create();
    Screen::factory(10)->create();
  }
}

C:\xampp\htdocs\datvietcoconut\app\Http\Controllers\TestController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Phone;
class TestController extends Controller
{
  public function dissociate()
  {
    $phone = Phone::with('screen')->where('id', 3)->first();
    $phone->screen->phone()->dissociate();
    $phone->push();
  }
}

C:\xampp\htdocs\datvietcoconut\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
  return view('welcome');
});
Route::get('/dissociate', [TestController::class,'dissociate']);

One To Many

C:\xampp\htdocs\datvietcoconut\app\Http\Controllers\TestController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\Comment;
class TestController extends Controller
{
  public function index($id)
  {
    $comments = Post::find($id)->comments;
    foreach ($comments as $comment) {
      echo '<pre>';
      var_export($comment->comment);
      echo '<pre>';
    }
  }
  public function index2($id)
  {
    $post = Comment::find($id)->post;
    echo '<pre>';
    echo "id and name => " . $post->id . " and " . $post->name;
    echo '<pre>';
  }
  public function save($id)
  {
    $post = Post::find($id);
    $comment1 = new Comment();
    $comment1->comment = "Hi ItSolutionStuff.com Comment 1";
    $comment2 = new Comment();
    $comment2->comment = "Hi ItSolutionStuff.com Comment 2";
    $post = $post->comments()->saveMany([$comment1, $comment2]);
  }
  public function create($id)
  {
    $post = Post::find($id);
    $comment = new Comment();
    $comment->comment = "Hi ItSolutionStuff.com Update ID 6";
    $post = $post->comments()->save($comment);
  }
  public function associate($idcomment, $idpost)
  {
    $comment = Comment::find($idcomment);
    $post = Post::find($idpost);
    $comment->post()->associate($post)->save();
  }
  public function dissociate()
  {
    $comment = Comment::with('post')->where('id', 8)->first();
    $comment->post()->dissociate();
    $comment->save();
  }
}

C:\xampp\htdocs\datvietcoconut\database\migrations\2022_11_18_022236_create_comments_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCommentsTable extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('comments', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('post_id')->unsigned()->nullable();
      $table->string("comment");
      $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('comments');
  }
}

C:\xampp\htdocs\datvietcoconut\database\migrations\2022_11_18_021539_create_posts_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('posts', function (Blueprint $table) {
      $table->increments('id');
      $table->string("name");
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('posts');
  }
}

C:\xampp\htdocs\datvietcoconut\app\Models\Post.php

<?php
namespace App\Models;

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

class Post extends Model
{
  use HasFactory;
  public $timestamps = false;
  protected $fillable = [
    'name'
  ];
  /**
   * Get the comments for the blog post.
   */
  public function comments()
  {
    return $this->hasMany(Comment::class);
  }
}

C:\xampp\htdocs\datvietcoconut\app\Models\Comment.php

<?php
namespace App\Models;

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

class Comment extends Model
{
  use HasFactory;
  protected $fillable = [
    'post_id',
    'comment'
  ];
  /**
   * Get the post that owns the comment.
   */
  public function post()
  {
    return $this->belongsTo(Post::class, 'post_id');
  }
}

C:\xampp\htdocs\datvietcoconut\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
  return view('welcome');
});
Route::get('/dissociate', [TestController::class, 'dissociate']);

Many To Many

Trong mối quan hệ many to many không có dissociate

Last updated