我如何获得谁发布产品

我如何获得谁发布产品

问题描述:

我有两个卖家和产品表。每个卖家可以发布他们的产品。我已经完成了post方法。我如何获得谁发布产品

如何知道每个产品的卖家,以及如何显示它?

class ProductController extends Controller 
{ 

    public function index() 
    { 
     return view('ps.products.create'); 
    } 

    public function store(Request $request) 
    { 
     $product = new Product; 
     $product->name = $request->name; 
     $product->description = $request->description; 
     $product->type = $request->type; 
     $product->size = $request->size; 
     $product->price = $request->price; 
     $product->image = $request->image; 

     $product->save(); 

     return view('pshome'); 
    } 

    public function show() 
    { 
     $id = DB::table('product')->get(); 
     return view('viewproduct', ['id' => $id]); 
    } 
} 

卖家模型

class Ps extends Authenticatable{ use Notifiable; 

/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
protected $fillable = [ 
    'name','lastname','email', 'password', 'username','phone', 
]; 

/** 
* The attributes that should be hidden for arrays. 
* 
* @var array 
*/ 
protected $hidden = [ 
    'password', 'remember_token', 
];} 

产品型号

类产品扩展模式{

公共$表= “产品”;

protected $ fillable = ['name','description','size','image','price','type'];

protected $ hidden = ['password','remember_token'];

}

+3

商店'AUTH() - > ID()'在产品表? – Devon

+0

你能告诉我们你的产品和卖家模型吗? –

+0

嗨莱昂纳多,我已编辑的问题 –

你可以用雄辩来做到这一点:

你可以宣布这样的两个模型之间的关系:在你的产品模型

您可以添加:

public function ps() 
{ 
    return $this->belongsTo('App\Ps'); 
} 

在您的ps模型中您可以添加:

public function products() 
{ 
    return $this->hasMany('App\Products'); 
} 

所以,你的存储方法可能如下:

use Auth; 
public function store(Request $request) 
{ 
    $ps = Auth::ps() 
    $product = new Product; 
    $product->name = $request->name; 
    $product->description = $request->description; 
    $product->type = $request->type; 
    $product->size = $request->size; 
    $product->price = $request->price; 
    $product->image = $request->image; 
    $ps->products()->save($product);//this save the product and the relarion to the user 

    return view('pshome'); 
} 

然后你就可以知道每一个产品是这样的: 你控制器把这样的事情:

public function something() 
{ 
    $ps = Ps::with("products")->all() 
    return view('viewproduct', compact("ps")); 
} 

,并在您刀片:

@foreach ($ps as $ps) 
$ps->products->id 
@endforeach 

你可以阅读更多关于人际关系的位置:

https://laravel.com/docs/5.4/eloquent-relationships 

This is the error

+0

嗨,我得到了这个错误(1/1)BadMethodCallException方法ps不存在。这是什么意思? –

+0

将代码更新为您现在拥有的代码并显示引发该错误的代码部分。 –

+0

我已更新代码 –

I found the answer just add "Auth::guard('ps')->-id to get who posted the product. 

public function store(Request $request){ 
$product = new Product; 
$product->image = $request->image; 
$product->name = $request->name; 
$product->description = $request->description; 
$product->type = $request->type; 
$product->ps_id = **Auth::guard('ps')->user()->id;** 
$product->ps_name = **Auth::guard('ps')->user()->name;** 
$product->size = $request->size; 
$product->price = $request->price; 
$product->save(); 
return view('pshome'); 
}