Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Fluent vs Eloquent

First question:

Why does Fluent return an array:

return DB::connection('mysql')->table('cards')
    ->where('setCode', '=', $setcode)
    ->get();

While Eloquent returns an object:

return Card::where('setCode', '=', $setcode)
    ->get();

The data itself looks exactly the same as far as I can tell.

Second question:

The following code is in Fluent:

DB::table('auth.users_deck_overview as deckOverviewDB')
        ->leftJoin('auth.users_deck_cards as deckCardsDB', 'deckOverviewDB.deck_uid', '=', 'deckCardsDB.deck_uid')
        ->leftJoin('mtg_cards.cards as cardsDB', 'deckCardsDB.card_uid', '=', 'cardsDB.uid')
        ->select('cardsDB.name', 'deckCardsDB.card_quantity', 'cardsDB.manaCost', 'cardsDB.colors', 'cardsDB.cmc')
        ->where('deckOverviewDB.username', '=', $user->username)
        ->where('deckOverviewDB.deck_uid', '=', $deckUid)
        ->where('deckCardsDB.board', '=', 0)
        ->where('cardsDB.cmc', '!=', '')
        ->get();

How would I change the above to Eloquent assuming the users_deck_overview, users_deck_cards, cards Models are DeckOverview, DeckCard, and Card respectively?

like image 514
rotaercz Avatar asked Jun 02 '15 03:06

rotaercz


People also ask

What is fluent Laravel?

Fluent is a utility class provided by @laravelphp that lets us handle data fluently. Out of the box it can be turned into an array or JSON, it can act as an array and it's serializable as a JSON. It also gets and sets attributes dynamically and in a fluent way.

What is difference between eloquent and DB in Laravel?

If you want to change the database, it will be easier with Laravel Eloquent as it can handle many different databases, whereas the DB facade requires you to write SQL which may have to be rewritten for a different database.

Why we use eloquent in Laravel?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Before getting started, be sure to configure a database connection in config/database.

What is lazy collection in Laravel?

Lazy collection class is mainly designed to keep memory usage low by the Laravel applications. It is using the power of PHP Generators and allows us to work with large databases while using low memory usage.


1 Answers

First Question Fluent is a query builder and Eloquent is an ORM. Eloquent is built upon Fluent.

Second Question The short answer is you wouldn't. An ORM isn't really suited to do things this way. This is a square peg/round hole situation.

Laravel also applies convention over configuration. In your case this means that you'd probably be better off trying to restructure your database to match Eloquent's convention rather then configure Eloquent to match your database schema.

An oversimplified view of the ORM structure may look like this.

DeckOverview
hasMany DeckCard

DeckCard
belongsToMany Card
belongsTo DeckOverview

Card 
belongsToMany DeckCard

The best way to pull all that data is through eager loading.

$deckOverview = DeckOverview::with('deckCards.cards')->first();

This is where the ORM really doesn't line up with your approach. An ORM is built to have an object that represents a record in the table. Here we have a DeckOverview that has a bunch of DeckCards. They would be accessed like so.

$deckCard = $deckOverview->deckCards->first();

Or maybe this...

foreach ($deckOverview->deckCards as $deckCard) 
{
    foreach ($deckCard->cards as $card)
    {
        // do something with each card in the deck
    }
}

You can limit the results pulled through eager loading. This would only load DeckCards where the board was 0.

$deckOverviews = DeckOverview::with(array('deckCards' => function ($query) 
{
    // Equals comparison operator can be assumed
    $query->where('board', 0);
})->get()

You can use has to apply constraints based on relationships. This would only load DeckOverviews that had DeckCards where the board was 0.

$deckOverviews = DeckOverview::has(array('deckCards' => function ($query)
{
    $query->where('board', 0);
});

There is a lot to take in here. It will require a major shift in how you plan and build your application.

like image 150
Collin James Avatar answered Oct 15 '22 07:10

Collin James