0

I have two databases named users and calls.

Calls table

<?php

public function up()
{
    Schema::create('calls', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->timestamps();
        $table->text('terminal_id', 20);
        $table->text('terminal_name', 100);
        $table->text('fault_description');
        $table->string('call_status', 10)->default('New call');
        $table->string('pending_on', 20)->nullable();
        $table->text('closed_on', 20)->nullable();
        $table->text('closed_by', 50)->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

CallsController

public function index()
{
    $calls = Call::orderBy('created_at', 'desc')->get(); 

    return view('pages.newCall')->with('calls', $calls); 
}

public function viewCall($id) { 
    $calls = Call::find($id);

    return view('pages.viewCall')->with('calls', $calls);
}

Currently, the CallsController is returning all the rows in the calls table, but I want it to return only the rows that have the property 'New call' on the call_status column in the calls table. How do I do this from the CallsController?

4
  • 4
    $calls = Call::where('call_status', 'New call')->orderBy('created_at', 'desc')->get(); Commented Nov 26, 2018 at 14:25
  • thanks ...problem solved Commented Nov 26, 2018 at 14:29
  • but what if the column call_status does not have any value, how do I reference it in $calls = Call::where('call_status', 'New call')->orderBy('created_at', 'desc')->get(); i.e removing the value New call Commented Nov 26, 2018 at 14:32
  • You could do where('call_status', '!=', 'New call') to get the rows where the call status is not New call. Commented Nov 26, 2018 at 14:42

1 Answer 1

1

If you want to get rows which have

1.call_status equal to 'New Call' :

$calls = Call::where('call_status', 'New call')->orderBy('created_at', 'desc')->get();

2.call_status not equal to 'New Call' :

$calls = Call::where('call_status', '<>', 'New call')->orderBy('created_at', 'desc')->get();

You could use != in place of <> above.

3.call_status equal to NULL (Empty) :

$calls = Call::whereNull('call_status')->orderBy('created_at', 'desc')->get();

4.call_status equal to 'New Call' or NULL :

$calls = Call::where('call_status', 'New call')->orWhereNull('call_status')->orderBy('created_at', 'desc')->get();

Laravel has an ORM which is Eloquent and it uses a query builder named as Fluent. For more on query building check official doc.

Sign up to request clarification or add additional context in comments.

1 Comment

Number two is throwing a Syntax error or access violation error. So, used $calls = Call::where('call_status', '<>', 'New call')->orderBy('created_at', 'desc')->get(); instead of $calls = Call::whereNull('call_status', '<>', 'New call')->orderBy('created_at', 'desc')->get();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.