anyone can help me?
Oct 25, 2022
21
Level 1
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.
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
Please or to participate in this conversation.