2017-08-28 11 views
0

売り手と商品の2つのテーブルがあります。各売り手は商品を投稿することができます。私はすでに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'); 
} 
関連する問題