【问题标题】:How to make each authenticated user only see their own product如何让每个经过身份验证的用户只能看到自己的产品
【发布时间】:2019-06-07 15:01:16
【问题描述】:

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

我已经尝试搜索其他教程但没有得到解决方案。

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

用户.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'];
}

产品控制器

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

}

我希望每个用户都有自己独特的仪表板,这意味着如果用户删除或创建产品,它应该只显示在他的仪表板中而不影响其他人。

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

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

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

    【讨论】:

    • 我应该更改或删除什么并用该代码替换? @杰克罗伯逊
    • products_model::all(); 应该是 products_model::where('user_id', $userId)->get();products_model::findOrFail($id); 应该是 products_model::where('user_id', $userId)->findOrFail($id);
    • 现在它说“未定义的变量:userId”@Jack
    • 因为您需要复制我定义$userId 的位;)或者只需将$userId 的任何引用替换为auth()->id()
    • 我更改了代码,但收到此错误“调用未定义方法 App\User::id()”@Jack
    【解决方案2】:

    在您的产品模型中,您需要添加 user_id 才能将表用户与产品相关联:

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

    在您的控制器中,您可以按用户过滤产品并返回这些产品,在创建新产品时可以获取用户登录的 id 并放入新产品

    【讨论】:

    • 我在模型和产品表中添加了它,但我仍然收到错误Call to undefined method App\User::id()@bayocr
    • 您也应该将其添加到数据库中并相应地保存并获取它
    • @msbomrel 你是什么意思保存好?我按照建议添加到数据库中,但我仍然收到错误 Call to undefined method App\User::id()
    • 我在哪里把'User_id'放在数据库中? @bayocr
    • @joh 是在数据库中,如果您使用 orm 创建数据库,则只需将 user_id 添加到产品模型中,如果您手动创建数据库,则需要在产品中添加用户的外键表
    猜你喜欢
    • 1970-01-01
    • 2017-04-12
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2017-09-11
    • 2010-09-14
    • 2020-11-15
    相关资源
    最近更新 更多