mardi 29 janvier 2019

How to use Laravel query builder with AND where claus in PHP?

I am trying to ensure that a record exists in the database prior to performing a user related action. When I execute the query directly in my PHPMyAdmin like so (for testing purposes)

SELECT * FROM `chat_participants` WHERE `chat_id` = 2 AND `user_id` = 2

I receive the correct record. However, when I try to use Laravels query builder to achieve the same like so:

dd($this->participants
        ->where('chat_id', '=', 2)
        ->where('user_id', '=', 2)
        ->get()
        ->first());

I get null. Is there a way I can ensure that the record exists in the database using query builder? Do I need to declare AND in the query builder?

Update, I set the participants variable in my constructor:

public function __construct()
{
    $this->middleware('auth');

    $this->header = DB::table('chat_headers');
    $this->participants = DB::table('chat_participants');
    $this->messages = DB::table('chat_messages');
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2S9ruGJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire