Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Norby Baruani
Norby Baruani

Posted on

     

Laravel Eloquent ORM 2-in-1 Methods

Overview

Eloquent is an object-relational mapper (ORM) that makes it enjoyable to interact with your database.

ORM allow us to have a neat structure within our codebase with concept ofModels. A Model in this case will be a representation of a database row as well as entity relationship.

Concepts

A Model has mainly 3 responsibilities:

  1. Managepersistence of data andrelationship
  2. Hold datain Memory
  3. Implementbusiness logic

We will be focusing on some useful methods in Eloquent which encapsulate 2 responsibility in one. The benefit of this is it reduces codes and conditional statements in your logic.

2-in-1 Functions

Retrieving Or Creating Record

  • firstOrCreate() will attempt to retrieve a record using the given data. However in case the record was not found, new record will be created and persisted to the database using given data.
  • firstOrNew() similar tofirstOrCreate() however if no records were found a new instance of the model will be returned with the given data but data will not automatically be persisted. A manual intervention will be required by callingsave() methods on the instance returned.
useApp\Models\Flight;// Retrieve flight by name// or create it if it doesn't exist...$flight=Flight::firstOrCreate(['name'=>'London to Paris']);// Retrieve flight by name// or create it with the name, delayed, and arrival_time attributes...$flight=Flight::firstOrCreate(['name'=>'London to Paris'],['delayed'=>1,'arrival_time'=>'11:30']);// Retrieve flight by name// or instantiate a new Flight instance...$flight=Flight::firstOrNew(['name'=>'London to Paris']);// Retrieve flight by name// or instantiate with the name, delayed, and arrival_time attributes...$flight=Flight::firstOrNew(['name'=>'Tokyo to Sydney'],['delayed'=>1,'arrival_time'=>'11:30']);
Enter fullscreen modeExit fullscreen mode

Updating Or Creating Records

  • updateOrCreate() will attempt to update a record using the given data. However in case the record was not found, new record will be created and persisted to the database using given data.
useApp\Models\Flight;// Update flight where departure and destination with price and discounted// or create new record with departure, destination, price and discounted$flight=Flight::updateOrCreate(['departure'=>'Oakland','destination'=>'San Diego'],['price'=>99,'discounted'=>1]);
Enter fullscreen modeExit fullscreen mode

Updating Or Creating Records (Batch)

  • upsert() Enable us to inserts multiple rows into a database table if they do not already exist, or updates them if they do in a single query.
useApp\Models\Flight;// The method's first argument consists of the values to insert or update,// while the second argument lists the column(s) that// uniquely identify records within the associated table.// While the third argument is an array of the columns// that should be updated if a matching record// already exists in the database.Flight::upsert([['departure'=>'Oakland','destination'=>'San Diego','price'=>99],['departure'=>'Chicago','destination'=>'New York','price'=>150]],['departure','destination'],['price']);
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Cape Town, South Africa
  • Pronouns
    He/Him
  • Work
    Software Developer Engineer
  • Joined

More fromNorby Baruani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp