Answer the question
In order to leave comments, you need to log in
How to make a correct request to display the data of one user?
Hello. I have a database and there are tables User and invoice, I connected them, now when I generate an invoice everything is OK, but I want to generate only the invoice of the person near whom I pressed the button, and it only generates an invoice for me (id 1 and person data id 1 ). And the person has id 2 and other data.
Here is the controller:
public function invoiceView()
{
$this->user = Auth::User();
$pdf = PDF::loadView('pdf.invoice', ['data' => User::where('id', $this->user->id)->get()], ['invoice' => Invoice::where('id', $this->user->id)->get()]);
return $pdf->stream('invoice.pdf');
}
Answer the question
In order to leave comments, you need to log in
Pass the user id and it will return the one you request.
$this->user = Auth::User();
is the currently logged in user.
public function invoiceView($id, $invoiceId)
{
$pdf = PDF::loadView('pdf.invoice', ['data' => User::where('id', $id], ['invoice' => Invoice::where('id', $invoiceId)->first()]);
return $pdf->stream('invoice.pdf');
}
1. - some kind of butter oil. The variable already contains the user. Why load it from the database again? Yes, even as a collection.
2. - Do the invoice IDs match the users? This is some nonsense. Probably, there should be a column
. As a result, we get the following:User::where('id', $this->user->id)->get()
$this->user
$pdf = PDF::loadView('pdf.invoice', ['user' => $this->user, 'invoice' => Invoice::where('user_id', $this->user->id)->first()]);
User ID: {{ $user->id }}<br>
Invoice {{ $invoice ? $invoice->id : 'n/a' }}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question