Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spAo's avatar
Level 1

If already exists

Hello, i have socialogs table and it's have question_id and token columns. I need if someone already voted then button must display but if he didn't voted button should appear.

    @foreach($socialogs as $socialog)
             @endforeach
             @if(!App\Models\Socialog::where('question_id', $socialog->question_id)->where('token', $socialog->token))
             <button type="submit" class="btn btn-primary">Vote</button>
             @endif

But it's displayed button in all votes.

0 likes
28 replies
Sinnbeck's avatar

You should move this logic out of blade. But anyways, you are missing get/first or similar (using exists() in my example)

!App\Models\Socialog::where('question_id', $socialog->question_id)->where('token', $socialog->token)->exists())
Sinnbeck's avatar

@spAo check there is data

App\Models\Socialog::where('question_id', $socialog->question_id)->where('token', $socialog->token)->get()-dd()
spAo's avatar
Level 1

@Sinnbeck I added another vote and voted it and its still showing me one array in dd i think its caching only token so when he sees the same token its displaying button

Sinnbeck's avatar

Is it by the way supposed to be inside the loop? You use the last $sociolog in the array

Sinnbeck's avatar

@MichalOravec I assume even though they are named such, that $socialogs as $socialog isn't the same as Socialog.. If that is the case then the code makes no sense

Sinnbeck's avatar

@MichalOravec I am starting to fear that you are correct :/

@spao maybe instead post your whole logic and model so we can better help you out

Sinnbeck's avatar

Let's start again

Where do you store votes?

Sinnbeck's avatar

@spAo but that is already in $socialog then ?

How does your controller method for this page look?

spAo's avatar
Level 1

@Sinnbeck

  public function socialog()
    {
        $questions = Question::all();
        $socialogs = Socialog::all();
        return view('pages.socialog.socialog', compact('questions'));
    }

    public function addVote(Socialog $socialogs)
    {
        $data = request()->validate([
            'answer_id' => 'required',
            'question_id' => 'required',
            'token' => 'required', 'unique:socialogs'
         ],[
             'answer_id.required' => 'please, choose answer'
         ]);
      $socialogs = Socialog::create($data);
      return redirect()->back(); 
    }
Sinnbeck's avatar

@spAo where do you set $socialogs?

You use it here but your variable is called $questions?

@foreach($socialogs as $socialog) 
spAo's avatar
Level 1

@Sinnbeck old controller i'm having in socialog function

        $socialogs = Socialog::all();
MohamedTammam's avatar

I see some weird parts in the code.

First, why you are using and empty foreach?

@foreach($socialogs as $socialog)
             @endforeach

Shouldn't it be something like that? and BTW, you can use just Socialog. And also you should using get/first/count or similar methods inside the if statement.

@foreach($socialogs as $socialog)
	@if(!Socialog::where('question_id', $socialog->question_id)->where('token', $socialog->token)->count())
		<button type="submit" class="btn btn-primary">Vote</button>
	@endif
 @endforeach

And then, it will always return true, isn't that right? Am I understanding correctly or what?

spAo's avatar
Level 1

@MohamedTammam count? why? nope it's not working i just need if already is voted to display button if in socialogs table already exists question_id and token button must display i was thinking it's simple but...

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@spAo it should be simple but I am very confused by you logic. You get all sociolog which are all that are voted on? And then want to check if they are voted on?

If you make a list of people with brown hair, then how would you use that list to check if they have brown hair? They will all have brown hair

And the naming is strange. Normally a model that has something to do with votes would be named as such

spAo's avatar
Level 1

@Sinnbeck yes but there will be only one quesiton_id and token in table yes? example question:1 token: zRUQvql4p6rEw57vYg7 / question:2 token: zRUQvql4p6rEw57vYg7

spAo's avatar
Level 1

@Sinnbeck i just want to hide button if person will vote so he can't vote again in same question and i was thinking that i can hide button if in socialogs table already exists record with question and token then it must hide button maybe i'm explaining it incorrectly because of my bad english...

Sinnbeck's avatar

If you used they questions table to cross reference then that would be something else

Sinnbeck's avatar

It might be a good idea to take a step back and create a fresh thread where you explain what you are trying to do overall, how you plan to solve it and ask for help on how to do that

1 like

Please or to participate in this conversation.