I'm working on a Laravel project about how to generate a QR Code with a hashed information. However, when I call the variable from my blade file, it says 'Undefined variable $qrcode'. May I get some help?
This is how my Controller (PostController) looks like:
public function index()
{
$post = Post::all();
return view('blog.index', compact('post'));
}
public function create()
{
return view('blog.create');
}
public function store(Request $request)
{
$post = new Post;
$post->randomnumber = random_int(100000, 999999);
$post->user_id = Auth::id();
$post->unit = $request->input('unit');
$post->guestname = $request->input('guestname');
$post->guestphone = $request->input('guestphone');
$post->guestemail = $request->input('guestemail');
$post->guestic = $request->input('guestic');
$post->guestcarplate = $request->input('guestcarplate');
$post->numberofguests = $request->input('numberofguests');
$post->date = $request->input('date');
$post->time = $request->input('time');
$post->hash = Hash::make($post->randomnumber);
$post->save();
return view('blog.qrcode');
}
public function generate ($id)
{
$post = Post::findOrfail($id);
$qrcode = QrCode::size(300)->generate($post->hash);
return view('blog.qrcode', compact('qrcode'));
}
This is how my Route looks like:
Route::get('posts/create', 'PostController@create')-
>middleware('auth');
Route::post('posts', 'PostController@store');
Route::get('/qrcode/{id}', function ($id) {
return redirect('blog.qrcode');
});
And this is how my blade file (qrcode.blade.php) looks like:
@extends('layouts.frontend')
@section('content')
<h4>
QR Code
</h4>
{!! $qrcode !!}
@endsection
try this:
Route::get('/qrcode/{id}', 'PostController@generate');
instead of:
Route::get('/qrcode/{id}', function ($id) {return redirect('blog.qrcode');});
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.