Remove an item from an array in Laravel’s Session

Asked

Viewed 980 times

2

I have an array in a Session and I need to remove only one item. Put

session()->forget('cart') 

it removes all items. What is the correct way to remove only one?

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)
{
    $product = $request->all();
    session()->push('cart', $product);
    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>Alterar</th>
                            <th>Excluir</th>
                        </tr>
                    </thead>
                    <tbody class="row">
                        @if(session()->get('cart'))
                            @foreach(session()->get('cart') as $key => $product)  
                                <tr>
                                    <td>{{ $product['name'] }}</td>  
                                    <td><a href="#">Alterar</a></td>    
                                    <td><a href="{{ session()->forget('key') }}">Excluir</a></td>                         
                                </tr>
                            @endforeach
                        @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>

2 answers

3


First recover the Cart, exclude the array by the key and then destroy and build again the Cart.

Method:

public function exclusao($chave)
{
    $carts = session()->get('cart');
    unset($carts[$chave]);
    session()->forget('cart');
    session()->put('cart', $carts);
    return redirect()->back();
}

Route:

Route::get('product/exclusao/{chave}', 'ProductController@exclusao');

Html

<td>
    <a href="/product/exclusao/{{ $key }}">Excluir</a>
</td>

References:

-2

unset(session()->get('cart')['INDICE_A_SER_REMOVIDO'])

As you are making a loop in each iteration then in foreach:

foreach(session()->get('cart') as $key => $product)

... href="{{ unset(session()->get('cart')[$key]) }}">Excluir<...
  • because the downvote?

  • Well it was not the one who voted negative, but, the code so is in trouble, I believe it is this, can not in a link for example make a command PHP, <a> server to link @Rafaelacioly ...

  • I believe that the href="..." is an example of what would be inserted in view using the blade, is basically a summary

Browser other questions tagged

You are not signed in. Login or sign up in order to post.