Convert Json Data to XML in Laravel 5.4 - laravel-5

I have an Laravel 5.4 Framework.I am going to send Json Request (in Controller) using Post Method.I would like to know how to how to convert Json Data to XML Data in Laravel
I have tried below code but its not working
public function store(Request $Request)
{
echo json_decode($Request);
}

You can do something like this its just an example
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$root=null;
$xml = new SimpleXMLElement($root ? '<' . $root . '/>' : '<root/>');
array_walk_recursive($age, function($value, $key)use($xml){
$xml->addChild($key, $value);
});
echo $xml->asXML();
In your case : json_decode outputs array
public function store(Request $Request)
{
$data=json_decode($Request);
$root=null;
$xml = new SimpleXMLElement($root ? '<' . $root . '/>' : '<root/>');
array_walk_recursive($data, function($value, $key)use($xml){
$xml->addChild($key, $value);
});
echo $xml->asXML();
}

Related

ErrorException: Array to string conversion in file

I have a problem when updating the URLImg data when I use the PUT method, it throws the following error in Postman 'ErrorException: Array to string conversion in file' but if I use the POST method I have no problem uploading the urls of my images.
public function store(Request $request)
{
$values = $request->except('URLImg');
$data = $request->only('URLImg[]');
if($request->hasFile('URLImg')){
foreach($request->file("URLImg") as $image)
{
$name = Str::random(10).'.'.$image->getClientOriginalExtension();
$path = 'storage/img/';
$image->move($path, $name);
$data[] = $name;
$tramite = Tramite::create($values);
$tramite->URLImg = json_encode($data);
$tramite->save();
}
}else{
$tramite = Tramite::create($values);
$tramite->save();
}
return response()->json($tramite, 201);
public function update(Request $request, Tramite $tramite)
{
$data = $request->only('URLImg[]');
if($request->hasFile('URLImg')){
foreach($request->file("URLImg") as $image)
{
$name = Str::random(10).'.'.$image->getClientOriginalExtension();
$path = 'storage/img/';
$image->move($path, $name);
$data[] = $name;
$tramite->URLImg = json_encode($data);
$tramite->save();
}
}
return response()->json($tramite, 201);
}
Postman Config
Postman Config
Yes, it is almost the same code but I only need to update the URLImg field
Want to use a PUT or PATCH request for form containing file uploads - submit a POST request with method spoofing
<form action="/foo/bar" method="POST">
#method('PUT')
...
</form>
via any javascript framework like vue
let data = new FormData;
data.append("_method", "PUT")
axios.post("some/url", data)
Using _method and setting it to 'PUT' or 'PATCH' will allow to declare route as a PUT route and still use POST request to submit form data
$_FILES will not be populated on a PUT or PATCH request with multipart/form-data - PHP limitation

How to serve the data returned from laravel Storage::disk('private')->get($file) as PDF

My question is about the problem in link below :
Understanding file storage and protecting contents Laravel 5
I need to use the same method mentioned in above example but instead of an image I need to provide a download link for PDF file or a link to open PDF file in browser and I can't do that because, as mentioned in above example's comments The Storage::disk('private')->get($file) returns the CONTENT of the file NOT the URL.
Please tell me how can I convert the row data (content of file) to a file and provide the link for the users inside the view.
You should follow the below steps:
I have store pdf file into storage/app/pdf
In controller:
public function __construct()
{
$this->middleware('auth');
}
public function index(Request $request, $file)
{
$file = storage_path('app/pdf/') . $file . '.pdf';
if (file_exists($file)) {
$headers = [
'Content-Type' => 'application/pdf'
];
return response()->file($file, $headers);
} else {
abort(404, 'File not found!');
}
}
if laravel below 5.2:
Add use Response; above controller class in the controller.
public function index(Request $request, $file)
{
$file = storage_path('app/pdf/') . $file . '.pdf';
return Response::make(file_get_contents($file), 200, [ 'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$file.'"'
]);
}
In web.php
Route::get('/preview-pdf/{file}', 'Yourcontroller#index');
In the blade view:
PDf
According to the Laravel documentation you can simply use the download method on the Storage facade.
From your controller, return the result of the command.
return Storage::disk('private')->download($file);

How to pass whole url as params?

I want allow user to scan QR code and go to website. This is search results with params in url address like
http://example.com/qr-code/http://example.org/search/user_id=12&model_id=4&role_id=8
My route is set as
Route::get('qr-code/{data?}/{size?}', 'QrController#qrCode')->name('qr-code');
so i hope my $data will get url:
http://example.org/search/user_id=12&model_id=4&role_id=8
My Controller has
public function qrCode(Request $request, $data = null, $size = 60) {
dd(88);
$data = (empty($data)) ? env('APP_URL') : $data;
return view('qr.code')->with('qr', QrCode::size($size)->generate($data));
}
But i can't even see dd(88) route redirect to main page.
How can i fix that ?
The thing is that Laravel doesn't know that you will provide an entire URL as a parameter. He could interpret everything like that:
http://example.com/qr-code/http:/ /search/user_id=12&model_id=4&role_id=8 . Try encoding the URL diffrently. Your $data will be "http:/" and your size will be 60.
Ok i took i added to helper
function base64url_encode($s) {
return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}
function base64url_decode($s) {
return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s));
}
And then i am getting URI from request on view like
Click me
From JS i send params to QrController
public function qrCode(Request $request, $data = null, $type = null, $size = 200) {
$data = (empty($data)) ? env('APP_URL') : $data;
if ($type == 'url') $data = env('APP_URL') . base64url_decode($data);
return view('qr.code')->with('qr', QrCode::size($size)->generate($data));
}
And works fine. Base64 has + or / sometimes so thats why need to replace both - and _ and second is i add base url from env to make whole address.
Thx

how to use $request->all() on controller and blade view in laravel 5.5

in Controller
public function index(Request $request)
{
$search_cons = $request->all();
$search_con = $search_cons->name; //error place
return $search_cons.$search_con;
}
->name this place has the error
Trying to get property of non-object
Or in blade.view
<p>{{$search_cons->name}}</p> has the error
Trying to get property of non-object
But if I use
$search_cons=$request->input('name');
on controller
the blade view
<p>{{$search_cons}}</p> will work ok!
I want the $search=request->all() so I can freely use $search->name on my blade view
How can I fix the question?
PS: I tried $resquest('name') still not to work
Request::all() ->tell me the
When you do $request->all() it returns all the inputs submitted in array format. So in your case, you can do
$search_cons = $request->all(); // dd($search_cons) so you can see its structure
$search_con = $search_cons['name']; // instead of ->name since it's not an object anymore
And anyway, you can skip the $request->all() thing - you can actually just do this directly:
$request->name.
EDIT:
You can cast the array as an object using (object)
$search_cons = (object) $request->all();
this will still let you use $search_cons->name
$search_cons is an array, not an object:
$search_con = $search_cons['name']
public function index(Request $request)
{
$search_cons = $request->all(); //returns array
$search_con = $search_cons['name']; //error place
return $search_cons.$search_con;
}
Or you can do like this
request()->name //request() isa global helper function
Try
In Controller
public function index(Request $request)
{
$search = $request->all();
return ['search' => $search];
}
In Blade
<p>Name : {{$search['name'] ? $search['name'] : ''}} </p>

Laravel 5.2 Multi sms

How to explode multi sms number on laravel 5.2?
public function store(Request $request)
{
$mes = new Outbox();
$mes->DestinationNumber = $request->input('DestinationNumber');
$explode = explode(' ',$mes->DestinationNumber);
return $explode;
}
This is not a Laravel related question.
Find the documentation from the PHP web and use your logic.
It depends how you have $mes->DestinationNumber stored and how you want them to return from your function.
// CSV to array
$result= explode(',', $mes->DestinationNumber);
// array to CSV
$result= implode(',', $mes->DestinationNumber);
Check before you convert and return your result:
var_dump($mes->DestinationNumber);
// your logic here ..
var_dump($result);exit;

Resources