What is the issue exactly; the docs are quite clear on writing to, and retrieving from the session https://laravel.com/docs/5.7/session#storing-data
Oct 25, 2018
3
Level 1
Put post request in session
Hi, I want to do the following:
Post my request in to a session this is for a shopping cart. I want to post the id of the product and the quantity in the session.
This is my view:
@extends('layouts.app')
@section('content')
<div class="container">
<h1>Speelgoed:</h1>
@if(Session::has('id', 'number'))
<div class="alert alert-success">
{{Session::get('id', 'number')}}
</div>
@endif
@foreach ($products as $product)
<ul>
<li><h3>{{ $product->name }}</h3></li>
<li>{{ $product->description }}</li>
<li>€ {{ $product->price }}</li>
<li>Op voorraad: {{ $product->amount }}</li>
<form method="post" action="{{url('categories\{id}')}}">
@csrf
<div class="col-md-4">
<label for="amount">Aantal:</label>
<input type="number" name="number">
<input type="hidden" id="id" name="id" value=id>
</div>
<button type="submit" class="btn btn-success">Add product</button>
</form>
{{var_dump(Session::get('cart'))}}
</ul>
@endforeach
</div>
@endsection
This is my controler:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
use \App\Product;
class ShopController extends Controller
{
public function index()
{
$categories = Category::with('products')->get();
return view('shop.index', compact('categories'));
}
public function category($id)
{
$products = Category::find($id)->first()->products;
return view('shop.1', compact('products'));
}
public function add(Request $request, $id, $number)
{
$request->input('id', $id);
$request->input('number', $number);
return redirect('categories/'.$id);
}
}
These are my routes:
Route::get('/cart/{id}/{number}', 'ShopController@add');
Route::post('/cart/{id}/{number}', 'ShopController@add');
Please or to participate in this conversation.