vendredi 7 juin 2019

How to make each authenticated user only see their own product

I'm trying to figure out how an authenticated user can only see products from their dashboard, i.e., every user has unique products list, and be able to create their own products. What i see now is if any user creates,delete or list product all users get affected.

I have tried to search other tutorials but didn't get the solution.

web.php

Route::group(['prefix'=>'seller', 'middleware'=>     ['auth']],function() {
Route::get('/',function (){
    return view('seller.index', compact('products'));
});
Route::resource('product', 'productController');
Route::get('/seller', 'ProductController@seller')->name('seller');

});

User.php

 public function products()
 {
  return $this->hasMany(Products_model::class);
 }

Products_model

class products_model extends Model
{
protected $table='products';
protected $primaryKey='id';
protected $fillable=   ['pro_name','pro_price','pro_info','image','stock','category_id'];
}

ProductController

class productController extends Controller
{

public function index()
{
   $products=products_model::all();
   return view('seller.product.index',compact('products'));
}

public function user()
{
return $this->belongsTo(User::class);
}

public function create()
{

    return view('seller.product.create');

}

public function seller()
{
   $products=products_model::all();
   return view('seller.product.index',compact('products'));
}

public function store(Request $request)
{
    $formInput=$request->except('image');
    $this->validate($request, [
     'pro_name'=> 'required',
     'pro_price'=> 'required',
     'pro_info'=> 'required',
     'image'=>'image|mimes:png,jpg,jpeg|max:10000'
    ]);

    $image=$request->image;
    if($image){
        $imageName=$image->getClientOriginalName();
        $image->move('images', $imageName);
        $formInput['image']=$imageName;
    }

    products_model::create($formInput);
    return redirect()->back();
}

public function show($id)
{
    //
}

public function edit($id)
{
    //
}

public function update(Request $request, $id)
{
    //
}

public function destroy($id)
{
   $deleteData=products_model::findOrFail($id);
   $deleteData->delete();

   return redirect()->back();
}

}

I want every user to have their unique dashboard, which means if a user deletes or creates a product it should only show in his dashboard without affecting others.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire