جستجو در لاراول

پرسیده شده
فعالیت 937 روز پیش
دیده شده 365 بار
0

باسلام و عرض خسته نباشید

یه مشکلی که دارم برای زمانی که میخوام چند کوئری جستجو رو در لاراول داشته باشم کوئری دوم در نتایح اعمال نمیشه اما زمانی که جدا جدا صدا زده میشن هرکدوم به درستی کار میکنن

 

مایگریشن جدول نظرات:

        Schema::create('comments', function (Blueprint $table) {
            $table->id();

            $table->string('name');
            $table->string('email');
            $table->unsignedBigInteger('commentable_id');
            $table->string('commentable_type');
            $table->unsignedBigInteger('parent_id')->default(0);
            $table->boolean('approved')->default(false);
            $table->text('content');
            $table->unsignedBigInteger('likes')->default(0);
            $table->string('ip', 128);

            $table->timestamps();
        });

دریافت:

    public function getAll()
    {
        $comments = Comment::query();
        if ($keyword = request('search')) {
            $comments->where('content', 'LIKE', "%{$keyword}%")
                ->orWhere('name', 'LIKE', "%{$keyword}%")
                ->orWhere('email', 'LIKE', "%{$keyword}%");
        }

        $approved = request('approved');
        if (
            !is_null($approved)
            && $approved !== 'all'
            && in_array($approved, ['true', 'false'])
        ) {
            $approved = ($approved === 'true') ? true : false;
            $comments->where('approved', $approved);
        }

        return $comments->latest()->paginate($this->commentPerPage);
    }

توی این کد زمانی که هم جستجو داریم و هم میخواهیم فیلتر کنیم بخش approved اعمال نمیشه در صورتی که $comments در dd مقدار دهی شده:

Illuminate\Database\Eloquent\Builder {#1613 ▼
  #query: Illuminate\Database\Query\Builder {#1616 ▼
    +connection: Illuminate\Database\MySqlConnection {#1432 ▶}
    +grammar: Illuminate\Database\Query\Grammars\MySqlGrammar {#1433 ▶}
    +processor: Illuminate\Database\Query\Processors\MySqlProcessor {#1434}
    +bindings: array:9 [▶]
    +aggregate: null
    +columns: null
    +distinct: false
    +from: "comments"
    +joins: null
    +wheres: array:4 [▼
      0 => array:5 [▼
        "type" => "Basic"
        "column" => "content"
        "operator" => "LIKE"
        "value" => "%محمد%"
        "boolean" => "and"
      ]
      1 => array:5 [▼
        "type" => "Basic"
        "column" => "name"
        "operator" => "LIKE"
        "value" => "%محمد%"
        "boolean" => "or"
      ]
      2 => array:5 [▼
        "type" => "Basic"
        "column" => "email"
        "operator" => "LIKE"
        "value" => "%محمد%"
        "boolean" => "or"
      ]
      3 => array:5 [▼
        "type" => "Basic"
        "column" => "approved"
        "operator" => "="
        "value" => false
        "boolean" => "and"
      ]
    ]
    +groups: null
    +havings: null
    +orders: null
    +limit: null
    +offset: null
    +unions: null
    +unionLimit: null
    +unionOffset: null
    +unionOrders: null
    +lock: null
    +beforeQueryCallbacks: []
    +operators: array:31 [▶]
    +useWritePdo: false
  }
  #model: App\Models\Comment {#1614 ▶}
  #eagerLoad: []
  #localMacros: []
  #onDelete: null
  #passthru: array:19 [▶]
  #scopes: []
  #removedScopes: []
}

چطوری میتونم به شکل زنجیر وار از where و orWhere استفاده کنم برای سناریو های مختلف؟

فایل پیوست

محمد گازری
محمد گازری

29 شهریور 00

0
حذف شده

سلام.
اولین if رو تغییر بده به شکل زیر:

if ($keyword = request('search')) {
    $comments->where(function ($query) {
               $query->where('content', 'LIKE', "%{$keyword}%")
                    ->orWhere('name', 'LIKE', "%{$keyword}%")
                    ->orWhere('email', 'LIKE', "%{$keyword}%");
            });
}
فایل پیوست

محسن موحد

توسط

محسن موحد

2 مهر 00