Shivam
Shivam

Reputation: 3131

From request redirecting to same page (not working). laravel 5.4

I am using from request named StoreAdminRequest to validate my form but nothing is happening. It redirects to the same page. I can't figure out whats wrong. However when I dd in rules() function it does enter in that function.

StoreAdminRequest.php

namespace App\Http\Requests\Admin;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;


/**
 * Class StoreAdminRequest.
 */
class StoreAdminRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // return access()->hasRoles([2,6]);
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
                'first_name'     => 'required|max:191',
                'last_name'  => 'required|max:191',
                'email'    => 'required', 'email', 'max:191', Rule::unique('users','email'),
                'mobile'  => 'required'
            ];
    }

}

AdminController.php

public function store(StoreAdminRequest $request)
    {
        $admin = $this->_admin->create($request->all());
    }

Upvotes: 2

Views: 883

Answers (2)

Shivam
Shivam

Reputation: 3131

I needed to loop through the $errors variable. thanks

@if ($errors->any())
       <div class="alert alert-danger">
           <ul>
               @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
               @endforeach
           </ul>
       </div>
    @endif

Upvotes: 2

Spears
Spears

Reputation: 2208

If you simply want to redirect to a different route you should use

return redirect()->route('/desired/route/to/redirect');

in your Controller.

Your actual problem is caused by a missing RedirectResponse to change the desired page you get from the server. If no other route is defined, you will always get redirected to the page you made the request.

Upvotes: 0

Related Questions