Laravel does not see my cookies - php

I tried to set cookie (easy example):
Route::get('set', function () {
Cookie::queue(Cookie::forever('cart_id', 123));
});
Route::get('get', function (Illuminate\Http\Request $request) {
dump($request);
});
But I get in dump of $request:
Request {#40 ▼
...
+cookies: ParameterBag {#43 ▼
#parameters: array:3 [▼
"cart_id" => null
"XSRF-TOKEN" => null
"laravel_session" => null
]
}
+headers: HeaderBag {#46 ▼
#headers: array:9 [▼
...
"cookie" => array:1 [▼
0 => "cart_id=eyJpdiI6IldNbjJwMHdRclBmWkFkUFJ3SnJ6Z2c9PSIsInZhbHVlIjoiVnhBWGl3ODFBaGhwTCtSc0NoSmlLUT09IiwibWFjIjoiYjU2NjRmNDI4OGQ4NGZmZjNjMDhmYTEwMWJlOWQ2ZjUwYjAxNTMxYjU2OWY3Y2I0NjYxNzQzZTNhZjVkMDcxOSJ9; XSRF-TOKEN=eyJpdiI6Ino2ckNVSU5cL3VEcE5VQWRqQm1wTkV3PT0iLCJ2YWx1ZSI6IkF2b1Y0VDFkMlVqam8zeWVJQk55Mm96XC9PZFlyZUxma3FBTXNBV2VoK3AyTkp5Q2RNOHZtQnRyMFB0a3c0Mm5cL1hJdkkyY1haUHhZV0FEejdKSjMzaHc9PSIsIm1hYyI6IjFlNjVkZDE2NDJiYjA2MDQzM2JiYWYzMWY2YWRiNTk0ZDM4MjBlNmEzZmEwNTUxZWUzY2NmYmEyZDljMmYwOGIifQ%3D%3D; laravel_session=eyJpdiI6Imh1QW1iamlscnVpV3JFTThHYWdTNkE9PSIsInZhbHVlIjoiZElUMU1MZlRwa1BRZFd6T0pWMGhSanNCcmVNcjdEUGJrd2Zhc3RqbFUzVFBOamxsclBnbWhHcVk4QXFXU2VFMkVGYXBUSm4xVVFvcGZoZnVxN1VTVnc9PSIsIm1hYyI6IjkyYjA5YTI3MmE3MWE5OWRhNDBjYWZkZjQwZjk0NjI1OTY4MGNjZDM4ZDQ4YWE3YjdkMWMxNmQwZjgxMzQzZTYifQ%3D%3D"
]
]
#cacheControl: array:1 [▶]
}
}
Of course, firstly I go to /set link, then /get.
Where did I go wrong? Why all of my cookies equals to null?
By the way, last 5 hours I tried to make a function, that will contains Cookie::queue() function, but it goes wrong too. I dont know, what's wrong with my laravel cookies

Related

Can you filter data on a retrieved relationship without creating a new attribute?

I have one to many relationship between users and posts.
I want to know if I eager load in the posts can I modify the collection without a new attribute getting created on the User model.
$user = User::with('posts')->get();
// Filter the posts on the user using business logic.
// Below is an example. I DO NOT want to do this logic in the db/query builder.
// Arbitrary business rule that is not easily possible to calculate in DB
$shouldGetTestPost = true;
$user->posts = $user->posts->filter(function($post) use ($shouldGetTestPost) {
if ($shouldGetTestPost && $post->name = 'test') {
return true;
}
return false;
});
dd($user);
If I run the above code laravel will create a new attribute called posts and assign the collection to that attribute instead of modifying the relationship.
e.g.
// I've removed irrelevant model data
App\User {#1145
#table: "users"
#attributes: array:6 [
"id" => 1
"email" => "test#test.com"
"password" => "secret"
"updated_at" => "2019-02-11 18:56:35"
"created_at" => "2019-02-11 18:56:35"
// Below is new
"posts" => Illuminate\Database\Eloquent\Collection {#1217
#items: array:1 [
0 => App\Post {#1269
#table: "posts"
#attributes: array:24 [
"id" => 1
"name" => 'test'
"user_id" => 1
]
}
]
}
]
#original: array:5 [...]
#relations: array:1 [
"posts" => Illuminate\Database\Eloquent\Collection {#1264
#items: array:2 [
0 => App\Post {#1269}
1 => App\Post {#1234
#table: "posts"
#attributes: array:24 [
"id" => 1
"name" => 'test'
"user_id" => 1
]
}
]
}
]
]
Another interesting thing that happens is that in #relations the data is removed from theitems array but the reference still remains. See 0 => App\Post {#1269}
Is this intended for laravel? I'm aware that laravel prioritises attributes over relations but it seems odd that it wouldn't change the relations array.
How would I only change the relations collection and not create a new attribute?
The setRelation method can be used to override what's stored in a relation.
$model->setRelation($relation, $value)
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_setRelation
However, there's very few situations where this would be useful so I still believe this is an XY problem. You can constrain the eager loading query by passing a closure as the second argument to with() which would be the solution to conditionally retrieving related items.

Laravel Voyager: Translation not working on posts and pages

I'm trying to add ar language to my website but I have a problem the body is not saving it saves <p> </p>
I have the request printed how I receive it when updating a post with 2 languages ar, en.
The request is as this with some dummy text
Request {#57 ▼
#json: null
#convertedFiles: array:2 [▶]
#userResolver: Closure {#1935 ▶}
#routeResolver: Closure {#1994 ▶}
+attributes: ParameterBag {#67 ▶}
+request: ParameterBag {#66 ▼
#parameters: array:17 [▼
"_token" => "LjyJ1cgN5tn7yxFbjyHRPAnRzqRKWg8ojRYM7cMP"
"title_i18n" => "{"en":"Data Services","ar":"خدمة البيانات"}"
"title" => "Data Services"
"body_i18n" => "{"ar":""}"
"body" => """
<pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 11.9px; margin-top: 0px; mar ▶
* Set whether or not the multilingual is supported by the BREAD input.\r\n
*/\r\n
'bread' => true,</code></pre>
"""
"excerpt_i18n" => "{"en":"as ds as d","ar":"as das s dsa"}"
"excerpt" => "as ds as d"
"slug_i18n" => "{"en":"data-services1","ar":"khdmh-albyanat"}"
"slug" => "data-services1"
"status" => "PUBLISHED"
"category_id" => "3"
"meta_description_i18n" => "{"en":"as das dsa d","ar":"asd as dasd a"}"
"meta_description" => "as das dsa d"
"meta_keywords_i18n" => "{"en":"as das","ar":"a sdas "}"
"meta_keywords" => "as das"
"seo_title_i18n" => "{"en":"Design and conduct surveys and measure relevant indicators in all areas","ar":"Design and conduct surveys and measure relevant indicators in all areas"}"
"seo_title" => "Design and conduct surveys and measure relevant indicators in all areas"
]
}
+query: ParameterBag {#65 ▶}
+server: ServerBag {#70 ▶}
+files: FileBag {#69 ▶}
+cookies: ParameterBag {#68 ▶}
+headers: HeaderBag {#71 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/admin/posts"
#requestUri: "/admin/posts"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: Store {#2051 ▶}
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}
My voyager.php for multilanguage config is like this
'multilingual' => [
/*
* Set whether or not the multilingual is supported by the BREAD input.
*/
'bread' => true,
/*
* Set whether or not the multilingual is supported by the BREAD input.
*/
'enabled' => true,
/*
* Set whether or not the admin layout default is RTL.
*/
'rtl' => false,
/*
* Select default language
*/
'default' => 'en',
/*
* Select languages that are supported.
*/
'locales' => [
'en',
'ar',
],
],
What would be the problem?
I searched a lot, finally I tried to solve it by turning around. The solution or let's say the problem here is in this file:
posts/add-edit.blade.php
and if you remove it the bread will work perfectly with default template for posts add-edit. I think this will help a little but I don't know why this issue appear.

Laravel relation data with first() method

Vender hasMany Banquet
Here is my query:
$banquet = Vendor::with(['banquet' => function($query){
$query->where('active',1)->where('id' ,1)->first();
}])->findOrfail(1);
but the relation part return an collection like get() method not first()
#relations: array:1 [▼
"banquet" => Collection {#283 ▼
#items: array:1 [▼
0 => Banquet {#285 ▼
How to trans like this? So that I can just get the banquet data like $vendor->banquet not $vendor->banquet[0]
#relations: array:1 [▼
"banquet" => Banquet {#285 ▼
I think, you are using OneToMany for banquet.
So, you are getting in the collection/array.
Please, try it as OneToOne. and say if you get any change or not.
return $this->belongsTo();
in banquet method.
or
return $this->hasOne();

Laravel Method Injection - Variable exists?

Let's say that we have Laravel's default User.php model, and we also have UsersController.php. Here's how the create method looks like:
public function create(User $user)
{
return view('backend.users.form', compact('user'));
}
As you can see - User $user is passed as argument, and view backend.users.form receives that $user.
If I add dd($user); in the create method:
public function create(User $user)
{
dd($user);
return view('backend.users.form', compact('user'));
}
the result will be:
User {#193 ▼
#fillable: array:3 [▼
0 => "name"
1 => "email"
2 => "password"
]
#hidden: array:2 [▼
0 => "password"
1 => "remember_token"
]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
+wasRecentlyCreated: false
}
So, variable (instance) $user exists, right?
But, in a view backend.users.form that receives $user, if we do something like this:
<h3>{!! $user->exists ? 'Editing '.$user->name : 'Create New User' !!}</h3>
The result will be as if $user does not exist? The result will be Create New User. I do not understand it.
Can you explain to me why $user->exists returns false when dd($user); shows that it exists?
Without knowing your DI-container configuration, the User instance you inject probably does not exist as a record in the database - it has no identity. It is merely a model instance you can use.
Can you explain to me why $user->exists returns false when dd($user);
shows that it exists?
Please look carefully at the result of dd() inside a controller:
...
+exists: false
...
$user->exists is false here, so it will return false in any case.

Laravel 5.2 Request to action from other controller without js

Need send request to controller Category from Wizard.
Wizard action:
$request = Request::create('/admin/category', 'POST', $this->prepare_category($row->toArray()));
Route::dispatch($request);
Category action:
public function store(Request $request, ContentPlugins $plugins)
{
dd(Request::all());
}
The request passes but Request::all() is empty []
dd on $request:
Request {#1922 ▼
#json: null
#userResolver: null
#routeResolver: null
+attributes: ParameterBag {#1859 ▼
#parameters: []
}
+request: ParameterBag {#1726 ▼
#parameters: array:11 [▼
"title" => "Фотосувениры"
"level" => "0"
"description" => null
"cost" => null
"what" => null
"type" => "catalog"
"parent" => 0
"url" => "fotosuveniry-0-0"
"sitemap" => 1
"active" => 1
"position" => 0
]
}
+query: ParameterBag {#1920 ▶}
+server: ServerBag {#1679 ▶}
+files: FileBag {#1728 ▶}
+cookies: ParameterBag {#1727 ▶}
+headers: HeaderBag {#1886 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: null
#requestUri: null
#baseUrl: null
#basePath: null
#method: null
#format: null
#session: null
#locale: null
#defaultLocale: "en"
}
I need to get request->parameters as $_POST on Category.php (Request::all())
How to cause the action of another component and pass it parameters as when submitting the form?
That's what I need from Kohana http://kohanaframework.org/3.3/guide/kohana/requests#external-requests
This uses POST
$request = Request::factory('http://example.com/post_api')- >method(Request::POST)->post(array('foo' => 'bar', 'bar' => 'baz'));
Answer:
Input::merge($this->prepare_category($row->toArray()));
before
$request = Request::create('/admin/category', 'POST');
Route::dispatch($request);

Categories