My Laravel boot method is not accepting any conditional



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have an app with which a user is able to view various posts.
In my model, I use boot method that will run again any query in that model so that only active posts are shown and all works so far.



protected static function boot()

parent::boot();

static::addGlobalScope('active', function (Builder $builder)
$builder->where('active', 1);
);



But now it happens that when a user is in the app, it became mandatory that he should be able to block a post, so every-time he logs in the app, he will never see that post again.



I created a hidden posts table that receives post and user_id.



public function hide_post(Request $request, $post_id)

$user = $this->getAuthorizedUser($request->header('X-User-Auth-Token'));
$user = User::find(201);
if (!$user)
return response()->json(['error' => true, 'code' => 401, 'message' => INVALID_AUTH_TOKEN], 401);


$data = [
'user_id' => $user->id,
'post_id' => $post_id,
'created_at' => now(),
'updated_at' => now()
];

$reported_post = DB::table('hidden_posts')->insert($data);

return response()->json(['success' => true, 'code' => 200, 'message' => 'Posted successfully hidden'], 200);



So far the table is updated.



Is there a way I can amend my boot method to also ensure that the app does not show me posts that I did hide?



I tried this:



protected static function boot()

parent::boot();

$hidden = DB::table('hidden_posts')->whereUser(Auth::id())->pluck('post_id')->toArray();

// A global scope is applied to all queries on this model
// -> No need to specify 'active' restraints on every query

static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



But when I try to view all my posts just for testing purposes, with a new endpoint I created, here comes the error:



SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user' in 'where clause' (SQL: select `post_id` from `hidden_posts` where `user` is null)


I thought of adding if statement in the boot method, but that does not seem to work.










share|improve this question
























  • Do I understand the business logic correctly? There is bunch of posts and bunch of users. Each user can decide to hide a post so they don't see it anymore. If thats the case I'd have a relationship between users and posts and just use pure eloquent to handle the use-case you want to achieve by querying Post::doesntHave().

    – Kyslik
    Nov 15 '18 at 13:54











  • @Kyslik a user has many posts and a post belongs to a user yes. But if i am on the site/app, I want to be able to hide any post, even if that post does not belong to me. So, from the moment I click to hide onwards, I will never see that post again on my side only. This is why I created my hidden_posts table, which receives the post id, and the id of the user of clicked on that post.

    – deSousa
    Nov 15 '18 at 13:57






  • 1





    It should be whereUserId not whereUser . In general whereSomeThing($y) is shorthand for where('some_thing','=',$y)

    – apokryfos
    Nov 15 '18 at 13:57












  • @apokryfos thans for that...but now I get an error that says hidden variable undefined

    – deSousa
    Nov 15 '18 at 13:59











  • Alright so you want to unpublish kind of functionality; all you need to do is simply flag just like you already have (active) I do not understand why do you want another table (hidden_posts) to be involved it doesn't make sense. I'd simply implement soft-deletes and thats it.

    – Kyslik
    Nov 15 '18 at 13:59


















1















I have an app with which a user is able to view various posts.
In my model, I use boot method that will run again any query in that model so that only active posts are shown and all works so far.



protected static function boot()

parent::boot();

static::addGlobalScope('active', function (Builder $builder)
$builder->where('active', 1);
);



But now it happens that when a user is in the app, it became mandatory that he should be able to block a post, so every-time he logs in the app, he will never see that post again.



I created a hidden posts table that receives post and user_id.



public function hide_post(Request $request, $post_id)

$user = $this->getAuthorizedUser($request->header('X-User-Auth-Token'));
$user = User::find(201);
if (!$user)
return response()->json(['error' => true, 'code' => 401, 'message' => INVALID_AUTH_TOKEN], 401);


$data = [
'user_id' => $user->id,
'post_id' => $post_id,
'created_at' => now(),
'updated_at' => now()
];

$reported_post = DB::table('hidden_posts')->insert($data);

return response()->json(['success' => true, 'code' => 200, 'message' => 'Posted successfully hidden'], 200);



So far the table is updated.



Is there a way I can amend my boot method to also ensure that the app does not show me posts that I did hide?



I tried this:



protected static function boot()

parent::boot();

$hidden = DB::table('hidden_posts')->whereUser(Auth::id())->pluck('post_id')->toArray();

// A global scope is applied to all queries on this model
// -> No need to specify 'active' restraints on every query

static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



But when I try to view all my posts just for testing purposes, with a new endpoint I created, here comes the error:



SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user' in 'where clause' (SQL: select `post_id` from `hidden_posts` where `user` is null)


I thought of adding if statement in the boot method, but that does not seem to work.










share|improve this question
























  • Do I understand the business logic correctly? There is bunch of posts and bunch of users. Each user can decide to hide a post so they don't see it anymore. If thats the case I'd have a relationship between users and posts and just use pure eloquent to handle the use-case you want to achieve by querying Post::doesntHave().

    – Kyslik
    Nov 15 '18 at 13:54











  • @Kyslik a user has many posts and a post belongs to a user yes. But if i am on the site/app, I want to be able to hide any post, even if that post does not belong to me. So, from the moment I click to hide onwards, I will never see that post again on my side only. This is why I created my hidden_posts table, which receives the post id, and the id of the user of clicked on that post.

    – deSousa
    Nov 15 '18 at 13:57






  • 1





    It should be whereUserId not whereUser . In general whereSomeThing($y) is shorthand for where('some_thing','=',$y)

    – apokryfos
    Nov 15 '18 at 13:57












  • @apokryfos thans for that...but now I get an error that says hidden variable undefined

    – deSousa
    Nov 15 '18 at 13:59











  • Alright so you want to unpublish kind of functionality; all you need to do is simply flag just like you already have (active) I do not understand why do you want another table (hidden_posts) to be involved it doesn't make sense. I'd simply implement soft-deletes and thats it.

    – Kyslik
    Nov 15 '18 at 13:59














1












1








1








I have an app with which a user is able to view various posts.
In my model, I use boot method that will run again any query in that model so that only active posts are shown and all works so far.



protected static function boot()

parent::boot();

static::addGlobalScope('active', function (Builder $builder)
$builder->where('active', 1);
);



But now it happens that when a user is in the app, it became mandatory that he should be able to block a post, so every-time he logs in the app, he will never see that post again.



I created a hidden posts table that receives post and user_id.



public function hide_post(Request $request, $post_id)

$user = $this->getAuthorizedUser($request->header('X-User-Auth-Token'));
$user = User::find(201);
if (!$user)
return response()->json(['error' => true, 'code' => 401, 'message' => INVALID_AUTH_TOKEN], 401);


$data = [
'user_id' => $user->id,
'post_id' => $post_id,
'created_at' => now(),
'updated_at' => now()
];

$reported_post = DB::table('hidden_posts')->insert($data);

return response()->json(['success' => true, 'code' => 200, 'message' => 'Posted successfully hidden'], 200);



So far the table is updated.



Is there a way I can amend my boot method to also ensure that the app does not show me posts that I did hide?



I tried this:



protected static function boot()

parent::boot();

$hidden = DB::table('hidden_posts')->whereUser(Auth::id())->pluck('post_id')->toArray();

// A global scope is applied to all queries on this model
// -> No need to specify 'active' restraints on every query

static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



But when I try to view all my posts just for testing purposes, with a new endpoint I created, here comes the error:



SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user' in 'where clause' (SQL: select `post_id` from `hidden_posts` where `user` is null)


I thought of adding if statement in the boot method, but that does not seem to work.










share|improve this question
















I have an app with which a user is able to view various posts.
In my model, I use boot method that will run again any query in that model so that only active posts are shown and all works so far.



protected static function boot()

parent::boot();

static::addGlobalScope('active', function (Builder $builder)
$builder->where('active', 1);
);



But now it happens that when a user is in the app, it became mandatory that he should be able to block a post, so every-time he logs in the app, he will never see that post again.



I created a hidden posts table that receives post and user_id.



public function hide_post(Request $request, $post_id)

$user = $this->getAuthorizedUser($request->header('X-User-Auth-Token'));
$user = User::find(201);
if (!$user)
return response()->json(['error' => true, 'code' => 401, 'message' => INVALID_AUTH_TOKEN], 401);


$data = [
'user_id' => $user->id,
'post_id' => $post_id,
'created_at' => now(),
'updated_at' => now()
];

$reported_post = DB::table('hidden_posts')->insert($data);

return response()->json(['success' => true, 'code' => 200, 'message' => 'Posted successfully hidden'], 200);



So far the table is updated.



Is there a way I can amend my boot method to also ensure that the app does not show me posts that I did hide?



I tried this:



protected static function boot()

parent::boot();

$hidden = DB::table('hidden_posts')->whereUser(Auth::id())->pluck('post_id')->toArray();

// A global scope is applied to all queries on this model
// -> No need to specify 'active' restraints on every query

static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



But when I try to view all my posts just for testing purposes, with a new endpoint I created, here comes the error:



SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user' in 'where clause' (SQL: select `post_id` from `hidden_posts` where `user` is null)


I thought of adding if statement in the boot method, but that does not seem to work.







laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 11:53









halfer

14.8k759117




14.8k759117










asked Nov 15 '18 at 13:47









deSousadeSousa

1,14021139




1,14021139












  • Do I understand the business logic correctly? There is bunch of posts and bunch of users. Each user can decide to hide a post so they don't see it anymore. If thats the case I'd have a relationship between users and posts and just use pure eloquent to handle the use-case you want to achieve by querying Post::doesntHave().

    – Kyslik
    Nov 15 '18 at 13:54











  • @Kyslik a user has many posts and a post belongs to a user yes. But if i am on the site/app, I want to be able to hide any post, even if that post does not belong to me. So, from the moment I click to hide onwards, I will never see that post again on my side only. This is why I created my hidden_posts table, which receives the post id, and the id of the user of clicked on that post.

    – deSousa
    Nov 15 '18 at 13:57






  • 1





    It should be whereUserId not whereUser . In general whereSomeThing($y) is shorthand for where('some_thing','=',$y)

    – apokryfos
    Nov 15 '18 at 13:57












  • @apokryfos thans for that...but now I get an error that says hidden variable undefined

    – deSousa
    Nov 15 '18 at 13:59











  • Alright so you want to unpublish kind of functionality; all you need to do is simply flag just like you already have (active) I do not understand why do you want another table (hidden_posts) to be involved it doesn't make sense. I'd simply implement soft-deletes and thats it.

    – Kyslik
    Nov 15 '18 at 13:59


















  • Do I understand the business logic correctly? There is bunch of posts and bunch of users. Each user can decide to hide a post so they don't see it anymore. If thats the case I'd have a relationship between users and posts and just use pure eloquent to handle the use-case you want to achieve by querying Post::doesntHave().

    – Kyslik
    Nov 15 '18 at 13:54











  • @Kyslik a user has many posts and a post belongs to a user yes. But if i am on the site/app, I want to be able to hide any post, even if that post does not belong to me. So, from the moment I click to hide onwards, I will never see that post again on my side only. This is why I created my hidden_posts table, which receives the post id, and the id of the user of clicked on that post.

    – deSousa
    Nov 15 '18 at 13:57






  • 1





    It should be whereUserId not whereUser . In general whereSomeThing($y) is shorthand for where('some_thing','=',$y)

    – apokryfos
    Nov 15 '18 at 13:57












  • @apokryfos thans for that...but now I get an error that says hidden variable undefined

    – deSousa
    Nov 15 '18 at 13:59











  • Alright so you want to unpublish kind of functionality; all you need to do is simply flag just like you already have (active) I do not understand why do you want another table (hidden_posts) to be involved it doesn't make sense. I'd simply implement soft-deletes and thats it.

    – Kyslik
    Nov 15 '18 at 13:59

















Do I understand the business logic correctly? There is bunch of posts and bunch of users. Each user can decide to hide a post so they don't see it anymore. If thats the case I'd have a relationship between users and posts and just use pure eloquent to handle the use-case you want to achieve by querying Post::doesntHave().

– Kyslik
Nov 15 '18 at 13:54





Do I understand the business logic correctly? There is bunch of posts and bunch of users. Each user can decide to hide a post so they don't see it anymore. If thats the case I'd have a relationship between users and posts and just use pure eloquent to handle the use-case you want to achieve by querying Post::doesntHave().

– Kyslik
Nov 15 '18 at 13:54













@Kyslik a user has many posts and a post belongs to a user yes. But if i am on the site/app, I want to be able to hide any post, even if that post does not belong to me. So, from the moment I click to hide onwards, I will never see that post again on my side only. This is why I created my hidden_posts table, which receives the post id, and the id of the user of clicked on that post.

– deSousa
Nov 15 '18 at 13:57





@Kyslik a user has many posts and a post belongs to a user yes. But if i am on the site/app, I want to be able to hide any post, even if that post does not belong to me. So, from the moment I click to hide onwards, I will never see that post again on my side only. This is why I created my hidden_posts table, which receives the post id, and the id of the user of clicked on that post.

– deSousa
Nov 15 '18 at 13:57




1




1





It should be whereUserId not whereUser . In general whereSomeThing($y) is shorthand for where('some_thing','=',$y)

– apokryfos
Nov 15 '18 at 13:57






It should be whereUserId not whereUser . In general whereSomeThing($y) is shorthand for where('some_thing','=',$y)

– apokryfos
Nov 15 '18 at 13:57














@apokryfos thans for that...but now I get an error that says hidden variable undefined

– deSousa
Nov 15 '18 at 13:59





@apokryfos thans for that...but now I get an error that says hidden variable undefined

– deSousa
Nov 15 '18 at 13:59













Alright so you want to unpublish kind of functionality; all you need to do is simply flag just like you already have (active) I do not understand why do you want another table (hidden_posts) to be involved it doesn't make sense. I'd simply implement soft-deletes and thats it.

– Kyslik
Nov 15 '18 at 13:59






Alright so you want to unpublish kind of functionality; all you need to do is simply flag just like you already have (active) I do not understand why do you want another table (hidden_posts) to be involved it doesn't make sense. I'd simply implement soft-deletes and thats it.

– Kyslik
Nov 15 '18 at 13:59













1 Answer
1






active

oldest

votes


















2














The boot function runs when the application is booted and only then. At that point you are collecting all hidden posts and hidding them using a global scope. However when you hide a post after the application has been booted it will not be reflected in the scope until you boot the application again. You can avoid this by having the scope get the hidden models when needed:



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$hidden = DB::table('hidden_posts')
->whereUserId(Auth::id())
->pluck('post_id')->toArray();
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



You might be able to save on some querying by doing :



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', function ($query)
$query->from('hidden_posts')
->whereUserId(Auth::id())
->select('post_id');
);
$builder->where('active', 1);
);






share|improve this answer























  • The reason why my code is not working it's because am not getting the user id actually. The front-end of the app is with reactive native and I cannot really use the Auth mechanism

    – deSousa
    Nov 15 '18 at 14:36











  • @deSousa I'd consider using Laravel Passport so you can use OAuth 2 for authentication.

    – apokryfos
    Nov 15 '18 at 16:32











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320888%2fmy-laravel-boot-method-is-not-accepting-any-conditional%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The boot function runs when the application is booted and only then. At that point you are collecting all hidden posts and hidding them using a global scope. However when you hide a post after the application has been booted it will not be reflected in the scope until you boot the application again. You can avoid this by having the scope get the hidden models when needed:



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$hidden = DB::table('hidden_posts')
->whereUserId(Auth::id())
->pluck('post_id')->toArray();
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



You might be able to save on some querying by doing :



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', function ($query)
$query->from('hidden_posts')
->whereUserId(Auth::id())
->select('post_id');
);
$builder->where('active', 1);
);






share|improve this answer























  • The reason why my code is not working it's because am not getting the user id actually. The front-end of the app is with reactive native and I cannot really use the Auth mechanism

    – deSousa
    Nov 15 '18 at 14:36











  • @deSousa I'd consider using Laravel Passport so you can use OAuth 2 for authentication.

    – apokryfos
    Nov 15 '18 at 16:32















2














The boot function runs when the application is booted and only then. At that point you are collecting all hidden posts and hidding them using a global scope. However when you hide a post after the application has been booted it will not be reflected in the scope until you boot the application again. You can avoid this by having the scope get the hidden models when needed:



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$hidden = DB::table('hidden_posts')
->whereUserId(Auth::id())
->pluck('post_id')->toArray();
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



You might be able to save on some querying by doing :



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', function ($query)
$query->from('hidden_posts')
->whereUserId(Auth::id())
->select('post_id');
);
$builder->where('active', 1);
);






share|improve this answer























  • The reason why my code is not working it's because am not getting the user id actually. The front-end of the app is with reactive native and I cannot really use the Auth mechanism

    – deSousa
    Nov 15 '18 at 14:36











  • @deSousa I'd consider using Laravel Passport so you can use OAuth 2 for authentication.

    – apokryfos
    Nov 15 '18 at 16:32













2












2








2







The boot function runs when the application is booted and only then. At that point you are collecting all hidden posts and hidding them using a global scope. However when you hide a post after the application has been booted it will not be reflected in the scope until you boot the application again. You can avoid this by having the scope get the hidden models when needed:



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$hidden = DB::table('hidden_posts')
->whereUserId(Auth::id())
->pluck('post_id')->toArray();
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



You might be able to save on some querying by doing :



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', function ($query)
$query->from('hidden_posts')
->whereUserId(Auth::id())
->select('post_id');
);
$builder->where('active', 1);
);






share|improve this answer













The boot function runs when the application is booted and only then. At that point you are collecting all hidden posts and hidding them using a global scope. However when you hide a post after the application has been booted it will not be reflected in the scope until you boot the application again. You can avoid this by having the scope get the hidden models when needed:



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$hidden = DB::table('hidden_posts')
->whereUserId(Auth::id())
->pluck('post_id')->toArray();
$builder->whereNotIn('post_id', $hidden);
$builder->where('active', 1);
);



You might be able to save on some querying by doing :



protected static function boot()

parent::boot();
static::addGlobalScope('active', function (Builder $builder)
$builder->whereNotIn('post_id', function ($query)
$query->from('hidden_posts')
->whereUserId(Auth::id())
->select('post_id');
);
$builder->where('active', 1);
);







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 14:29









apokryfosapokryfos

20k53464




20k53464












  • The reason why my code is not working it's because am not getting the user id actually. The front-end of the app is with reactive native and I cannot really use the Auth mechanism

    – deSousa
    Nov 15 '18 at 14:36











  • @deSousa I'd consider using Laravel Passport so you can use OAuth 2 for authentication.

    – apokryfos
    Nov 15 '18 at 16:32

















  • The reason why my code is not working it's because am not getting the user id actually. The front-end of the app is with reactive native and I cannot really use the Auth mechanism

    – deSousa
    Nov 15 '18 at 14:36











  • @deSousa I'd consider using Laravel Passport so you can use OAuth 2 for authentication.

    – apokryfos
    Nov 15 '18 at 16:32
















The reason why my code is not working it's because am not getting the user id actually. The front-end of the app is with reactive native and I cannot really use the Auth mechanism

– deSousa
Nov 15 '18 at 14:36





The reason why my code is not working it's because am not getting the user id actually. The front-end of the app is with reactive native and I cannot really use the Auth mechanism

– deSousa
Nov 15 '18 at 14:36













@deSousa I'd consider using Laravel Passport so you can use OAuth 2 for authentication.

– apokryfos
Nov 15 '18 at 16:32





@deSousa I'd consider using Laravel Passport so you can use OAuth 2 for authentication.

– apokryfos
Nov 15 '18 at 16:32



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320888%2fmy-laravel-boot-method-is-not-accepting-any-conditional%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20