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

BeginnerSoul's avatar

Api route form

Hello I don't understand what I am doing wrong. I am trying pass the information to api route but it redirects me to the main page.

I will give my source, maybe someone will see something:

HTML

<form method="POST" enctype="application/x-www-form-urlencoded" accept-charset="UTF-8" action="{!! route('blacklistvotes') !!}">
                    @method('POST')
                    {{ csrf_field() }}
                    <input type="hidden" id="vote" name="vote" value="down">
                    <input type="hidden" id="reportedcase" name="reportedcase" value="{{ $results->id }}">
                    <button type="submit" class="btn btn-danger">Remove vote</button>
</form>

API.php route:

Route::post('blvotes', ['middleware' => 'auth:api', 'uses' => 'ApiController@blacklistvotes'])->name('blacklistvotes');

ApiController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Blacklist;
use App\BlacklistVotes;

class ApiController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth:api');
  }

  function blacklistvotes(Request $request)
  {
    if ($request->all())
    {
      $voteinput = $request->input('vote');
      $reportedcaseinput = $request->input('reportedcase');
      if (!empty($voteinput) && !empty($reportedcaseinput)) {
        //Checking if reported case exists in the $caseexists

        if($caseexists)
        {
          $user = Auth::id();
          ///Checking if a vote exists in $voteexists
          if($voteinput === "up")
          {
            if (!$votexists)
            {
              ///Saving details
              return 'back()';
            }

          }

          if($voteinput === "down")
          {
            if ($votexists)
            {
              //Deleting vote
              return 'back()';
            }
          }
        }
        else {
          return redirect('blacklist');
        }

        }
        else {

          return redirect('blacklist');
        }
    }
    else {
      return redirect('index');
    }
    }
      }

Yeah did put in 'back()' where is the return. I hoped I will see that text just for testing. I have this code but I am redirected to the main page when I click to the vote button. I am logged in and I get the Auth::id() without problem so hmm it is not issue. Someone sees a problem in the code? I am trying to solve this in these 4 days but no success.

0 likes
3 replies
kalemdzievski's avatar

Did u try to put logs in between your code to see what actually happening? That might help.

But here is an idea, what might be wrong, Those redirect('blacklist') doesn't look good. For example, if your code comes to them, the blacklist route will be hit again, but this will be hit without any input, and in that case as I can see you get redirected to the index route. Look into that.

guybrush_threepwood's avatar

Hi @beginnersoul

You should start by cleaning up your code a little and avoid nested if/else statements as they make the code difficult to read (instead try to exit early). Something like this:

function blacklistvotes(Request $request)
{
	$voteinput = $request->input('vote');
	$reportedcaseinput = $request->input('reportedcase');
	
	if (empty($voteinput) || empty($reportedcaseinput)) {
		return redirect('index');
	}
	
	// Checking if reported case exists in the $caseexists
	if (!$caseexists) { 
		return redirect('blacklist');
	}
	
	$user = Auth::id();
	
	if ($voteinput == "up" && !$votexists) {
		// Saving details
	}

	if ($voteinput == "down" && $votexists) {
		// Deleting vote
	}

	return back();
}

You should also check out:

  • You're applything the auth middleware twice (once in the route and once in the constructor)
  • You're missing the logic to check wherever the case exists
  • You're missing the logic to check wherever the vote exists
  • You're missing the saving/deleting vote logic
  • The return back statements shouldn't be strings.
  • As kalemdzievski told you, you should try dd() after each block to see if you're getting the expected result.
BeginnerSoul's avatar

Thank you for the answers. :) It seems I forgot to add the "api_token" to pass in the request and therefore I was redirected to main page.

1 like

Please or to participate in this conversation.