【发布时间】:2017-10-07 09:48:21
【问题描述】:
所以我有 2 个表产品和购买,我试图获得属于产品所有者的购买。任何人都可以购买,但只有用户拥有产品
我的桌子是这样的:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->text('description');
$table->string('category');
$table->string('auction_type');
$table->string('allow_offers');
$table->string('condition');
$table->integer('quantity');
$table->integer('auctionlength');
$table->decimal('buyitnow_price');
$table->decimal('auction_price');
$table->string('shipping');
$table->decimal('shipping_cost');
$table->integer('shipping_time');
$table->string('international_shipping');
$table->string('location');
$table->string('allow_returns');
$table->date('enddate');
$table->timestamps();
$table->softDeletes();
});
采购表
Schema::create('purchases', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('product_id');
$table->decimal('amount');
$table->string('name');
$table->string('address');
$table->string('city');
$table->string('state');
$table->string('country');
$table->string('postal');
$table->string('email');
$table->string('status');
$table->timestamps();
$table->softDeletes();
});
我尝试了以下方法,但这似乎仍然会出现行:
dd(Product::where('user_id', Auth::user()->id)->with('purchases')->get());
采购模式
public function product()
{
return $this->belongsTo('App\Models\Market\Product');
}
产品型号
public function purchases()
{
return $this->hasMany('App\Models\Market\Purchases');
}
我的两个型号是Purchases和Products,没有原始查询,我如何购买属于user_id的产品?
【问题讨论】:
-
我真的建议你在问任何问题之前使用文档,你必须了解 Laravel 是如何工作的,就像 Eloquent 一样,然后做你的模型/DB,然后问我们。对于那些写得不好,甚至对他/她正在使用的系统一无所知的人,真的很难帮助......
标签: php laravel laravel-5 eloquent