I've been scratching my head over this one, could you do an sql dump and post it here so we could see
all fields in all tables you have?
Hi!
My badge table,"badges"
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`category_id` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
PRIMARY KEY (`id`)
My user table, "users"
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(75) COLLATE utf8_unicode_ci NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
PRIMARY KEY (`id`)
And the pivot table between them, "badge_user"
`id` int(11) NOT NULL AUTO_INCREMENT,
`badge_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`achieved_at` int(11) NOT NULL,
`accuracy` tinyint(4) NOT NULL DEFAULT '0',
`state` char(1) COLLATE utf8_unicode_ci NOT NULL,
`comment` varchar(140) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
My model "User" has defined the following relation,
public function badges()
{
return $this->belongsToMany('Badge', 'badge_user')->withPivot('created_at', 'updated_at', 'achieved_at', 'accuracy', 'state', 'comment');
}
;)
Thank you very much again!
I don't see where user is linked to badges, but would
a simpler table work:
ID NAME MERIT ARCHERY SKING TOBACCO_SPITTING
1 JOE BLOW 2 3 1 1
2 SALLY MAE 1 2 5 0
3 BILLY BOB 1 0 0 19
SiPoX said:
- I have a model "User" which has a relationship called badges. (A user can have many badges)
I'm guessing name in badge table is badge name.
Also read this thread, there's a link - may or may not be helpful.
http://laravel.io/forum/12-09-2014-model-with-many-types-advice-wanted
Hi jimgwhit! ;)
Yes, the field "name" in the badge table is the name of the badge. I don't understand what you mean when you say "where user is linked to badge". ;) The relation (defined in the user model) badges() joins a user with his badges through the pivot table badge_user, doesn't it?
In fact, I can get the user's badges, and the badges of a user from a category (first example above). But I don't know how I could calculate the max and the min category of a user (I mean, in which category a user has more badges and in which less).
I hope to be able to explain myself ;) :P
Thank you very much! ;)
Is badge_user a table in your database, or is it a temp table being created?
SiPoX said: But I would get the best category of the user. I mean, the category where the user has the max number of badges. How could I execute this? I've tried several ways with no results.. :( ;)
I'm just not seeing where you are entering a number of badges. I see badge_id` int(11) NOT NULL in table badge_user.
But where are you actually entering something like
Joe Blow has 12 badges for swimming? I believe you are a couple of fields short, but without having the whole application I could be and probably am wrong -- sorry. Please, if you solve this on your own, don't leave us hanging, please show what you did, I think anyone who sees the final solution can learn from it.
Hi!
Yes, my table badge_user is in my database. ;) Well, I don't save how many badges a user has, due to the fact I calculate this with the SQL function count().
"Manually", I did (in my User model)
public function bestWeakest()
{
// IDs of my categories (categories are in the table categories but I manually set them here)
$topics = array (3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
// Variables to save the max ID and the min ID
$max = 0;
$min = 0;
foreach ($topics as $topic)
{
// Number of badges of this user by each category
$nob = $this->achievedBadgesByTopic($topic)->count();
// Save if max
if ($nob >= $max) $max = $topic;
// Save if min
if ($nob <= $min) $min = $topic;
}
// Compose an array
$r = array('max' => $max, 'min' => $min);
// Return the array
return $r;
}
Obviously, it works, but I wonder if I could do this through a SQL sentence via Laravel Eloquent functions. ;) :P
Thank you again! ;)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community