Laravel Fluent vs Eloquent

First question :

Why Fluent

returns array

:

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

      

While it Eloquent

returns 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 to change the above value to Eloquent

if the models users_deck_overview

, users_deck_cards

, cards

are DeckOverview

, DeckCard

, and Card

, respectively?

+3


source to share


1 answer


Fluent's first question is Query Builder and Eloquent is ORM. Eloquent is built on Fluent.

Second question The short answer is not. ORM is not suitable for this. This is a square stub / round hole situation.

Laravel also enforces conditional configuration. In your case, this means that you are probably better off trying to restructure your database according to the Eloquent convention, rather than customizing Eloquent according to your database schema.

A simplified view of the ORM structure might look like this.

DeckOverview
hasMany DeckCard

DeckCard
belongsToMany Card
belongsTo DeckOverview

Card 
belongsToMany DeckCard

      

The best way to get all this data out is through download.

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

      

This is where the ORM doesn't really fit your approach. ORM is built to create an object that represents a record in a table. Here we have a DeckOverview which has a bunch of DeckCards. Access to them will be the same.



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

      

Or maybe it is ...

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

      

You can limit the results obtained with an active load. This will 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 to apply relationship based constraints. This will 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 here. This will require a significant change in planning and building your application.

+5


source







All Articles