Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ErikRobles's avatar

Using not equal to in query within Controller

I have a task management app and I am trying to display all the tasks except where the 'task_status' is equal to 'Completed'. I am unclear on how to do this. I tried the following which did not work.

        $task =  Task::find($id)->where('task_status', '!=', 'Completed')->get();
        return view('tasks.show')->with('task', $task);

I still get all the tasks including the Completed ones. Any help on this would be greatly appreciated. Thank you.

0 likes
31 replies
Braunson's avatar

Your query is finding by ID then trying to filter but it'll only return the result for the ID you've provided. If you want to search for all where task_status doesn't equal Completed then you want this.

Task::where('task_status', '!=', 'Completed')->get();

This is assuming task_status is a column on the Task model table as well as your task_status is a capitalized Completed

Nakov's avatar

@erikrobles why you use find() together with the where clause?

And is your task_status exactly that word or is it maybe lower case completed?

$task =  Task::where('id', $id)->where('task_status', '!=', 'completed')->get();
ErikRobles's avatar

Thank you @nakov and @braunson . Unfortunately neither change worked. I have the string 'Completed' in the 'task_status' column within the tasks table. Still unclear how to do this. Thanks for your help.

Nakov's avatar

@erikrobles and have you tried using a db client app and running this query:

select * from tasks where task_status != 'Completed';

Does it returns correct? Do you have any global scope applied to your class or anything. This is such a simple query to cause you any trouble :)

ErikRobles's avatar

Yes. I ran this query directly in my phpmyadmin and got just the records that did not have the status of 'Completed'. Even tried

        DB::select('*')->from('tasks')->where('task_status', '!=', 'Completed')->get();

and that did not work either.

Nakov's avatar

That's weird :)

$task =  Task::whereRaw('task_status != ?', ['Completed'])->get();

Does this work? :)

ErikRobles's avatar

Damn. Doesn't work either. This is strange. I must be doing something really wrong. I am wondering what I might have missed because as a whole the app works as expected. Just not ignoring my Completed tasks.

Nakov's avatar

Can you share your Task model please?

ErikRobles's avatar
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Image;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;



class Task extends Model
{
    protected $fillable = [
        'task_name', 'task_priority', 'task_assigned_to', 'task_assigned_by', 'task_description', 'task_to_be_completed_date', 'task_status',
        'task_notes', 'task_due'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function images()
    {
        // return $this->hasMany('App\Image');
        return $this->hasMany(Image::class);
    }

    public static function boot()
    {
        parent::boot();
        self::deleting(function ($task) {
            foreach ($task->image ?: [] as $image) {
                $image->delete();
            }
        });
    }

    public function scopeSearch($query, $s)
    {
        return $query->where('task_priority', 'like', '%' . $s . '%')->orWhere('task_status', 'like', '%' . $s . '%')->orWhere('task_assigned_to', 'like', '%' . $s . '%');
    }
}
Nakov's avatar

@erikrobles this is your full class? Are you connected to the right database? This makes no sense at all :)

Nakov's avatar

@erikrobles and then is it really Task::where('task_status', '!=', 'Completed')->get() that you are trying or you are adding the ->search() scope to it? Because with that I can see why you get all the results. Using like and using orWhere will cause issues when not used properly.

MichalOravec's avatar

There is one more option, to use <> instead of !=

$tasks = Task::where('task_status', '<>', 'Completed')->get();
jlrdw's avatar

Or you sure one of the above did not work, maybe you did not clear cache.

Clear those temp views as well.

ErikRobles's avatar

I ran php artisan cache:clear and tried all of the above ideas with no success. also php artisan view:clear

MichalOravec's avatar

Your scopeSearch has to be like this

public function scopeSearch($query, $s)
{
    return $query->where(function($query) use ($s) {
        $query->where('task_priority', 'like', $term = "%{$s}%")
            ->orWhere('task_status', 'like',  $term)
            ->orWhere('task_assigned_to', 'like', $term);
    });
}

https://laravel.com/docs/8.x/queries#logical-grouping

ErikRobles's avatar
public function show()
    {
        // $task =  Task::find($id)->where('task_status', '!=', 'Completed')->get();
        // $task =  Task::where('task_status', '!=', 'Completed')->get();
        // $task =  Task::where('task_status', '<>', 'Completed')->get();
        $task =  DB::select('*')->from('tasks')->where('task_status', '!=', 'Completed')->get();
        // $task =  Task::whereRaw('task_status != ?', ['Completed'])->get();

        return view('tasks.show')->with('task', $task);
    }
MichalOravec's avatar

When you dd($tasks)

$tasks = Task::where('task_status', '<>', 'Completed')->get();

dd($tasks);

Do you still see it there?

jlrdw's avatar

Those above replies are 100% correct, check spelling, are you querying correct database, etc.

You have a letter off somewhere.

And show a dd result.

ErikRobles's avatar

Ok. something is wrong here as I tried a dd and am not getting any data dumps. So I have to dig here.

Nakov's avatar

You are probably not even reaching that method, maybe start with your routes file, and check if you are hitting this endpoint at all :)

Or maybe you use View Composer through which you are overriding the tasks variable. Some global scope is in play.

ErikRobles's avatar
@extends('layouts.master')
@section('content')
<div class="container">
    <a href="/home" class="btn bg-purple mb-4">Go Back</a>
    <div class="card p-3">
        <div class="row">
            <div class="col-md-4 col-sm-12">
                <h3>Task</h3>
                <p>{{ $task->task_name }}</p>
                <h3>Assigned On:</h3>
                <p>{{ $task->created_at->format('m/d/Y') }}</p>
                <h3>Assigned To:</h3>
                <p>{{ $task->task_assigned_to }}</p>
            </div>
            <div class="col-md-4 col-sm-12">
                <h3>Task Description</h3>
                <p>{{ $task->task_description }}</p>
                <h3>Priority</h3>
            <p>{{ $task->task_priority }}</p>
                <h3>Status</h3>
                <p>{{ $task->task_status }}</p>
            </div>
            <div class="col-md-4 col-sm-12">
                <h3>Test Environment Date:</h3>
                <p>{{ $task->task_to_be_completed_date }}</p>
                <h3>Notes</h3>
                <p>{{ $task->task_notes }}</p>
                <h3>Action</h3>
                <div style="display: inline;">
                    <a href="/tasks/{{$task->id}}/edit" class="btn btn-sm btn-primary mr-2">
                        <i class="fa fa-edit"></i> Edit
                    </a>
                </div>
            <form style="display: inline;" action="/tasks/{{ $task->id }}" method="POST" class="">
                        @csrf
                        @method('DELETE')
                      <button type="submit" class="btn btn-danger btn-sm ml-1 mr-1">
                        <i class="fa fa-trash"></i> Delete
                      </button>
                    </form>

            </div>

            <div class="col-md-12">
                <h3>Due:</h3>
                 <p>{{ $task->task_due }}</p>
            </div>

            <div class="col-md-12">
                <h5>Images</h5>
                <hr />
                  <div class="row">
                    @if($task->images->count()>0)

                    @for($i=0; $i < count($images = $task->images()->get()); $i++)
                    {{-- @forelse($task->images as $image) --}}
                    <div class="col-lg-4 col-md-6 col-sm-12">
                    <form action="{{ route('delete.image', ['image' => $images[$i]['id']])}}" method="Post">
                        {{ csrf_field() }}
                        @method('delete')
                        <button type="submit" class="btn btn-danger btn-sm mb-2 mt-3"><i class="fa fa-trash"></i> Delete</button>
                    </form>
                        @if (in_array($extension = pathinfo($images[$i]['name'], PATHINFO_EXTENSION), ['jpg', 'png', 'bmp', 'webp']))
                            <a data-toggle="" href="/storage/upload/{{ $images[$i]['name'] }}" target="_blank">
                                <img src="/storage/upload/{{ $images[$i]['name'] }}" class="image-fluid w-50">
                            </a>
                        @else
                            <a href="/storage/upload/{{ $images[$i]['name'] }}" target="_blank">
				                <img src={{ asset("/img/{$extension}.jpg") }} class="image-fluid" style="max-width: 110px;">
                           </a>
                        @endif
                    </div>
                    @endfor
                    @else
                    <p class="ml-3 mb-1">No images found</p>
                    @endif
                </div>
                  </div>
                <br />
              </div>
        </div>
    </div>
</div>
@endsection


Nakov's avatar

@erikrobles there is no way to use $task->task_name while you attach ->get() to the query, as that returns collection and not a single instance. Debug your request please. Start with the routes file.

MichalOravec's avatar

With this

$tasks = Task::where('task_status', '<>', 'Completed')->get();

you get a collection and not a single model.

ErikRobles's avatar

A big thanks to all those who tried to help me. I appologize. The task was being controlled via the home controller. I did add the != to the index request and got what I was looking for.

public function index(Request $request)
    {
        $s = $request->input('s');
        $tasks = Task::where('task_status', '!=', 'Completed')->orderBy('created_at', 'desc')->search($s)->paginate(4);

        return view('home')->with('tasks', $tasks);
    }

Please or to participate in this conversation.