Custom EloquentBuilder based on current user

Hello, I need to display a list of articles based on the currently logged in user. If the user is assigned to a company, I only recover the articles of this one otherwise I recover all the articles. Here is my code:
public static function getEloquentQuery(): Builder
{
$user = Auth::user();

if ($user->entreprise_id == null)
return parent::getEloquentQuery();

return parent::getEloquentQuery()
->where('entreprise_id', '=', $user->entreprise_id);
}
public static function getEloquentQuery(): Builder
{
$user = Auth::user();

if ($user->entreprise_id == null)
return parent::getEloquentQuery();

return parent::getEloquentQuery()
->where('entreprise_id', '=', $user->entreprise_id);
}
I always have the list of all the articles displayed regardless of the logged in user I specify that it uses version v2.17.24 yet I managed to customize my request like that on another project with version v2.16.28. Can you help me please
7 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Moudjames23
Moudjames23OP2y ago
Hello @mcfaith, Thank you for your reply. What does $this->taskWork represent and this code is implemented in which method please?
Dennis Koch
Dennis Koch2y ago
Did you use some debugging tool to check if the code is run? Did you add it to the resource?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Moudjames23
Moudjames23OP2y ago
hey @Dennis Koch When I put a dd("test") in the getEloquentQuery() method and I refresh the page nothing happens but as soon as I click on a resource, the dd("test") is executed yet my ViewArticle page does not contains only the base code Yess I add it in my Resource Sorry for the delai
Dennis Koch
Dennis Koch2y ago
Can you try getTableQuery() on the list page instead?
Moudjames23
Moudjames23OP2y ago
Hey @Dennis Koch, thank you. It's works now
protected function getTableQuery(): Builder
{
$user = Auth::user();
if ($user->entreprise_id == null)
return Article::with(['rubrique']);

return Article::with(['rubrique'])
->where('entreprise_id', $user->entreprise_id);
}
protected function getTableQuery(): Builder
{
$user = Auth::user();
if ($user->entreprise_id == null)
return Article::with(['rubrique']);

return Article::with(['rubrique'])
->where('entreprise_id', $user->entreprise_id);
}
Want results from more Discord servers?
Add your server