I am new to Laravel (version 8) and i am trying to figure out why my cookie is returning a null. I am setting up the cookie like this'
public function setCookies($value){
$minutes = 60;
$response = new Response('Hello World');
$response->withCookie(cookie('name', $value, $minutes));
return $response;
}
where $value is the string value of the cookie
and i am trying to get the cookie with this method
public function getCookies(Request $request) {
$value = $request->cookie('name');
return $value;
}
but the return value is always null. please let me know where i am going wrong
here are my routes
Route::get('/cookie/set','App\Http\Controllers\Cont#setCookies');
Route::get('/cookie/get','App\Http\Controllers\cont#getCookies');
you have to change code to this :
public function setCookie(Request $request){
$minutes = 60;
$response = new Response('Set Cookie');
$response->withCookie(cookie('name', 'MyValue', $minutes));
return $response;
}
You need to change the route to accept dynamic value.
If you're sending values from url, then set the route to
Route::get('/cookie/set/{value}','App\Http\Controllers\Cont#setCookies');
Related
i am creating a website which needs to store user info and save to cookie.
here is my get cookie controller
public function index($id, Request $request)
{
$petitions = Petitions::where('id',$id)->first();
$sign->name = $request->cookie('name');
$sign->email = $request->cookie('email');
return view('petitions.index', compact('petitions'))->with(['name'=>$sign->name,'email'=>$sign->email]);
}
and here is set cookie controller
public function store(Request $request)
{
$sign = new ActionUsers();
$sign->name = $request->get('name');
$sign->email = $request->get('email');
$sign->job = $request->get('job');
$sign->reason = $request->get('reason');
$sign->ip_address = $request->get('ip_address');
$sign->petition_id = $request->get('petition_id');
$sign->is_anonymous = $request->get('is_anonymous');
$sign->save();
$name_cookie = cookie('name', $sign->name, 30);
$email_cookie = cookie('email', $sign->email, 30);
return back()->withCookie($name_cookie,$email_cookie);
}
my view to check cookie existed
#if( isset($sign->name) && isset($sign->email) )
done!
#else Oops! Error
and return error Creating default object from empty value in $sign->name = $request->cookie('name');
$sign->email = $request->cookie('email');
anyone help me solve that! thank you
Try return in store function as :
return back()->withCookie($name_cookie)->withCookie($email_cookie);
There are several ways to do this:
$response = new \Illuminate\Http\Response(view('your_view'));
$response->withCookie(cookie('cookieName' , 'cookieValue' , expires_in_minutes));
return $response;
or
\Cookie::queue('cookieName', 'cookieValue', expires_in_minutes);
return view('your_view');
you can also use this code to get cookies :
$request->cookie('coockieName');
I have simple question here.
$order = Order::where('unique_id', $id)->first();
$canPickup = $order->canPickup();
$order->payment_id = Input::get('payment_id');
$order->save();
$order2 = Order::where('unique_id', $id)->first();
$canPickup; //return false
$order->canPickup(); //return false
$order2->canPickup(); //return true
Why $order->canPickup(); differs form $order2->canPickup(); how I can achieve this function to return new value without creating new instance? Thanks
Update:
In canPickup() method i use paymentType relationship:
public function paymentType()
{
return $this->belongsTo('PaymentType', 'payment_id');
}
And in same method I have these results. Why these values differs?
Debugbar::warning($this->payment_id); //return 3
Debugbar::warning($this->paymentType->id); //return old value 1.
I am trying to send from Laravel a response to an AJAX post request.
public function infoRoute(Request $request)
{
// Get info
$ship_id = $request->ship_id;
$startDate = $request->datepicker_start;
$endDate = $request->datepicker_end;
// Get all the locations between those dates
$routeArray = $this->measurementRepository->getCoordinates($ship_id, $startDate, $endDate);
$ship = $this->shipRepository->getShipForId($ship_id);
$info = $this->createRouteArrayForShip($ship, $routeArray);
if($request->ajax()) {
return response()->json(json_encode($info));
}
}
protected function createRouteArrayForShip($ship, $routeArray)
{
$info['type'] = "showRoute";
$index = 0;
foreach($routeArray as $coordinates)
{
$info['info']['route']['loc'. $index] = $coordinates;
$index++;
}
$info['info']['shipInfo'] = $ship;
//dd($info);
return $info;
}
When I receive the information and process it with jQuery, everything shows except from the route, that is empty.
Thank you,
The response()->json() method converts the given array into JSON using the json_encode() PHP function behind the scene.
Therefor you should remove your json_encode() from inside the response()->json() call.
Basically it should look like this
return response()->json($info);
I have a function that I use to get the user id of Auth component. It works fine without use return json_encode. The problem is that I need this works with json_encode because I get values from ajax request.
Using json_encode it always return null to id and I can't understand why does it occur. The problem is with the function indexAjax() below.
How could I use $this->Auth->user("id") with json_encode and it not return null ?
Trying.
//using $this->set it works fine
public function index() {
$id = $this->Auth->user("id");
$empresas = $this->Empresa->find('all', array(
'fields'=>array("id", "nomeFantasia", "cnpj",
"telefone1", "telefone2", "celular", "aberto"),
'conditions'=>array("users_id = "=> $id)
));
debug($id) or die;
//$this->set(compact('empresas'));
}
//with json_encode always return null
public function indexAjax() {
$this->autoRender = false;
$id = $this->Auth->user("id");
$empresas = $this->Empresa->find('all', array(
'fields'=>array("id", "nomeFantasia", "cnpj",
"telefone1", "telefone2", "celular", "aberto"),
'conditions'=>array("users_id = "=> $id)
));
return json_encode($id);
}
solved the problem. My solution was when user make login I get the user id and write in session so when I need this id I get from session directly and not from AuthComponent.
It works.
I've set a few cookies in a Controller action and then in another action I want to read the cookie set and do something with the value. However, when trying to read the cookies, all i see is an empty array, my code is as follows:
public function testSetCookieAction()
{
$value = 'ABCDEFGHI'
$cookie = new Cookie('SYMFONY2_TEST', $value, (time() + 3600 * 24 * 7), '/');
$response = new Response();
$response->headers->setCookie($cookie);
$response->send();
.
.
.
}
public function testReadCookieAction()
{
$response = new Response();
$cookies = $response->headers->getCookies();
// $cookies = array(0) { }
}
When i var_dump($_COOKIE);, I see array(1) { ["SYMFONY2_TEST"]=> string(9) "ABCDEFGHI" } Does anybody know what I am doing wrong?
Thanks in advance
You must read cookies on the Request object, not on the void Response object you just created ;)
public function testReadCookieAction(Request $request)
{
$cookies = $request->cookies;
if ($cookies->has('SYMFONY2_TEST'))
{
var_dump($cookies->get('SYMFONY2_TEST'));
}
}