I'm setting cookie on some click event. Then after storing value in cookie, I want to
- Check for existence of cookie
- Get cookie values
I have developed a function by referring Laravel official docs. Console shows that cookies have been set. But after that, I can not solve two point (mentioned in above list) for view(Blade Template). It always shows (Cookie::get('cookie.clients')) 'null'. But browser console displays that cookie
.
If anyone knows answer, it will be appreciated.
Here is my code.
Controller
use App\Client;
use App\Http\Requests;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Repositories\ClientRepository;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
class ClientController extends Controller
{
public function cookieadd(Request $request, Client $client)
{
$clients = [];
if (Cookie::get('cookie.clients') != null)
{
$clients = Cookie::get('cookie.clients');
}
array_push($clients, $client);
Cookie::forever('cookie.clients', $clients);
return redirect('/client');
}
}
View
@if (Cookie::get('cookie.clients') != null)
<p>cookie is set</p>
@else
<p>cookie isn't set</p>
@endif