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

sudhir.sharma101's avatar

Laravel 8 - 400 Bad Request Error

I am facing problem in Laravel 8, a 400 Bad Request Error page displaying randomly at every page, but when I refresh the page, It will disappear and that page working fine. Please guide me why this is happening and what will be solution to remove or handle this error.

https://tinyurl.com/yg48zwdh

0 likes
11 replies
gitwithravish's avatar

Not able to understand the context. Provide more information

automica's avatar

@sudhir.sharma101 can you post the route, the controller method and the blade for the route you are hitting which is generating the 400 error.

sudhir.sharma101's avatar
//API Transactions Routes

Route::get('/api_transactions', [ApiTransactionController::class, 'index'])->middleware('auth')->name('api_transactions');
Route::get('/api_transactions/{id}', [ApiTransactionController::class, 'getById'])->middleware('auth');


//ApiTransactionController method - 


	public function index(Request $request){
		$data['page'] ='api_transactions';
		$data['api_transactions'] = ApiTransaction::orderBy('transactionId','desc')->get();
		return view('api_transactions.index',$data);
	}


//api_transactions.index view - 


<div class="container">
	<div class="title_box">
		<h1 class="primary_title">API Transactions</h1>
	</div>
    <div class="row justify-content-center">
        <div class="col-md-12">			
        <table id="example" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th>Transaction ID</th>
                <th>Platform</th>
                <th>API Type</th>
                <th>Method Name</th>
                <th>Response Code</th>
				<th>Response Message</th>
				<th>Created At</th>
				<th>Action</th>
            </tr>
        </thead>
        <tbody>
	if($api_transactions)
		foreach($api_transactions as $val)
            <tr if($val->response_code == '200') class="success-row" else class="error-row" endif >
                <td>{{$val->transactionId}}</td>
				<td>{{$val->plateform}}</td>
				<td>{{$val->api_type}}</td>
				<td>{{$val->method_name}}</td>
				<td>{{$val->response_code}}</td>
				<td>{!!$val->response_message!!}</td>
				<td>{{date('d-m-Y H:i:s',strtotime($val->created_at))}}</td>
				<td>
					<a class="btn-link" href="{{url('/api_transactions')}}/{{$val->transactionId}}" title="View API Transaction Detail" ><i class="fa fa-eye" aria-hidden="true"></i></a>
					
					if($val->response_code != '200')
						
						<a class="btn-link ml-3" href="{{url('/edit_api_call/')}}/{{$val->transactionId}}" title="Re-Submit API Call" ><i class="fa fa-pencil" aria-hidden="true"></i></a>
						
					else
						<a class="btn-default ml-3" href="javascript:void(0);" style="color:gray;cursor: no-drop;"><i class="fa fa-pencil" aria-hidden="true"></i></a>
					endif
				</td>
            </tr>	
		endforeach
	endif
        </tbody>
        <tfoot>
            <tr>
                <th>Transaction ID</th>
                <th>Platform</th>
                <th>API Type</th>
                <th>Method Name</th>
                <th>Response Code</th>
				<th>Response Message</th>
				<th>Created At</th>				
				<th>Action</th>
            </tr>
        </tfoot>
    </table>
	
        </div>
    </div>
</div>
automica's avatar

Can you reformat the code above using 3 ticks above and below each code block?

As this issue looks like a problem with the data you are passing, I would suggest you add some logging at the top of your method which should help you see what the data looks like before your server issues the 400 response.

Ideally you should write a feature test for this in php unit which will allow you to mock the data coming into the method. This would allow you to consistently reproduce your 400 response and then you’ll be able to tweak your code until it provides the correct response you want.

jlrdw's avatar

Is something being malformed during the request, or are you getting too many requests.

frankielee's avatar

Just curious, your sessions are using the file driver? Try to comment out the codes that have used the session() helper. Wondering it might be your files/folder permissions issue.

sudhir.sharma101's avatar

Yes. I commented that in config/session.php file and then I got this error -

RuntimeException Session store not set on request.

But I don't think it is file/folder permissions issue.

frankielee's avatar

Maybe just try to comment out the codes line by line.

automica's avatar

@sudhir.sharma101 i suspect you issue may arise from using an array before declaring it.

eg

	public function index(Request $request){
		$data['page'] ='api_transactions';
		$data['api_transactions'] = ApiTransaction::orderBy('transactionId','desc')->get();
		return view('api_transactions.index',$data);
	}

you are setting $data['page']before declaring data. Also you are passing in request but i can't see it being used.

so a better refactor for index would be as follows:

public function index()
{
    $data = [
        'page' => 'api_transactions',
        'api_transactions' => ApiTransaction::orderBy('transactionId', 'desc')->get(),
    ];
    return view('api_transactions.index', $data);
}

also, your blade file is missing the @ symbols so should look like:

<div class="container">
    <div class="title_box">
        <h1 class="primary_title">API Transactions</h1>
    </div>
    <div class="row justify-content-center">
        <div class="col-md-12">
            <table id="example" class="table table-striped table-bordered" style="width:100%">
                <thead>
                <tr>
                    <th>Transaction ID</th>
                    <th>Platform</th>
                    <th>API Type</th>
                    <th>Method Name</th>
                    <th>Response Code</th>
                    <th>Response Message</th>
                    <th>Created At</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>
                @if($api_transactions)
                @foreach($api_transactions as $val)
                <tr @if($val->response_code == 200) class="success-row" @else class="error-row" @endif >
                    <td>{{$val->transactionId}}</td>
                    <td>{{$val->plateform}}</td>
                    <td>{{$val->api_type}}</td>
                    <td>{{$val->method_name}}</td>
                    <td>{{$val->response_code}}</td>
                    <td>{!!$val->response_message!!}</td>
                    <td>{{date('d-m-Y H:i:s',strtotime($val->created_at))}}</td>
                    <td>
                        <a class="btn-link" href="{{url('/api_transactions')}}/{{$val->transactionId}}" title="View API Transaction Detail" ><i class="fa fa-eye" aria-hidden="true"></i></a>

                        @if($val->response_code != 200)

                        <a class="btn-link ml-3" href="{{url('/edit_api_call/')}}/{{$val->transactionId}}" title="Re-Submit API Call" ><i class="fa fa-pencil" aria-hidden="true"></i></a>

                        @else
                        <a class="btn-default ml-3" href="javascript:void(0);" style="color:gray;cursor: no-drop;"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                        @endif
                    </td>
                </tr>
                @endforeach
                @endif
                </tbody>
                <tfoot>
                <tr>
                    <th>Transaction ID</th>
                    <th>Platform</th>
                    <th>API Type</th>
                    <th>Method Name</th>
                    <th>Response Code</th>
                    <th>Response Message</th>
                    <th>Created At</th>
                    <th>Action</th>
                </tr>
                </tfoot>
            </table>

        </div>
    </div>
</div>
1 like

Please or to participate in this conversation.