0
votes

i am new to laravel

this is my code for controller

I am writing a code to save the data from the user in Mysql And then redirect me to the list page with updated table and a flash message that the data has been updated successfully

i also tried using session->('key') but it didn't work

 <?php

namespace App\Http\Controllers;

use App\restaurent_name;
use Illuminate\Http\Request;

class RestaController extends Controller
{
    function Index()
    {
        return view('home');
    }

    function list()
    {
        $data = restaurent_name::all();
        return view('list',["Data"=>$data]);
    }

     function add(Request $req)
    {
        //return $req->input();
        $save = new restaurent_name;
        $save->name=$req->input("name");
        $save->address=$req->input("address");
        $save->contact=$req->input("contact");
        $save->save();
        $save->session()->has('status');
        
                //$save->session()->put('status', 'Task was successful!');

        
        //$save->session()->flash('status','Restaurent added succesfully');
        return redirect('/list');
    }
}

the data gets saved properly into the database but the issue is that i am not able to receive message from flash session

This the error message i receive when i try to use flash session

`

 Illuminate\Database\Eloquent\Model::throwBadMethodCallException C:\xampp\htdocs\Laravel 

 Projects\Restaurants\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:50

            $pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~';

 

            if (! preg_match($pattern, $e->getMessage(), $matches)) {

                throw $e;

            }

 

            if ($matches['class'] != get_class($object) ||

                $matches['method'] != $method) {

                throw $e;

            }

 

            static::throwBadMethodCallException($method);

        }

    }

 

    /**

     * Throw a bad method call exception for the given method.

     *

     * @param  string  $method

     * @return void

     *

     * @throws \BadMethodCallException

     */

    protected static function throwBadMethodCallException($method)

    {

        throw new BadMethodCallException(sprintf(

            'Call to undefined method %s::%s()', static::class, $method

        ));

    }

}

`

1
What do you want to achieve? This wont work actually $save->session()->has('status'); you want to pass a flash message?sta
You are using session method like it is related to the model and the exception says that this method is undefined.Hosein Shendabadi
You may try this return redirect('/list')->with('status', 'Restaurent added succesfully');sta
the code u provided work thank u for thatRoy Alexander
but can u tell me why is the session not working, and yes i have also created the model with restaurent_name;Roy Alexander

1 Answers

1
votes

You can do that with :

return redirect('/list')
  ->with('status', 'Restaurent added succesfully')
  ->with('status2', 'Task was successful!');

With Session Flash :

Session::flash('status', 'Restaurent added succesfully');
Session::flash('status2', 'Task was successful!'); 

return redirect('/list');