I'm posting a CSV file from one Laravel app to another:
Sending:
$contents = file_get_contents($filePath);
Log::debug('contents', ['contents'=>$contents]); // I can see contents of file
Http::withToken($token)->attach('attachment', $contents)->post($uri);
Receiving:
$content = $request->getContent();
Log::debug('about to store content to file...', ['content'=>$content]);
// about to store content to file... {"content":""}
What am I missing here?
Files are not the content, you should fetch the file out with.
$request->file('attachment');
If you want to send a file as a raw request body, this snippet shows you how from the documentation.
$response = Http::withBody(
file_get_contents($filePath), 'text'
)->post($uri);
Related
I am trying to create the pdf, using the domPDF library, for each single client present in the table of my application. So far everything ok!
When I go to save the pdf I want the name of the pdf document to be saved as:
name_client_surname_client.pdf,
so if for example I have a client named Alex Haiold, the document must be saved as Alex_Haiold.pdf
To do this, since I am passing the single client in the controller, as shown below, I tried to write
return $pdf->download('client->surname client->name.pdf');
but it is saved as
client-_surname client-_name.pdf (then printing client-_surname client-_name).
Here my code:
public function generatePDF(Client $client)
{
$data = [
'title' => 'Welcome to LaravelTuts.com',
'date' => date('m/d/Y'),
'client' => $client
];
//dd($data);
$pdf = PDF::loadView('client.clientPDF', $data);
return $pdf->download('client->surname client->name.pdf');
}
Can anyone kindly help me?
$pdf = PDF::loadView('client.clientPDF', $data);
return $pdf->download($client->surname . ' ' . client->name . '.pdf');
On the first line, we are just making a view file. The file is resources/views/client.clientPDF.blade.php and we are feeding data to the view file by passing the variable $data to it.
After making the view, our next step is to download the pdf file. For doing this, we need to call the download method of the instance.
On the first parameter, we are passing the filename with extension(pdf).
I am using phpword package in my laravel. I edit content and try to send file as email attachment but email attachment docx file not opens it comes with corrupted file in the attachment. please check below code.
below is my code where i am returning the docx file using phpword
$save_path = $storage_path.'output.docx';
$templateProcessor->saveAs($save_path);
$headers = ['Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
return $WordContent = response()->download($save_path,null,$headers);
below is code which I use to send email
$message->subject($email_data['title'])
->from($email_data['from_email'], $email_data['from'])
->attachData($WordContent, $email_data['title'].'.'.$extension,'mime'=>$mime]);
});
I'm generating pdf using following code in Laravel but the issue is sometimes data is saved in the database and pdf is not generated when i check the pdf folder there is no pdf files with that name, this does not happens every time i generate the pdf but there are some entries in my database for which the pdf is not generated, i want some way so that i can check whether the pdf has been generated and mark status true in the database if pdf is generated later on recreate pdf and send it to user through cron jobs or is there any other way also, Please suggest some solution Thanks
use PDF;
..
..
$DataStore->user_id = $user_id;
$DataStore->description = $description;
$DataStore->save();
if($DataStore) {
$pdf = PDF::loadView('/admin/user_submission_pdf', ['data' => $DataStore]);
$content = $pdf->download()->getOriginalContent();
Storage::put('public/pdfFiles/' . $DataStore->Id . '.pdf', $content);
to download your pdf, change the the line
$content = $pdf->download()->getOriginalContent();
to
$content = $pdf->download('file-name-you-want.pdf');
then at last line should return:
return $content;
I'm trying to upload an image file . When i am calling my API to upload file using postman , getting error like Fatal error: Call to a member function file() on array.
I could not post this file to my Laravel controller .How to post files in Laravel. Anyone could help me to solve this issue?
Here is my controller function,
public function edit(Request $request){
$request = $request->input();
if(empty($request)) {
$request = json_decode(file_get_contents('php://input'),true);
}
$to_return = array();
$file = $request->file('files');
}
at this line $file = $request->file('files'); getting fatal error.
set method as post .. and at body ... change to option from form data to w-xxx-formurlencod ... and try ..
If you want to file uploads with Postman and Laravel, simply remove the Content-Type header setting in Postman.
set method as post
and at body ... select the radio that shows url encoded
see an image ... click on text set it as flie
When I want upload file with form in laravel, I cant remove file value from request.
This is my full code
if($request->hasFile('image')){
$string_name = str_random(12);
$image = $request->file('image')->move(getcwd().'/image/original',$string_name.'.'.$request->file('image')->getClientOriginalExtension());
$request->request->remove('image');
}
if($request->hasFile('thumbnail')){
$string_name = str_random(12);
$thumbnail = $request->file('thumbnail')->move(getcwd().'/image/thumbnail',$string_name.'.'.$request->file('thumbnail')->getClientOriginalExtension());
}
Portfolio::create($request->all());
but image or thumbnail file do not remove from $request. This means this line of code not working :
$request->request->remove('image');
I've tried many ways but the file does not get removed from the request.
Instead of removing the image from your $request before saving, you can explicitly mention what you would like to save to your model by using the ->only([]) method on $request.
Portfolio::create($request->only(['title', ...]));
This will allow you to specify exactly what you would like saved from the $request data.
You can do the reverse and use the ->except() method to remove the image too:
Portfolio::create($request->except('image'));
use this to remove the file
$request->offsetUnset('input_file_name');
or
$request->except('filename');