joh
joh

Reputation: 238

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 his 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.

Upvotes: 0

Views: 1100

Answers (2)

Jack Robertson
Jack Robertson

Reputation: 383

Wherever you need to display only the authenticated user's products, change your query to filter out other peoples products:

public function controllerAction(Request $request)
{  
    $userId = $request->user()->id;
    // or $userId = Auth::id(); (Via the Auth facade)
    // or $userId = auth()->id();

    $products = products_model::where('user_id', $userId)->get();
}

Upvotes: 1

bayocr
bayocr

Reputation: 1

In your product model you need add the user_id to able relate the tables user with products:

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

After in you controller you can filter the products by user and return these, at create new product can get the user logged id and put in new product

Upvotes: 0

Related Questions