简体   繁体   中英

How can I send a Success Message with compact in Laravel

I'm trying to send a success message with a compact but it didn't work for me a message does not appear?

public function index(Request $request)
{
    $brand=$request->get('searchbrand');
    $model=$request->get('searchmodel');
    $cars=car::where('brand','like','%'.$brand.'%')
    ->where(function ($query)use($model) {
        $query->where('model','like','%'.$model.'%');
    })->get();
    return view('cars.index',compact('cars'))
    ->with('success','Results Car');
}

You can use compact() or with() , but generally not both.

Here's some options:

  1. Use ->with() with a single array :
return view('cars.index')->with(['cars' => $cars, 'success' => 'Results Car']);
  1. Chain ->with() calls:
return view('cars.index')->with('cars', $cars)->with('success', 'Results Car');
  1. Pass an array instead of with() or compact() :
return view('cars.index', ['cars' => $cars, 'success' => 'Results Car']);
  1. Use compact() with a $success variable:
$success = 'Results Car';
return view('cars.index', compact('cars', 'success'));

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