redirect from controller not working

Published 3 weeks ago by anp24

After create function in laravel (database insertion) , controller is not redirecting to another controller function. I am using the following code - gridredirect is not working





web.php





<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', '[email protected]')->name('home');

Route::get('/gridcon','[email protected]')->name('gridcon');

Route::post('/gridconcreate','[email protected]');




mygrid.blade.php

@extends('layouts.app')

@section('content') Register New Applicant

            <div class="panel-body">
                <form class="form-horizontal" method="POST" action="gridconcreate">
                    {{ csrf_field() }}

                    <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                        <label for="name" class="col-md-4 control-label">Name</label>

                        <div class="col-md-6">
                            <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

                            @if ($errors->has('name'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('name') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    
                     <div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}">
                        <label for="address" class="col-md-4 control-label">Address</label>

                        <div class="col-md-6">
                            <input id="address" type="text" class="form-control" name="address" value="{{ old('address') }}" required autofocus>

                            @if ($errors->has('address'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('address') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    
                    <div class="form-group{{ $errors->has('contactno') ? ' has-error' : '' }}">
                        <label for="contactno" class="col-md-4 control-label">Contact No.</label>

                        <div class="col-md-6">
                            <input id="contactno" type="text" class="form-control" name="contactno" value="{{ old('contactno') }}" required autofocus>

                            @if ($errors->has('contactno'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('contactno') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>

                            @if ($errors->has('email'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    
                    
                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Register
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
    <div class="container">
        <style>
            #example_grid4 td {
                white-space: nowrap;
            }
        </style>
        <?= $grid ?>
    </div>
</div>

@stop

MyGridController.php



<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


use App\User;
use Grids;
use HTML;
use Illuminate\Support\Facades\Config;
use Nayjest\Grids\Components\Base\RenderableRegistry;
use Nayjest\Grids\Components\ColumnHeadersRow;
use Nayjest\Grids\Components\ColumnsHider;
use Nayjest\Grids\Components\CsvExport;
use Nayjest\Grids\Components\ExcelExport;
use Nayjest\Grids\Components\Filters\DateRangePicker;
use Nayjest\Grids\Components\FiltersRow;
use Nayjest\Grids\Components\HtmlTag;
use Nayjest\Grids\Components\Laravel5\Pager;
use Nayjest\Grids\Components\OneCellRow;
use Nayjest\Grids\Components\RecordsPerPage;
use Nayjest\Grids\Components\RenderFunc;
use Nayjest\Grids\Components\ShowingRecords;
use Nayjest\Grids\Components\TFoot;
use Nayjest\Grids\Components\THead;
use Nayjest\Grids\Components\TotalsRow;
use Nayjest\Grids\DbalDataProvider;
use Nayjest\Grids\EloquentDataProvider;
use Nayjest\Grids\FieldConfig;
use Nayjest\Grids\FilterConfig;
use Nayjest\Grids\Grid;
use Nayjest\Grids\GridConfig;

class MygridController extends Controller
{
    
   public function created()
   {
       $max = 10000;
         do{
        $result = floor($max*(hexdec(bin2hex(openssl_random_pseudo_bytes(4)))/0xffffffff));
    }while($result == $max);
    
     
    
        return User::create([
            'name' => $_POST['name'],
            'address' =>$_POST['address'],
            'contactno' =>$_POST['contactno'],
            'email' => $_POST['email'],
           'password' => bcrypt('abc'),
        ]);
        
        
        redirect()->action('[email protected]');
 
         
      

       
   }
   public function getExample4()
    {
         $query = (new User)
    ->newQuery();
    
    
        $grid = new Grid(
            (new GridConfig)
                ->setDataProvider(
                    new EloquentDataProvider($query)
                )
                ->setName('example_grid4')
                ->setPageSize(15)
                ->setColumns([
                    (new FieldConfig)
                        ->setName('id')
                        ->setLabel('ID')
                        ->setSortable(true)
                        ->setSorting(Grid::SORT_ASC)
                         ->setCallback(function ($val1) {
                                  return '<a href="user/edit/">edit</a>';
                            })
                        
                    ,
                    (new FieldConfig)
                        ->setName('name')
                        ->setLabel('Name')
                        ->setCallback(function ($val) {
                            return "<span class='glyphicon glyphicon-user'></span>{$val}";
                        })
                        ->setSortable(true)
                        ->addFilter(
                            (new FilterConfig)
                                ->setOperator(FilterConfig::OPERATOR_LIKE)
                        )
                    ,
                     (new FieldConfig)
                        ->setName('address')
                        ->setLabel('Address')
                        ->setCallback(function ($val) {
                            return "<span class='glyphicon glyphicon-user'></span>{$val}";
                        })
                        ->setSortable(true)
                        ->addFilter(
                            (new FilterConfig)
                                ->setOperator(FilterConfig::OPERATOR_LIKE)
                        )
                    ,
                    (new FieldConfig)
                        ->setName('contactno')
                        ->setLabel('Contact Number')
                        ->setCallback(function ($val) {
                            return "<span class='glyphicon glyphicon-user'></span>{$val}";
                        })
                        ->setSortable(true)
                        ->addFilter(
                            (new FilterConfig)
                                ->setOperator(FilterConfig::OPERATOR_LIKE)
                        )
                    ,
                    (new FieldConfig)
                        ->setName('email')
                        ->setLabel('Email')
                        ->setSortable(true)
                        ->setCallback(function ($val) {
                            $icon = '<span class="glyphicon glyphicon-envelope"></span>&nbsp;';
                            return
                                '<small>'
                                . $icon
                                . HTML::link("mailto:$val", $val)
                                . '</small>';
                        })
                        ->addFilter(
                            (new FilterConfig)
                                ->setOperator(FilterConfig::OPERATOR_LIKE)
                        )
                          ->setCallback(function ($val2) {
                                  return '<a href="user/delete/">delete</a>';
                            })
                    ,
                    
                ])
                ->setComponents([
                    (new THead)
                        ->setComponents([
                            (new ColumnHeadersRow),
                            (new FiltersRow)
                                ->addComponents([
                                    (new RenderFunc(function () {
                                        return HTML::style('js/daterangepicker/daterangepicker-bs3.css')
                                        . HTML::script('js/moment/moment-with-locales.js')
                                        . HTML::script('js/daterangepicker/daterangepicker.js')
                                        . "<style>
                                                .daterangepicker td.available.active,
                                                .daterangepicker li.active,
                                                .daterangepicker li:hover {
                                                    color:black !important;
                                                    font-weight: bold;
                                                }
                                           </style>";
                                    }))
                                        
                                ])
                            ,
                            (new OneCellRow)
                                ->setRenderSection(RenderableRegistry::SECTION_END)
                                ->setComponents([
                                    new RecordsPerPage,
                                    new ColumnsHider,
                                    (new CsvExport)
                                        ->setFileName('my_report' . date('Y-m-d'))
                                    ,
                                    new ExcelExport(),
                                    (new HtmlTag)
                                        ->setContent('<span class="glyphicon glyphicon-refresh"></span> Filter')
                                        ->setTagName('button')
                                        ->setRenderSection(RenderableRegistry::SECTION_END)
                                        ->setAttributes([
                                            'class' => 'btn btn-success btn-sm'
                                        ])
                                ])
                        ])
                    ,
                    (new TFoot)
                        ->setComponents([
                            
                            (new OneCellRow)
                                ->setComponents([
                                    new Pager,
                                    (new HtmlTag)
                                        ->setAttributes(['class' => 'pull-right'])
                                        ->addComponent(new ShowingRecords)
                                    ,
                                ])
                        ])
                    ,
                ])
        );
        $grid = $grid->render();
        return view('mygrid', compact('grid'));
    }
}

tykus
tykus
3 weeks ago (391,190 XP)

Show your code... very difficult to help you without it, but this is the correct syntax for redirecting to other controller methods:

return redirect()->action('[email protected]');

Or, with data required by the action:

return redirect()->action(
    '[email protected]', ['id' => 1]
);

EDIT: since you have since added the code

This return route('gridredirect'); is not correct since you have not named the gridredirect route: . You would need to name the route, but to be honest, I don't know why you need to define a route like that, just redirect from the controller:

// replace this:
return route('gridredirect');

//with this:
redirect()->action('[email protected]');
anp24
anp24
3 weeks ago (1,520 XP)

I have displayed my code

tykus
tykus
3 weeks ago (391,190 XP)

I have edited my answer ; )

anp24
anp24
3 weeks ago (1,520 XP)

it is on the JSON output

name "zzbxbcbx" address "xbcxcxcbx" contactno "9853223523" email "[email protected]" updated_at "2017-11-21 17:34:01" created_at "2017-11-21 17:34:01" id 2

anp24
anp24
3 weeks ago (1,520 XP)

JSON output is shown in the URL

http://localhost/CPMS/public/gridconcreate

tykus
tykus
3 weeks ago (391,190 XP)

What does that have to do with the question you are asking? The problem you have is (i) there is no route named gridredirect, and (ii) you are redirecting incorrectly in my opinion -- it results in an extra request

anp24
anp24
3 weeks ago (1,520 XP)

I have used - redirect()->action('[email protected]');

still the above JSON output is shown and is not redirecting to the Controller action

anp24
anp24
3 weeks ago (1,520 XP)

I have updated code, but still it is on the JSON output only - on the link http://localhost/CPMS/public/gridconcreate

tykus
tykus
3 weeks ago (391,190 XP)

We cannot visit a localhost link?!?!

anp24
anp24
3 weeks ago (1,520 XP)

raw json data is shown and above mentioned url

{"name":"dsgsdgsdgds","address":"sdgsdgsdg","contactno":"9812345678","email":"[email protected]","updated_at":"2017-11-21 17:48:06","created_at":"2017-11-21 17:48:06","id":1}

anp24
anp24
3 weeks ago (1,520 XP)

how can I attach the snapshot of the page

anp24
anp24
3 weeks ago (1,520 XP)

it is now working , I have changed my controller code to

User::create([ 'name' => $_POST['name'], 'address' =>$_POST['address'], 'contactno' =>$_POST['contactno'], 'email' => $_POST['email'], 'password' => bcrypt('abc'), ]);

return redirect()->back();

Please sign in or create an account to participate in this conversation.