Laravel - Blade templates get the POST response

0

I have a blade template that is a user creation form that calls a function of a controller that performs the user's insertion into the database.

This driver has a code like this (the code works and the user is inserted into the DB)

    /**
 * Parses, validate and create a new resource.
 *
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function storeUser(Request $request)
{
    $roles = Input::get('roles');

    $validator = app('validator')->make($request->all(), [
        'name' => 'bail|required|string',
        'email' => 'bail|required|string',
        'status'=> 'bail|boolean',
        'created_at' => 'bail|datetime',
        'created_by' => 'bail|integer',
    ]);

    if ($validator->fails()) {
        return $this->jsonValidatorFailedResponse($validator->errors());
    };


    $userCreated = User::create($data->toArray());

    return $this->jsonCreatedResponse($userCreated);

}

Then I have the template that is the form from which the user enters the data that is how it is

@extends('layouts.app')

@section('title', trans('messages.User Create'))

@section('content')
    <div id="semester-create" class="row">
        <div class="col-md-12">
            <div class="jumbotron">
                <div class="row">
                    <div class="col-md-12">
                        <div id="section-title" class="h2">{{ trans('messages.User Create') }}</div>
                    </div>
                </div>
                <hr class="my-3">
                <div id="section-body">
                    <form id="section-form" action="{{ route('admin.users.store') }}" method="post">
                        <div class="row">
                            <div class="col-md-6">
                                <label for="identifier">@lang('messages.name') *</label>
                                <input type="text" id="name" class="form-control" name="name" aria-describedby="name_help" data-validation="required" />
                            </div>
                            <div class="col-md-6 form-group">
                                <label for="qr_code">@lang('messages.email')</label>
                                <input type="text" id="email" class="form-control" name="email" aria-describedby="email_help" data-validation="required" />
                            </div>
                            <div class="col-md-6 form-group">
                                <label for="enabled_check">¿Activar usuario?</label>
                                <div id="enabled_check" class="form-check">
                                    <input type="checkbox" id="status" class="form-check-input" name="status" value="1" aria-describedby="status_help">
                                    <label class="form-check-label" for="enabled">@lang('messages.Enabled')</label>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <input class="btn btn-primary" type="submit" value="@lang('messages.Save')"/>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

What it does is show a form where the user enters the data and makes a call to the function that I have previously set.

The previous function returns a JSON because the idea is to be able to reuse that method in other templates, so my question is from this template how can I make the POST do the action and capture the response of the function and can show something by screen or redirect.

Thanks

    
asked by edica 08.08.2018 в 09:58
source

2 answers

0

What happens is that you are returning the data as JSON instead of a view. This is useful when Laravel is used as a backend offering services to perform actions on the server so that the frontend can make HTTP queries without status.

In the mode that you are working I see that you are using Laravel for back (business logic) and also for the frontend so it is usual that the views are rendered from the server (return views instead of data - json , xml ) then to solve what you say you can do it in two different ways:

Solution A

Keep your work mode but returning views from the controller instead of data.

/**
 * Parses, validate and create a new resource.
 *
 * @param Request $request
 */
public function storeUser(Request $request)
{
    $roles = Input::get('roles');

    $validator = app('validator')->make($request->all(), [
        'name' => 'bail|required|string',
        'email' => 'bail|required|string',
        'status'=> 'bail|boolean',
        'created_at' => 'bail|datetime',
        'created_by' => 'bail|integer',
    ]);

    if ($validator->fails()) {
        return $this->jsonValidatorFailedResponse($validator->errors());
    };

    $userCreated = User::create($data->toArray());

    return $this->view('nombre_de_tu_view')->withUser($userCreated);
    // de este modo puedes utilizar el usuario creado en tu vista desde
    // la variable $user
}

This does not fully meet what you expect to do: reuse the method for other views. So the strategy you can take is to create a class that manages this logic (like a repository or trait) so that from the methods of your controllers you only invoke that class for the creation of users, returning the data you want along with the views what you want.

Solution B

Perform queries to your server from the frontend through stateless HTTP calls through a library such as Axios .

This way your controllers only return the data that you are currently returning ( $userCreated ) to any view of your frontend when you want it. Here is a guide that will give you a more detailed guideline .

    
answered by 08.08.2018 в 15:37
-1

In your role, place a dd($request);

the dd what it does is show you what you are sending by parameter.

then if you want to return this you should just use

return redirect('/url')->with('variable_en_vista', $request);

I hope this helps you.

    
answered by 08.08.2018 в 13:31