<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Syntax Byte Solutions</title>
    <description>The latest articles on Forem by Syntax Byte Solutions (@syntaxbytesolution).</description>
    <link>https://forem.com/syntaxbytesolution</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1120279%2F5a3390f0-944b-4712-9c42-00ecef2dfb95.jpeg</url>
      <title>Forem: Syntax Byte Solutions</title>
      <link>https://forem.com/syntaxbytesolution</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/syntaxbytesolution"/>
    <language>en</language>
    <item>
      <title>Page keep redirecting back to the same page while it should be redirected to another page in Laravel Project</title>
      <dc:creator>Syntax Byte Solutions</dc:creator>
      <pubDate>Sat, 15 Jul 2023 13:32:27 +0000</pubDate>
      <link>https://forem.com/syntaxbytesolution/page-keep-redirecting-back-to-the-same-page-while-it-should-be-redirected-to-another-page-in-laravel-project-3jj1</link>
      <guid>https://forem.com/syntaxbytesolution/page-keep-redirecting-back-to-the-same-page-while-it-should-be-redirected-to-another-page-in-laravel-project-3jj1</guid>
      <description>&lt;p&gt;I have a method that should collect test answers (Yes or No) from the form, calculate the results, and insert them into the Database Table. But once the user clicks on submit, it will redirect back to the same question page. Below is the code:&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
public function storeAnswer(Request $request, $id) {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    $questions = Question::with('category')-&amp;gt;get();

    if ($request-&amp;gt;isMethod('post')) {
        $answers = $request-&amp;gt;input('answer');

        //$request-&amp;gt;dd();

        $passCountByCategory = [];

        foreach ($questions as $question) {
            $answer = $answers[$question-&amp;gt;id] ?? null;

            if ($answer === 'Yes') {
                $categoryId = $question-&amp;gt;category-&amp;gt;id;
                $passCountByCategory[$categoryId] = ($passCountByCategory[$categoryId] ?? 0) + 1;
            } elseif ($answer === 'No') {
                $categoryId = $question-&amp;gt;category-&amp;gt;id;
                $passCountByCategory[$categoryId] = ($passCountByCategory[$categoryId] ?? 0);

            } else {

                return redirect()-&amp;gt;back()-&amp;gt;with('error', 'You did not choose any options');
            }
        }

        $overallPassed = 0;
        $overallTotal = 0;

        foreach ($passCountByCategory as $categoryId =&amp;gt; $passCount) {
            $category = Category::find($categoryId);
            $categoryTotal = count($category-&amp;gt;questions);
            $categoryPassed = $passCount;

            $categoryResult = new CategoryResult();
            $categoryResult-&amp;gt;result_id      = $id;
            $categoryResult-&amp;gt;category_name  = $category-&amp;gt;name;
            $categoryResult-&amp;gt;pass_count     = $categoryPassed;
            $categoryResult-&amp;gt;total_count    = $categoryTotal;
            $categoryResult-&amp;gt;percentage     = ($categoryPassed / $categoryTotal) * 100;
            $categoryResult-&amp;gt;category_status = ($categoryPassed &amp;gt;= 2) ? 'Passed' : 'Failed';
            $categoryResult-&amp;gt;save();

            $overallPassed += $categoryPassed;
            $overallTotal += $categoryTotal;
        }

        $overallPercentage = ($overallPassed / $overallTotal) * 100;
        $overallStatus = ($overallPercentage &amp;gt;= 66) ? 'Passed' : 'Failed';

        $testResult = new TestResults();
        $testResult-&amp;gt;app_id         = $id;
        $testResult-&amp;gt;pass_count     = $overallPassed;
        $testResult-&amp;gt;total_count    = $overallTotal;
        $testResult-&amp;gt;percentage     = $overallPercentage;
        $testResult-&amp;gt;status         = $overallStatus;
        $testResult-&amp;gt;save();

        $app_name = UserApps::where('app_id', $id)-&amp;gt;value('app_name');

        $appStatus = ($overallStatus === 'Passed') ? 'Certified' : 'Unapproved';
        UserApps::where('app_id', $id)-&amp;gt;update(['status' =&amp;gt; $appStatus]);

        return redirect()-&amp;gt;route('test.results', ['id' =&amp;gt; $id]);
    }

}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What could have been the issue?&lt;/p&gt;

&lt;p&gt;Apparently, this is the getResults method:&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
public function getResults($id) {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    $testResult = TestResults::findOrFail($id);
    $categoryResults = CategoryResult::where('result_id', $id)-&amp;gt;get();

    return view('result_view', compact('testResult', 'categoryResults'));

}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here are my two routes:&lt;br&gt;
`&lt;br&gt;
Route::post('/user/store-results/{id}', [QuestionController::class, 'storeAnswer'])-&amp;gt;name('store_results');&lt;/p&gt;

&lt;p&gt;Route::get('/user/results-test/{id}', [QuestionController::class, 'getResults'])-&amp;gt;name('test.results');`&lt;/p&gt;

&lt;p&gt;I could not figure out what could be the problem.&lt;/p&gt;

&lt;p&gt;I $request-&amp;gt;dd(), got the answer from the form: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--90KU3gVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqelmhturcakiouoke24.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--90KU3gVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqelmhturcakiouoke24.png" alt="Image description" width="399" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
  </channel>
</rss>
