5

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?

1 Answer 1

10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.