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

Bump, bump...

0

Bump, bump...

0

Make an eloquent model for "Item" as a start. I'd then created two function calls for the user model. ItemsWanted and ItemsHave which will both be a one to many relation (user to items).

0

I will try thank you for your response!

0

Also, on your item tables, you're going to need a field for the user that item belongs to.

id|item_name|item_image|item_user

index the user field, that way your model call can be something like

public function ItemsHave(){
    return $this->hasMany('App\ItemHave','item_user','id');
}
0

Thank you. I will try it now

0

Here are my tables

CREATE TABLE `items_have` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `stock` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `items_have_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `items_want` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `stock` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `items_want_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `steamid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `steamidfull` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `avatar` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_steamid_unique` (`steamid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf

everything is ok. But i need one trade table i think for many (items_have) and many (items_want)

Last updated 9 years ago.
0

bump.....

0

bump.....

0

Ok... with many re-searching and reading finally i've got that i need. The returned information is what i expected to be. But im not sure if its ok to get relations like this in loop?

    $itemsHave = $trade->itemsHave()->get();
    $itemsWant = $trade->itemsWant()->get();

Here full example:

    $trades = App\Models\Trades::with([
        'user' => function($query) {
            $query->where('steamid', '=', Auth::user()->steamid);
        }
    ])->with('itemsHave', 'itemsWant')->get();


    foreach ($trades as $trade) {

        echo $trade->title . '<br/>';

        $itemsHave = $trade->itemsHave()->get();
        $itemsWant = $trade->itemsWant()->get();

        foreach ($itemsHave as $ih) {
            echo $ih->market_name . '<br/>';
        }

        echo ' < > <br/>';

        foreach ($itemsWant as $iw) {
            echo $iw->market_name . '<br/>';
        }
    }
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

xscence xscence Joined 22 Aug 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.