duanan1228 2018-09-03 11:49
浏览 76

如何在子查询中使用“With”laravel雄辩的条件?

I am trying to add new functionality to my project to be able to add tweet anonymously so I need to check if the public field = 0 , return the tweets without user or null user object !

How I can use "with" with OrWhere or something like that?

I have done that by merging the collections but I need more efficient query to do that when I try OrWhere()->with() that return user data for all collection I need the data in with() comes only with condition ? how to do that ? Thanks

$tweets = Tweet::where(function ($query) {
                    $query->where('public',1);
                    $query->where('hashtag_id', request('hashtag_id'));
                })->with([
                    'user' => function ($query) {
                        $query->select('id', 'name', 'username', 'photo', 'verified')->withTrashed();
                    },
                    'hashtag' => function ($query) {
                        $query->withTrashed();
                    },
                  ])->  where(function ($query) {
                        $query->whereRaw('DATE_ADD(created_at,INTERVAL expiration_time SECOND) >= "' . Carbon::now()->toDateTimeString() . '" or expiration_time = 0');})
                        ->withCount(['replies'])->orderBy('created_at', 'desc')->get();
$tweets2 = Tweet::where(function ($query) {
                    $query->where('public',0);
                    $query->where('hashtag_id', request('hashtag_id'));
                })->with(['hashtag' => function ($query) {
                        $query->withTrashed();
                    },
                    ])-> where(function ($query) {
                        $query->whereRaw('DATE_ADD(created_at,INTERVAL expiration_time SECOND) >= "' . Carbon::now()->toDateTimeString() . '" or expiration_time = 0');})
                        ->withCount(['replies'])->orderBy('created_at', 'desc')->get();

$tweets = $tweets->merge($tweets2);           
  • 写回答

1条回答 默认 最新

  • dsklzerpx64815631 2018-09-05 15:36
    关注

    I have figured this solution by adding method in the model check this condition and return depends on this condition

        $tweets = Tweet::Where(function ($query) {
            $query->where('hashtag_id', request('hashtag_id'));
        })->with([
            'hashtag' => function ($query) {
                $query->withTrashed();
            },
        ])->where(function ($query) {
            $query->whereRaw('DATE_ADD(created_at,INTERVAL expiration_time SECOND) >= "' . Carbon::now()->toDateTimeString() . '" or expiration_time = 0');})
            ->withCount(['replies'])->orderBy('created_at', 'desc')->get();
        foreach($tweets as $tweet){
            $tweet->user = $tweet->GetPublic();
        }
    

    That is the method in the model

    public function GetPublic()
        {
            if($this->public == 1)
            {   $user = $this->user;
                return $user;
            }
            else{
                return null;
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘