skip to Main Content

I have 2 models: publictxt and Pulicetxtrecive

publictxt model:

class Publictxt extends Model
{
    protected $fillable=['sender','reciver','description','useridseen'];

    public function Publictxtrecives()
    {
        return $this->hasMany(Pulicetxtrecive::class,'publictxt_id', 'id');
    }
}

Pulicetxtrecive model:

protected $fillable=['publictxt_id','user_id','seen'];

public function publictxts()
{
    return $this->belongsTo(Publictxt::class);
}

I want to get the values ​​from the Publictxt that are available in the Pulicetxtrecive.
When a record is stored in the Publictxt, users are registered in the Pulicetxtrecive after viewing.

$pulictxtcount=Publictxt::where('reciver',Auth::user()->shift)->orwhere('reciver',1)->with('Publictxtrecives')->whereHas('Publictxtrecives',function($q){$q->where('seen', '=', 0);})->count();

this code doesn’t work.

3

Answers


  1. Chosen as BEST ANSWER

    I solved this problem:

    $pulictxtcount = Publictxt::where(function($q){$q->where('reciver',Auth::user()->shift)->orwhere('reciver',1);})->whereDoesntHave('Publictxtrecives', function($q){$q->where('user_id', Auth::user()->id); })->count();


  2. There are some conflicts in your database structure.

    You said when a user sees a letter the Publictxtrecives will be created.
    That means if a Publictxt has a Publictxtrecives that definitely has been seen .
    But there is a seen column in Publictxtrecives table.
    You should pick one.
    But anyway as this structure:

    $pulictxtcount=Publictxt::where(
     function($query){
      $query->where('reciver',Auth::user()
      ->shift)->orwhere('reciver',1);
    })
    ->Where(function($query)
      {
    $query->whereHas('Publictxtrecives',
      function($q){$q->where('seen',1);
                   }
    )->orWhereDoesntHave('Publictxtrecives');
    })
    ->with('Publictxtrecives');
    
    Login or Signup to reply.
  3. $pulictxtcount = Publictxt::with([
            'Publictxtrecives' => function($query){
                $query->where('seen', 0);
            }
            ])
            ->where('reciver', Auth::user()->shift)
            ->orwhere('reciver', 1)
            ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search