0
votes

I have two tables for sellers and products. Each seller can post their product. I've already done the post method.

How can I know each product's seller, and how can I display it?

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

Seller model

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',
];}

Product model

class Product extends Model {

public $table = "Product";

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

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

}

2
Store auth()->id() in the products table?Devon
can you show us your product and seller models?Leonardo Cabré
Hi leonardo, i have edit the questionDaniel Asyraf
does your Seller and Product has one to one relationship ?jaysingkar

2 Answers

0
votes

you can use Eloquent to do that:

you can declare the relation between the two models like this:

in you products model you can add:

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

in your ps model you can add:

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

so, your store method may look like this:

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

And then you you can know each product like this: in you controller put something like this:

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

and in your blade:

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

You can read more about relationships here:

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

This is the error

0
votes

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