Laravel: Redirect with post route - php

Been trying to find a way to call to pass a multidimensional array to a Post route with no success.
The array looks like this:
"order" => array:16 [
"id" => "1"
"total" => "4825"
"neighborhood" => "Barrio Bravo"
]
"products" => array:2 [
4 => array:4 [
"id" => "4"
"name" => "Maestro Dobel 750ml"
"price" => "530"
"quantity" => "1"
]
1 => array:4 [
"id" => "1"
"name" => "Don Julio 70 700ml"
"price" => "650"
"quantity" => "1"
]
]
"grandTotal" => "1180"
"balanceToPay" => "354"
"cartTotal" => "826"
I don't have any problem asserting the route in the unit test calling the route like so:
$this->post(route('order.success', $orderInfo));
But when it comes to the controller I can't find the way to redirect to order.success with its orderInfo array.
This won't work since redirect only works with GET:
return redirect(route('order.success', $orderInfo));
Ideas?

It's not going to work with a simple redirection because you cannot choose the HTTP method. It's always GET when you make a redirection. It works in your test because you make a POST request manually.
I can see 2 solutions:
You can send data using GET method (they are added as URL parameters). If they are not confidential it can be a solution.
If you don't want to send those data in the URL, you have to save them somewhere and get them from the storage when you're on the order.success page. You can save them, for instance, in the session storage or in the local storage of your browser.
Also, your test isn't good if it tests a behavior that does not happen in your app (post request instead of redirection).

Related

How to write a PHP script that searches a PHP file for specific information?

Disclaimer: This is my first foray into PHP and scripting in general - all of my development experience is in iOS (Swift and Objective-C). I've started doing a tutorial.
We have a large .php file, let's call it objects.php, with a number of static variables.
The structure looks something like this:
'ObjectDictionaryTom' => [
A_Class::NAME => "Tom"
]
'ObjectDictionaryDick' => [
A_Class::NAME => "Dick"
]
'ObjectDictionaryHarry' => [
A_Class::NAME => "Harry"
]
'ObjectDictionaryAlsoTom' => [
A_Class::NAME => "Tom"
]
The structure isn't consistent; Sometimes, it even looks like this:
`Objects` => [
'ObjectDictionaryTom' => [
A_Class::NAME => "Tom"
],
'ObjectDictionaryDick' => [
A_Class::NAME => "Dick"
],
'ObjectDictionaryHarry' => [
A_Class::NAME => "Harry"
],
'ObjectDictionaryAlsoTom' => [
A_Class::NAME => "Tom"
]
]
How can I read through this file and get all objects that belong to "Tom"? Maybe gather them in a CSV?
I'm happy to edit or explain further as needed!
How about something like this:
'(.*?)'\s*=>\s*\[[\n\r\s]*\s*(.*?)\s*=>\s*"(.*?)"
As you can see here at regex101.com
You can see in the sidebar provided which capturing groups store which values - in particular, the ones you want are in capturing groups 1 (the first set of parentheses).
To access them, just get the values of capturing group 1 from the result.

Zammad API: Create ticket with tag

For those who don't want to read the whole question:
I'm looking for the index in the API-Request (Zammad) to set a tag while creating a ticket.
Details:
I'm using PHP to make an API-Request to my server where Zammad is installed. The following shows the data i sent via curl:
json_encode([
"title" => $title,
"group_id" => 2,
"priority_id" => 2,
"category" => 'Some Category',
"state_id" => 1,
"type" => "Some Type",
"customer" => $userID,
"article" => [
"body" => $htmlBody,
"type" => "note",
"content_type" => "text/html",
],
"tag_list" => [ // <-- The question is about this segment
"The tag i want to add",
],
]);
After converting the data to JSON, im sending it via POST to http://<myServerIP>/api/v1/tickets
What I've tried so far:
I tried guessing the index of the tag at which i failed.
The first full example is shown above.
Secondly:
...
"tag_id" => 9, // I've check its the actual ID of the tag i want to add
And Finally:
...
"tag" => "The tag i want to add",
Needless to say that i didn't succeed. Sometimes i get an error id (im assuming its because the index doesn't exist [who would have thought that? :)]), sometimes i get nothing and Zammad just creates the ticket without the tag. What do i mean when i say sometimes? I refer my tries specified above.
What I've also tried:
Searching for some answer on the web. The thing that comes close to what i want is this. But i would rather create the ticket with the tag instead of making another request just to add the tag.
I've looked inside the code, its written in ruby. The index is 'tags' and needs to be sperated by ,.
Basicly:
json_encode([
"title" => $title,
"group_id" => 2,
"priority_id" => 2,
"category" => 'Some Category',
"state_id" => 1,
"type" => "Some Type",
"customer" => $userID,
"article" => [
"body" => $htmlBody,
"type" => "note",
"content_type" => "text/html",
],
"tags" => "tag1,tag2,tag3", // or simply "tags" => "tag1"
]);
It might help someone in the future ...

Passing Form-data Array from one function to another function in same controller

I have a multidimensional array containing data from a form and I need this array in another Controller in the same controller to continue working with it, but I don't know how I do that.
The array can look like this example:
array [
"absender" => "Maxim Ivan",
"email" => "maximivan#example.com",
"telefon" => "1234567890",
"fax" => null,
"grund" => "Gehaltserhöhung",
"termin" => [
0 => [
"person" => "Some Name",
"meeting" => "10.05"
],
1 => [
"person" => "Another Name",
"meeting" => "18.05"
],
2 => [
"person" => "Again another name",
"next-possible-meeting" => "1"
],
3 => [
"person" => "And again",
"next-possible-meeting" => "1"
],
],
"bemerkung" => "some notes by Maxim"
]
This array is created(and the input data validated) in the store-method of the 'TerminController'.
This method will return a view where all of this data gets displayed again, to let the user check the info and can then add a document.
When the document is added and the data gets submitted with an input-button the upload-method in the same Controller gets called.
And there's where I need the array with the form-data to go on working with it.
But how do I achieve passing the array through to the next function that is only called with an input-button.
First approach was to save the array into the session, which did even work even though it was hard due to the multidimensional; but it's a really ugly solution.
Should I save the input data into a database in the store-method and fetch it again in the upload-method?
Or is it somehow possible to pass the array through the Controllers/ make it accessible in the upload-Controller even though it gets created in another one?
I have also heard something about using serialize()and unserialize(), but I'm not exactly sure how this could help me..
Or maybe there's another and even better solution I just don't think of?
I'd appreciate all the help I can get.
The array varies, it can be 17 arrays nested in 'termin' but I can also be just one.
You can store it in the cache:
Cache::put('multiArray', $multiArray); //put array in cache
$array = Cache::get('multiArray'); //retreive from cache

Optimized query in laravel with relationship

I have 3 model: Tag, Post, User, and map them with TagPost, TagUser model.
When User search Tag in input search, I using $http.post (VUE) and send array tag selected to Server.
Example tags print in server:
array:2 [
0 => array:10 [
"id" => 197
"name" => "nisi"
"slugify" => "nisi"
"thumbnail" => "http://lorempixel.com/400/400/?85397"
"seo_title" => null
"seo_description" => null
"seo_keyword" => null
"cover" => null
"created_at" => "2017-06-13 07:19:30"
"updated_at" => "2017-06-04 06:01:14"
]
1 => array:10 [
"id" => 184
"name" => "dolores"
"slugify" => "dolores"
"thumbnail" => "http://lorempixel.com/400/400/?45793"
"seo_title" => null
"seo_description" => null
"seo_keyword" => null
"cover" => null
"created_at" => "2017-06-08 15:37:59"
"updated_at" => "2017-05-27 13:34:02"
]
]
I want when user search tags then view update users and posts have tags user search.
View here:
In server (laravel) how to code with relationship model using least query?
Assuming that you have correctly set your relationship this should work
Tag::with('posts','users')->whereIn('id', $your_ids_send_by_vue)->get();
And then in your controller do the logic to get your desired data

Send $variable to a view in Laravel 5

I have an $alerts variable array.
Look like this
array:3 [▼
0 => array:3 [▼
"status" => 200
"message" => "Success"
"data" => []
]
1 => array:3 [▼
"status" => 200
"message" => "Success"
"data" => []
]
2 => array:3 [▼
"status" => 404
"error_code" => 35
"message" => "invalid json - api not supported"
]
]
I want to send it from my controller to my view.
I've tried this
controller
return Redirect::to('/account/'.$id)
->with('alerts',$alerts)
Route
My route : http://localhost:8888/account/1007
View
I tried accessing like this
{!!$alerts!!}
Then, I tried accessing it like this, but I kept getting
Undefined variable: alerts
As per the Laravel documentation on redirects, when redirecting using with() it adds the data to the session, and not as a view variable. Therefore, you will need to access it like:
#foreach (session('alerts') as $alert)
<p>{{ $alert['message'] }}</p>
#endforeach
Try this:
Session::flash('alerts', $alerts);
return route('ROUTENAME', $id);
Just change ROUTENAME, in the name of the route (if defined in routes.php).
For example:
Route::get('account/{id}', ['as' => 'account.show', 'uses' => 'AccountController#show']);
In this example, ROUTENAME would be 'account.show'.
In your view you can access it by doing:
Session::get('alerts');
Info:
- http://laravel.com/docs/5.1/session#flash-data
Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method.
You haven't posted the code that actually loads the view. When you return a Redirect->with(...) all you're doing is passing the variable to the next request. In your controller that serves the account/{id} route you need to return view('viewname', ['alerts' => session('alerts')])

Categories