Put variable in session in Laravel

2

I am registering a name in a modal, sending to the controller and putting in the session. Returning to the view has nothing in the session. What am I doing wrong?

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use Illuminate\Support\Facades\Session;

class ProductsController extends Controller
{

public function index()
{
    return view('product.index');
}

public function store(Request $request)
{
    $products = $request->name;
    session()->put('products', $products);
    return redirect()->back();
}

}

View

<div class="container">
<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">Produtos</div>
            <div class="panel-body">
                <table class="table m-0" id="products-table">
                    <thead>
                        <tr>
                            <th>Produto</th>
                            <th>Quantidade</th>
                        </tr>
                    </thead>
                    <tbody class="row">
                        @if(!empty($products))
                            <tr>
                                <td>{{ $products }}</td>
                                <td><a href="#">Alterar</a></td>
                                <td><a href="#">Remover</a></td>
                            </tr>
                        @endif
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="5" style="text-align: left;">
                                <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Incluir</button>
                            </td>
                        </tr>
                    </tfoot>
                </table>    
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
        </div>
        <form method="post" action="{{ route('product.store') }}">
        {{ csrf_field() }}
        <div class="modal-body">
            <input type="text" class="form-control" name="name">
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
            <button type="submit" class="btn btn-primary">Salvar</button>
        </div>
        </form>
    </div>
</div>

    
asked by anonymous 31.08.2017 / 00:18

1 answer

2

In part of controller everything is right to save an item in session() it is anyway, but in its View , it is being called as if it were a common variable, but it must be accessed at session() to search for this data, example :

<tbody class="row">
@if(session()->has('products'))
    <tr>
        <td>{{ session()->get('products') }}</td>
        <td><a href="#">Alterar</a></td>
        <td><a href="#">Remover</a></td>
    </tr>
@endif
</tbody>

References:

31.08.2017 / 01:48