Ví dụ về eloquent without() và withOnly() trong laravel.

Hello mọi người, Bài viết này mình sẽ chia sẻ cách sử dụng phương thức eloquent without() và withonly() trong laravel 8. Các bạn hãy theo dõi các ví dụ bên dưới để hiểu hơn về cách sử dụng của 2 phương thức này nhé. Đầu tiên mình sẽ tạo 3 tables với dữ liệu

Hello mọi người,
Bài viết này mình sẽ chia sẻ cách sử dụng phương thức eloquent without() và withonly() trong laravel 8.
Các bạn hãy theo dõi các ví dụ bên dưới để hiểu hơn về cách sử dụng của 2 phương thức này nhé.
Đầu tiên mình sẽ tạo 3 tables với dữ liệu demo như bên dưới.
table users

table payments

table countries

Tiếp theo trong Model User mình sẽ tạo relationships cho 3 table users, payments, countries như bên dưới.
app/Models/User.php

<?phpnamespaceAppModels;useIlluminateContractsAuthMustVerifyEmail;useIlluminateDatabaseEloquentFactoriesHasFactory;useIlluminateFoundationAuthUseras Authenticatable;useIlluminateNotificationsNotifiable;useLaravelSanctumHasApiTokens;classUserextendsAuthenticatable{useHasApiTokens, HasFactory, Notifiable;protected$with=['payments','country'];//đoạn này sẽ tự động thêm điều kiện with() vào câu query khi select dữ liệu từ bảng userspublicfunctionpayments(){return$this->hasMany(Payment::class);}publicfunctioncountry(){return$this->belongsTo(Country::class);}}

Ví dụ sử dụng query lấy tất cả dữ liệu từ bảng users

Hãy thêm code bên dưới vào trong controller như bên dưới.
app/Http/Controllers/UserController.php

<?phpnamespaceAppHttpControllers;useAppModelsUser;classUserControllerextendsController{publicfunctionindex(){$users=User::get()->toArray();dd($users);}}

Output:

array:2 [▼
  0 => array:11 [▼
    "id" => 1
    "name" => "user1"
    "email" => "[email protected]"
    "country_id" => 1
    "password" => "123456"
    "remember_token" => null
    "is_active" => 1
    "created_at" => null
    "updated_at" => null
    "payments" => array:1 [▼
      0 => array:5 [▼
        "id" => 1
        "user_id" => 1
        "total_money" => 14000
        "created_at" => "2021-12-30T16:51:53.000000Z"
        "updated_at" => null
      ]
    ]
    "country" => array:4 [▼
      "id" => 1
      "name" => "Viet Nam"
      "created_at" => "2021-12-31T13:35:46.000000Z"
      "updated_at" => null
    ]
  ]
  1 => array:11 [▼
    "id" => 2
    "name" => "user2"
    "email" => "[email protected]"
    "country_id" => 2
    "password" => "123456"
    "remember_token" => null
    "is_active" => 1
    "created_at" => null
    "updated_at" => null
    "payments" => array:1 [▼
      0 => array:5 [▼
        "id" => 2
        "user_id" => 2
        "total_money" => 20000
        "created_at" => "2021-12-30T16:51:53.000000Z"
        "updated_at" => null
      ]
    ]
    "country" => array:4 [▼
      "id" => 2
      "name" => "Thai Lan"
      "created_at" => "2021-12-31T13:35:46.000000Z"
      "updated_at" => null
    ]
  ]
]

Ví dụ sử dụng phương thức without()

app/Http/Controllers/UserController.php

<?phpnamespaceAppHttpControllers;useAppModelsUser;classUserControllerextendsController{publicfunctionindex(){$users=User::without("payments")->get()->toArray();dd($users);}}

Output:

array:2 [▼
  0 => array:10 [▼
    "id" => 1
    "name" => "user1"
    "email" => "[email protected]"
    "country_id" => 1
    "password" => "123456"
    "remember_token" => null
    "is_active" => 1
    "created_at" => null
    "updated_at" => null
    "country" => array:4 [▼
      "id" => 1
      "name" => "Viet Nam"
      "created_at" => "2021-12-31T13:35:46.000000Z"
      "updated_at" => null
    ]
  ]
  1 => array:10 [▼
    "id" => 2
    "name" => "user2"
    "email" => "[email protected]"
    "country_id" => 2
    "password" => "123456"
    "remember_token" => null
    "is_active" => 1
    "created_at" => null
    "updated_at" => null
    "country" => array:4 [▼
      "id" => 2
      "name" => "Thai Lan"
      "created_at" => "2021-12-31T13:35:46.000000Z"
      "updated_at" => null
    ]
  ]
]

Ví dụ sử dụng phương thức withOnly()

app/Http/Controllers/UserController.php

<?phpnamespaceAppHttpControllers;useAppModelsUser;classUserControllerextendsController{publicfunctionindex(){$users=User::withOnly("payments")->get()->toArray();dd($users);}}

Output:

array:2 [▼
  0 => array:10 [▼
    "id" => 1
    "name" => "user1"
    "email" => "[email protected]"
    "country_id" => 1
    "password" => "123456"
    "remember_token" => null
    "is_active" => 1
    "created_at" => null
    "updated_at" => null
    "payments" => array:1 [▶]
  ]
  1 => array:10 [▼
    "id" => 2
    "name" => "user2"
    "email" => "[email protected]"
    "country_id" => 2
    "password" => "123456"
    "remember_token" => null
    "is_active" => 1
    "created_at" => null
    "updated_at" => null
    "payments" => array:1 [▶]
  ]
]

Hy vọng bài viết này sẽ giúp ích cho bạn!

Nguồn: viblo.asia

Bài viết liên quan

Check nameservers của tên miền xem website trỏ đúng chưa

Tìm hiểu cách check nameservers của tên miền để xác định tên miền đó đang dùn

Mình đang dùng Google Domains để check tên miền hàng ngày

Từ khi thông báo dịch vụ Google Domains bỏ mác Beta, mình mới để ý và bắt đầ

Cách sử dụng SFTP (Giao thức truyền file qua SSH an toàn hơn)

SFTP là cách an toàn để truyền files giữa các máy tính, gữa máy local và web hostin

Hotlinking: Key Reasons to Avoid and Methods to Protect Your Site

Hotlinking might seem an easy way to acquire website assets, but in reality, it brings several disad