Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

emilpapelas4@gmail.com's avatar

How to use Relationship.

So i have 2 tables with items.

In first table have items that i own.

public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('icon_url');
            $table->string('tradable');
            $table->timestamps();
        });
    }

In second have items that got from an api.

 public function up()
    {
        Schema::create('bp_items', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('classid');
            $table->decimal('price', 9, 3);
            $table->string('color');
            $table->timestamps();
        });
    }

What i want is to get is compare ex MYITEM from first table an search for that item in second table by MYITEM (name ) and i want to get the item ->price from that table.

0 likes
21 replies
kokoshneta's avatar

Your question is titled “How to use relationship[s]”, but you have no relationship at all between these two tables.

The only thing your tables have in common is that they both have a name column, and that’s not a relationship – half your tables probably have a name column without having anything to do with each other. So basically, they’re completely unrelated tables.

So what you really want is just to retrieve records from the second table based on the name column. That’s a simple Eloquent query:

$item = Item::find(1234);
$bp_items = BpItem::where('name', $item->name)->get();
kokoshneta's avatar

@emilpapelas4@gmail.com So do that, then. I don’t understand what’s not clear.

Have you learned the basics of Laravel? Do you know how Eloquent works?

In the code I gave you above, let’s say row in the items table with ID 1234 has the name MyItem1234. In that case, $bp_items will be a collection of all bp_items whose name is MyItem1234. For each of those, you can simply add ->price to get the price.

If this is not clear to you, you need to learn how the basics of Laravel work before you start coding larger projects.

emilpapelas4@gmail.com's avatar

@kokoshneta Ya know laravel

have done this

$skins=Items::where('tradable',1)->get();
    $pbi=BpItems::where('name',$skins->name)->get();
    dd($skins);
    return view('user.skins' , compact('skins'));

but error Property [name] does not exist on this collection instance.

emilpapelas4@gmail.com's avatar

@MichalOravec @kokoshneta

Let me show my db

https://imgur.com/dnYAXoC

On items table have an item ,also same item on bp_items table difference is that item contain the price ,color etc. Only want to use the items table to get the item because this is the item that i own , but alos i need the price and i need to get them from pb_items table .

I hope are u understand what i want to get.

kokoshneta's avatar

@emilpapelas4@gmail.com As I said, you need to get more acquainted with how Laravel, or at least Eloquent, works. Using get() will return a collection of items, since there may be more than one. Unless you only have one item which is tradable, there will in fact definitely be more than one.

Obviously, there is no name property on a collection. That property belongs to the item, not to the collection.

emilpapelas4@gmail.com's avatar

@kokoshneta ya but as u send me the example above i can't use

$item = Item::find(1234);

because this will return me only the specific item. But i want all the item to be shown from that Item List

emilpapelas4@gmail.com's avatar

@MichalOravec

$it=DB::table('Items')->select('name')->join('items','name')->join('bpitems','name');

I do this , i know is not correct , but i dont use DB and join until now. maybe please correct my code

kokoshneta's avatar

@emilpapelas4@gmail.com Well, that is definitely not correct, for several reasons:

  1. There is no Items table; the table is called items
  2. You’re joining the items table to itself (DB::table('items')->join('items'))
  3. You’re only selecting the name, nothing else
  4. You’re not specifying how to join the tables – you need a condition

This is all explained very clearly in the documentation.

kokoshneta's avatar
Level 27

If you always want to get most info from items + price from bp_items and you KNOW that the name column is always unique in both tables, then, as @michaloravec says, use a join clause to get the price column from the BP table.

Item model

use Illuminate\Database\Eloquent\Builder;

class Item extends Model {
	public function scopeWithPrice(Builder $builder) {
		$builder->join('bp_items', 'items.name', '=', 'bp_items.name')
			->select('items.*', 'bp_items.price')
		;
	}
}

Controller

$skins = Items::where('tradable', 1)->withPrice()->get();
return view('user.skins', compact('skins');
1 like
Lumethys's avatar

@emilpapelas4@gmail.com we guiding you, not working for you, so we provide a path, a hint, a document link, you have to read it, understand it and work with it

You clearly have no idea how database work AND how ORM work, without that knowledge, you will get nowhere, the only thing you could do is ask a question and wait for someone to return the whole solution

1 like

Please or to participate in this conversation.