How does one-to-many association in PHP model add conditions to the association table?

I want to take out the notification list of the current user (including the status of whether the user views it or not. The status is in another table. If user--id exists in notice_record, it means it has been read.) the structure of the table is as follows

here is my code

namespace appindexmodel;
use thinkModel;
class Notice extends Model
{

public function record()
{
    return $this->hasMany("NoticeRecord","notice_id");
}

}

/ / query the notification list of the current user (assuming the current user id=100)
$list = Notice::hasWhere ("record", [" user_id"= > 100])-> select ();

the result of this check is different from what I expected. There is no Filter to drop the data in the notice_ record table that does not belong to user_id=100. Is it the official BUG or did I make a mistake? I tried $list = Notice::hasWhere ("record", [" user_id"= > 100])-> the user_id in select (); deliberately misspelled user_ids, and reported that the notice_record table did not have this field, so it proved that my thinking was correct and why the result was different from what I had imagined. I am very depressed

Mar.31,2021

answer the first question first

what you want is to exist in the notice table, and exclude data from the notice table when searching.

you can do this without model association

first, take out the data in the notice table. You only need to take out the data in the user_id field

.
public function getData(Notice $notic , User $user)
{

    $userIds = $notic->all()->pluck('user_id')->toArray();

    $users = $user->query()->whereNotIn('id' , $userIds)->get();

    dd( $users);
}
The pluck method gets all the set values for the given key, and queries the data using whereNotIn through the query statement

the second question queries the notification list of the current user

not through self-increasing id to query the first 100 items of data,

if you are using the sql statement to query, use limit 0100

if you use laravel, there is a take method, such as take (100)

There seems to be no such method in
hasWhere laravel. Oh
Menu