0

Laravel Controller my insert data method in controller

$category = array(
   'name' => $request->name,
   'code' => $request->code,
   'image' => $image_name
);
     Category::create($category,$image_name);

            return Redirect::to('/category')->with('success','Record 
                  inserted successfully');

laravel view index view where I want to display message

             @if($message = Session::get('success'))
                           <div class="alert alert-success">
                             <p>{{$message}}</p>
                          </div>
              @endif

Route.php Route::resource('/category', 'CategoryController');

Qaiser
  • 63
  • 1
  • 7
  • 2
    This isn't enough for us to go on. As far as we know, that session variable hasn't been set. Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and edit your question accordingly. – M. Eriksson Aug 31 '20 at 08:13
  • What version of Laravel are you using? Do you have `StartSession` middleware applied to your routes? – Ivanka Todorova Aug 31 '20 at 08:25
  • you can use `flash` in controller to define `Session` like this: `\Session::flash('success', 'text');` and then return with no `with`. – Reza sh Aug 31 '20 at 08:25
  • please describe in detail how to do it?? thanks in advance – Qaiser Aug 31 '20 at 13:30

4 Answers4

0

Try something like this:

<div class="panel panel-default">
    @if (session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
</div>
Rouhollah Mazarei
  • 3,969
  • 1
  • 14
  • 20
0

try:

@if(session()->has('success'))
    <div class="alert alert-dismissable alert success" style="background: white;">
        <button type="button" class="close" data-dismiss="alert" aria-label="close">
            <span aria-hidden="true">&times;</span>
        </button>
        <strong>
            {!! session()->get('success') !!}
        </strong>
    </div>

@endif
0

Try this, is should work.

Route

Route::get('category', 'CategoryController@index')->name('category');

    $category = array(
        'name' => $request->name,
        'code' => $request->code,
        'image' => $image_name
    );
    Category::create($category, $image_name);

    return redirect()->route('category')->with('success', 'Record inserted successfully');

Success Message View

@if($message = Session::get('success'))
<div class="alert alert-success">
    <p>{{$message}}</p>
</div>
@endif
Binay7587
  • 15
  • 1
  • 5
  • I am doing with resource route like below .Route::resource('/category', 'CategoryController'); – Qaiser Aug 31 '20 at 12:46
  • Then use return redirect()->route('category.index')->with('success', 'Record inserted successfully'); like this – Binay7587 Aug 31 '20 at 13:49
0

Hi every one and thanks alot for replying me I got solution to my stated problem from below link Laravel 5.2 Validation Error not appearing in blade

Qaiser
  • 63
  • 1
  • 7