AmandaRJ
AmandaRJ

Reputation: 249

Fetch data via select according to logged user ID Laravel

I'm using the code below that returns the number of products registered per day:

$products = Product::select(DB::raw('count(id) as `number_products`'), DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') new_date"))
            ->whereRaw('DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)')
            ->groupBy('new_date')->orderBy('new_date') 
            ->where('user_id',Auth::user()->id)
            ->get();
 $qtdProduct = ($products ->pluck('number_products')->toArray());

I need to do this now for the logged in user. I thought about using this:

where('user_id',Auth::user()->id)

I inserted the line above and even then, continues to select all products ignoring the 'user_id' logged. Where am I missing this? can you help me?

Upvotes: 0

Views: 117

Answers (1)

Mario Inostroza
Mario Inostroza

Reputation: 766

you can use a Policy for that:

php artisan make:Policy ProductPolicy --model=Product

and then you need to register the Policy inside AuthServiceProviders:

 protected $policies = [
    Product::class =>ProductPolicy::class,
];

Inside your policy you can manipulate your policies entries accord your methods inside your ProductController.

Good luck!

Upvotes: 1

Related Questions