Laravel api - how to upload file - laravel

I am building a web service for mobile application using Laravel. I need to let the users to upload images. I am using passport package. In developing environment I created a function store as follows
public function store(Request $request)
{
return $request;
}
and using Firefox poster I post contents. These are the parameters
The problem is the function store always returns empty array while I expect it to return information about the file. What is wrong here?
thanks

I found it. Firefox Poster has problems with this. I used Chrome Postman. I removed all the headers and set a key for file. as follows
it is header
and this is the code
public function store(Request $request)
{
$file = $request->file('image');
return $file->getClientOriginalExtension();
}

Returning $request will not work. If you want to check if the file exists, try return $request->allFiles()

Related

Auth is not working inside the Stripe Webhook Function in Stripe [Laravel]

I am trying to access Authenticated User information inside the Stripe-Webhook-Url function. But when webhook url hits, the Auth inside that function doesn't return anything!
Here is the route and action I am working with:
route
Route::post('stripe-webhook', [StripeController::class, 'stripe_webhook']);
action
public function stripe_webhook(Request $request)
{
$response = $request->all();
$user_id = Auth::id(); // It returns Nothing.....
}
In the above function, The Auth returns nothing. Even session doesn't work inside this function.
I am trying to debug this issue but nothing worked out. Kindly help me to sort out this problem.
Thanks!

Laravel request->all() is empty

In Laravel 8, $request->all() is not showing dynamic path parameters.
Steps to reproduce:
Start a new Laravel project with composer, Laravel version 8.61.0.
Define a route in routes/api.php as follows:
Route::get('/first/{first}/second/{second}', function (Request $request) {
return $request->all();
});
Run php artisan serve and direct the browser to http://localhost:8000/api/first/hello/second/world
Expect something like the following response: {"first":"hello","second":"world"}. Instead we simply see [].
Change the route to:
Route::get('/first/{first}/second/{second}', function (Request $request) {
return [
'first'=>$request->first,
'second'=>$request->second,
];
});
Then we see the expected {"first":"hello","second":"world"}
So...why isn't $request->all() giving me this response?
I looked at Laravel doc
Retrieving All Input Data
You may retrieve all of the incoming request's input data as an
array using the all method. This method may be used regardless of
whether the incoming request is from an HTML form or is an XHR
request
Means $request->all() equal to $request->input('name', 'email', ....)
instead of $request->all() you can try this
Route::get('/first/{first}/second/{second}', function (Illuminate\Http\Request $request, $first, $second) {
return [
'first'=> $first,
'second'=> $second,
];
});
UPDATE
You can do it too with (splat operator in PHP)
Route::get('/first/{first}/second/{second}', function (Illuminate\Http\Request $request, ...$all) {
return $all;
});
// returns
[
"hello",
"world"
]
I hope this helped you,
Happy Coding.
Have you tried dd($request->all()) statement in Controller, not in closure. In previous Laravel version (version 5 or version 7), that state works in that versions.
don't know why $request->all() is empty, but I used $request->getContent() and it returned the data in the body of the request I was looking for.
Credit:
Laravel empty request

update profile in laravel API for mobile apps

I want to build an api for mobile apps. for now, i want to create an edit profile api. but i dont know how to store an image, if user wants to upload their avatar.
Here is my code:
public function profileedit($id, Request $request){
$users = user::find($id);
$users->name = $request->firstName;
$users->thumbnail = $request->avatar;
$users->save();
$data[] = [
'id'=>$users->uid,
'name'=>$users->name,
'avatar'=>$users->thumbnail,
'status'=>200,
];
return response()->json($data);
}
how to put the $request->avatar into my project storage, and show it in url form (my project is already uploaded on the server)
The easiest way to store files in Laravel is this.
use Illuminate\Support\Facades\Storage;
public function profileedit($id, Request $request){
//validator place
$users = user::find($id);
$users->name = $request->firstName;
$users->thumbnail = $request->avatar->store('avatars','public');
$users->save();
$data[] = [
'id'=>$users->uid,
'name'=>$users->name,
'avatar'=>Storage::url($users->thumbnail),
'status'=>200,
];
return response()->json($data);
}
but as you probably know, you should run php artisan storage:link command to generate storage shortcut in public directory.
for security reason you can use validator to let only image file store. in this example I limited file to all image types with maximum 4MB size
$request->validate([
'avatar' => 'required|image|max:4096',
]);
for more information these are document links.
File Storage
Validation

how to save pdf inside controller and pass pdf patch

pdf api -> https://github.com/barryvdh/laravel-dompdf
I create some simple API, everyone who pass some data will recaive link to pdf file.
The thing is, i dont know how to save pdf after conroller make changes inside my blade template.
so...
i already try changing data by request and that was working.
something like :
class pdfGenerator extends Controller
{
public function show(Request $request)
{
$data = $request->json()->all();
return view('test2', compact('data'));
}
}
that work well, also pdf creator works well from web.php
Route::get('/test', function () {
$pdf = PDF::loadView('test2');
return $pdf->download('test2.pdf');
});
that one just download my pdf file as expected.
but, now i try to pust some changes from request and save file but... without efford... any idea?
My code after tray to save content
class pdfGenerator extends Controller
{
public function show(Request $request)
{
$data = $request->json()->all();
return $pdf = PDF::loadView('test2', compact('data'))->save('/pdf_saved/my_test_file.pdf');
}
}
But i get only some errors. Please help :D
How can I save a PDF inside my controller and pass the PDF patch?
$data = $request->json()->all();
$pdf = app('dompdf.wrapper');
$pdf->loadView('test2', $data);
return $pdf->download('test2.pdf');

deleteFileAfterSend Laravel response doesn't delete file in tests

I an have laravel 5.5 App.
I'm trying to make a test for download PDF with laravel snappy.
So far I have in my CheckTest.php one function:
public function testPrintCheck(){
$check = $this->createCheck();
$route ="/api/checks/print/$check->id";
$response = $this->get($route);
$this->assertEquals($response->headers->get('content-type') , 'application/pdf');
$response->assertStatus(200);
}
In my controller I have:
public function printCheck(Request $request, Check $check){
$pdf = \PDF::loadView("pdf.check");
$file= storage_path(). "/app/checks/$check->id.pdf";
$pdf->save($file);
$headers = array(
'Content-Type: application/pdf'
);
return response()->download($file, "check_n_$check->id.pdf", $headers)->deleteFileAfterSend(true)->setStatusCode(200);
}
The current test works but I expect to delete file that I have create after download (with deleteFileAfterSend(true)) , but the file is not deleted.
So if I add the assert that file not exist it fails.
There is a possible solution to this according to the issue created not so long ago on github:
https://github.com/laravel/framework/issues/36286
Working on Laravel 8.24
Basically we need to force sending file contents, which then triggers file deletion
public function test(): void
{
$response = $this->get('url/to/download');
// Here the file is not deleted yet
$response->sendContent();
// Here the file is deleted
}

Resources