简体   繁体   中英

How to store input in session Laravel

I'm new in Laravel Here I am trying to post my input form in to a session but its not working I get this error without any message:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

I found nothing, here I am sharing some of my code.

My Controller:

<?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)->products;
        return view('shop.1', compact('products'));
    }

    public function addToShoppingCart(Request $request)
    {
        $request->session()->put('cart', 'id');
        $request->session()->put('cart', 'number');

        $request->session()->flash('status', 'Product is toegevoegd!');
        return redirect()->back();
    }
}

My view:

@extends('layouts.app')

@section('content')
    @if(Session::has('id', 'number'))
        <div class="alert alert-success">
            {{Session::get('id', 'number')}}
        </div>
    @endif
    @foreach ($products as $product)
        <ul>
            <li>{{ $product->name }}</li>
            <li>{{ $product->description }}</li>
            <li>{{ $product->price }}</li>
            <li>{{ $product->amount }}</li>
        </ul>
        <form method="post" action="{{url('categories\{id}')}}">
            @csrf
            <div class="col-md-4">
                <label for="number">Aantal:</label>
                <input type="number" name="number">

                <label for="id">Id:</label>
                <input type="text" id="id" name="id" value= {{$product->id}}>

                <button type="submit" class="btn btn-success">Add product</button>
            </div>
        </form>
    @endforeach
    {{var_dump(Session::get('cart'))}}

@endsection

My routes:

Route::get('/shop', 'Shopcontroller@index')->name('shop');
Route::get('/categories/{id}', 'ShopController@category');
Route::get('/cart/{id}', 'ShopController@addToShoppingCart');

I hope someone can help me out with my problem to put the input in the session of laravel.

You are trying to "post" using a "get" route. You should change Route::get by Route::post

First of all, i suggest you use the route() method. There is nothing wrong with the url() method but i just using the route() method.

So let's solve your problem out!

1. Add names to your routes and set the ROUTE to POST instead of GET

Route::get('/shop', 'Shopcontroller@index')->name('shop');
Route::post('/categories/{id}', 'ShopController@category')->name('category');
Route::get('/cart/{id}', 'ShopController@addToShoppingCart')->name('cart.add');

2. Pass your route as an action to your form

action="{{route('category',['id' => 'your_category_id_here'])}}

3. Add use Session;

Your controller is missing the use Session; statement, add it on the top of the controller page so you can be able to use sessions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM