D
D
Dmitry28PRO2020-11-25 16:33:30
Laravel
Dmitry28PRO, 2020-11-25 16:33:30

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');
    }

Why is it showing only the first user? and not the second or 3rd.?
if I change the query a little, it will output correctly to this 'data' => User::where('id', 2)->get() //it will display user 2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanes, 2020-11-25
@Dmitry28PRo

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');
    }

The template doesn't need a loop. More or less like this.

I
Ilya S, 2020-11-25
@Stalinko

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()]);

In template:
User ID: {{ $user->id }}<br>
Invoice {{ $invoice ? $invoice->id : 'n/a' }}

Again - why is there only one invoice? In the real world, there must be a collection of invoices, or the latest invoice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question