2017-08-28 53 views
0

我有兩個賣家和產品表。每個賣家可以發佈他們的產品。我已經完成了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

嗨萊昂納多,我已編輯的問題 –

回答

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

我已更新代碼 –

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'); 
}