如何使每个经过身份验证的用户只能看到自己的产品 [英] How to make each authenticated user only see their own product

查看:80
本文介绍了如何使每个经过身份验证的用户只能看到自己的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚经过身份验证的用户如何只能从其仪表板看到产品,即每个用户都有唯一的产品列表,并能够创建自己的产品.我现在看到的是,如果有任何用户创建,删除或列出所有用户受到影响的产品.

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

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

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.

推荐答案

只要您只需要显示经过身份验证的用户的产品,就可以更改查询以过滤出其他人的产品:

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

这篇关于如何使每个经过身份验证的用户只能看到自己的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆