Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Maybe I don't understand the question, but have you tried with pivot?: http://laravel.com/docs/eloquent#working-with-pivot-tables

Last updated 2 years ago.
0

Ruk33 said:

Maybe I don't understand the question, but have you tried with pivot?: http://laravel.com/docs/eloquent#working-with-pivot-tables

Maybe I can explain my problem a bit better.

The relationship is the following: Decks have many Cards, and Cards can be in many Decks. Whenever a Card is added to a Deck with 'attach', an amount representing the amount of copies of that specific Card in the Deck is also passed:

$card->decks()->attach($deck->id, array('amount' => 4));

This ends up forming a table like the one in my first post: a list of card_id, deck_id and amount (the number of that one card in that specific deck). What I'm trying to do now is look at ALL the Cards in ALL the Decks and see, for instance, which ones are used the most. In pseudocode I see it this way:

for (deck_id in all_decks {
    for (card_id in all_cards) {
        total of card_id = 'amount' of card_id in deck_id
    }
}

From there I could do things like "card with id 123 was used 400 times which is more than every other card" or "card with id 123 was used in 25 decks out of 150" and so on.

Hopefully that clears things up. Thanks!

Last updated 2 years ago.
0

What about something like:

	public function scopeAmountSum($query)
	{
		return $query->select(DB::raw('sum(amount) as amount_sum'));
	}
Last updated 2 years ago.
0

Ruk33 said:

What about something like:

  public function scopeAmountSum($query)
  {
  	return $query->select(DB::raw('sum(amount) as amount_sum'));
  }

Thanks! I ended up using select with DB::raw like you mentioned but had to change it a bit. Here's the working code I have for now. I'm sure it's not optimal but I wanted to move on for now and will probably look back at it once I know a bit more about the inner workings of Laravel.

	public function index()
	{
		/* Let's find the top cards */
		$top = array();
		$cards_url = array();
		$s = DB::table('cards_decks')->select(DB::raw('sum(amount) as card_count, card_id'))
			->groupBy('card_id')->orderBy('card_count', 'desc')
			->take(20)->get();
		foreach ($s as $b) {
			$c = Card::find($b->card_id);
			if ($c !== null) {
				if (!Card::isBasicLand($c)) array_push($top, $c);
				array_push($cards_url, Card::grabImage($c->name));
			}
			else {
				/* Big problem with card_id not matching the id's in cards_decks */
			}
		}

		return $this->layout->content = View::make('cards.index')->with(array(
			'top15' => $top,
			'images' => $cards_url,
		));
	}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Alexstre alexstre Joined 9 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.