If select Laravel

Asked

Viewed 395 times

2

I have a question here and I would like some help. I have a controller where you have to make a query in the Boxes table where you check whether the box is open or closed.

public function index(Request $request)
    {   
        $caixas = DB::select('SELECT * FROM caixas INNER JOIN
        users ON users.id = users.id JOIN caixas c ON c.data_abertura = c.data_abertura');

        if( $caixas )
            return view('vendas.index', compact('caixas'));
        else
            return view('vendas.caixa');
    }

It is working, however when I soon with user 1 and open the box it opens and does the if blz, if I dislodge and log in with user 2 it continues with the open box, does not identify that user 2 is with closed box.

Thank you.

  • That one on select has no sense. And where do you use user information in that query ? And why not use Eloquent to obtain bank information?

  • Hello Isac, I’m trying to do that when logging into the system in the PDV menu it check if the box is open or closed so entering in the if however I’m not able to validate it, because when I open the box with user 1 blz, Then I move and then user 2 checks that the user 2 is with closed box, would have an example of that with eloquent? Thank you

  • To use Eloquent and obtain the same information you must have the models and relationships constructed correctly. Where do you use the logged-in user information ? I don’t see this in this query. And how do you detect if you have the box open or closed ?

  • Actually I do not know if the logged-in user will solve, plus what I need the system to do, would be a validation that when user1 log into the system to operate the box it identifies whether the box is open or not there would be a query in the status table for example, and in case there are more boxes like supermarket it identifies that the logged-in user would be open cashier or not. I’m sorry if I can’t explain it properly, thank you

2 answers

1

You are checking the return of the query, ie if the query returns a result will always be true, why php performs this conversion before checking in the if.

I also noticed that the INNER JOIN you are doing seems a little strange in the ON clause "users.id = users.id". A JOIN is done to query results with related columns.

Ex: SELECT * FROM caixa INNER JOIN user ON user.id = caixa.user_id;

BS: I don’t know if your bank is like this, it’s just an example.

I hope it helped you.

  • The columns are related, With this select, it worked. Thanks, Hug

0

If use model has a more practical way of doing this:

//Caixas seria o Model

Caixas::with('user')->where('data_abertura', $data_abertura)->get();

With Query Builder it would be simple too

DB::table('caixas')->join('users', 'caixas.user_id','=', 'users.id')->where('data_abertura', $data_abertura)->get();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.