I
I
Ismail-12022-04-15 07:06:16
Laravel
Ismail-1, 2022-04-15 07:06:16

How to build query to get data from DB with Elequent ORM?

How to write a query for a selection, for example, through if I can select the records that I need, but at the same time I then get all the records from the database and compare and send the
necessary ones through a loop.

$user = User::find($user_id);
        $messages =  Message::where('active', true)->get();
if($message->referrer == true || $message->referrer == false && $user->referrer_1_id == null){

And how can you already write if in a
query
?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Anton, 2022-04-15
@Fragster

https://laravel.com/docs/9.x/queries#conditional-c...
something like

Message::where('active', true)
  ->when($user->referrer_1_id !== null, function ($query, $userReferrerIsNotNull) {
    $query->where('referrer ', true);
  })->get()

V
vism, 2022-04-15
@vism

I don't know what kind of perversion to use when.
like this, you can configure it as you like and just do pass the request anywhere in the variable.

$user = User::find($user_id);
$messagesQuery =  Message::where('active', true);
if($message->referrer == true || ($message->referrer == false && $user->referrer_1_id == null)){
  $messagesQuery->where(....);
}
$messages =  $messagesQuery->get()

P
pLavrenov, 2022-04-16
@pLavrenov

$response = User::where([
    ['active', true],
    ['referrer', true],
])->orWhere([
    ['active', true],
    ['referrer', false],
])->whereHas('user', function ($q) {
    $q->where('referrer_1_id', null);
})->get();

Street Magic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question