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

ameenmathers's avatar

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cid' in 'where clause' (SQL: select * from `category` where `cid` is null)

I am trying to displaying data on my contractsTable view, the fields from my contracts table are showing, but i am getting this error because i am trying to view data from categories table through my contracts category table which the fk is, let me share my code it would make more sense

my controller code. .... public function contractTable() { $contracts = contract::first(); $cid = $contracts->id; $categories = category::where('cid', $cid)->with('Contract')->get();

    return view('/contractTable',[
        'contracts' => $contracts,
        with('categories', $categories)
    ]);
}

my schema ...

    Schema::create('contract_categories', function (Blueprint $table){
    $table->increments('ccid');
    $table->integer('ctid');
    $table->integer('cid');
    $table->timestamps();


});

    Schema::create('contracts', function (Blueprint $table) {
        $table->increments('cid');
        $table->string('date_serviced');
        $table->string('contract_date');
        $table->string('location_desc');
        $table->enum('rate',array('hourly','per_turn','per_flight','per_push'))->nullable();
        $table->string('contract_expiration_date');
        $table->string('past_due_penalty');
        $table->string('billing_mail_address');
        $table->string('billing_email_address');
        $table->string('billing_contact_name');
        $table->string('billing_contact_phone');
        $table->string('account_number');
        $table->string('signed');
        $table->timestamps();
    });

    Schema::create('category', function (Blueprint $table) {
        $table->increments('ctid');
        $table->string('name');
        $table->timestamps();
    });

my blade file ... @foreach( $contracts as $contract)

            <tr class="text-white">

                <td><td>{{ $category->Contract->name }}</td></td>
                <td>{{ $company->contract->name }}</td>
                <td>{{$contract->date_serviced}}</td>
                <td>{{$contract->contract_date}}</td>
                <td>{{$contract->location_desc}}</td>
                <td>{{$contract->contract_expiration_date}}</td>
                <td>{{$contract->past_due_penalty}}</td>
                <td>{{$contract->billing_mail_address}}</td>
                <td>{{$contract->billing_email_address}}</td>
                <td>{{$contract->billing_contact_name}}</td>
                <td>{{$contract->billing_contact_phone}}</td>
                <td>{{$contract->account_number}}</td>
                <td>{{$contract->signed}}</td>
                <td><a class='btn btn-primary' href="{{url('update-contract/'. $contract->cid)}}">Update</a></td><br>
                <td><a type='submit' name='action' class='btn btn-danger' href="{{url('delete-contract/' . $contract->cid) }}">X</a></td>
                @endforeach
            </tr>
0 likes
14 replies
himanshu-dhiman's avatar

In Category Schema, there is no column named as 'cid'.

Schema should be like:

Schema::create('category', function (Blueprint $table) {
        $table->increments('ctid');
        $table->integer('cid');
        $table->string('name');
        $table->timestamps();
    });
Yorki's avatar

You don't have cid field on category table. Did you meant ctid?

$categories = category::where('ctid', $cid)->with('Contract')->get();

or

$categories = category::whereHas('Contract', function ($query) use ($cid) {
    $query->where('cid', $cid);
})->get();
ameenmathers's avatar

@Yorki I tried the first solution see the error i got

(E_RECOVERABLE_ERROR) Argument 2 passed to with() must be callable or null, object given, called in C:\xampp\htdocs\project\app\Http\Controllers\HomeController.php on line 107

36864's avatar

Looks to me like you've swapped cid and ctid around in your definitions. This is why you should avoid abbreviating your column names.

ameenmathers's avatar

@Yorki I tried the second solution and i am still getting errors

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Call to a member function getRelationExistenceQuery() on null

36864's avatar

Post your models, specifically, the relationships you've defined in them.

Yorki's avatar

You have messed up models. Try with lowercase in whereHas('contract'). You should name you models with camel case.

pantox's avatar

The error is maybe in your Controller code:

$cid = $contracts->id; 

//should be 

$cid = $contracts->cid;

Maybe the setup for the pivot-table is not working correct, it should be singular and in alphabetical order category_contract to work out of the box

The SQL Error will result from the wrong call

$categories = category::where('cid', $cid)->with('Contract')->get();

because 'cid' is no field of the category table

36864's avatar

Ignoring the capitalization issues for now, you've simply defined your relationships wrong. You're not even returning anything from the Contract relationship in your category class.

What you have here is a many-to-many relationship between category and contract. This should be defined like this:

//category model
public function contract() 
{
    return $this->belongsToMany(Contract::class, 'contract_categories');
}

//contract model
public function category()
{
    return $this->belongsToMany(Category::class, 'contract_categories');
}

pantox's avatar

This is the crap

$categories = category::where('cid', $cid)->with('Contract')->get();
  1. schema should be

       Schema::create('categories', function (Blueprint $table) {
           $table->increments('id');
           $table->string('name');
           $table->timestamps();
       });
    
    Schema::create('contracts', function (Blueprint $table) {
           $table->increments('id');
           $table->string('number');
           $table->timestamps();
       });
    
       Schema::create('category_contract', function (Blueprint $table) {
           $table->unsignedInteger('category_id');
           $table->unsignedInteger('contract_id');
       });
    
    
  2. The Model setup

    class Category extends Model
    {
        public function contract() {
            return $this->belongsToMany(Contract::class);
        }
    }
    
    class Contract extends Model
    {
        public function category() {
            return $this->belongsToMany(Category::class);
        }
    }
    
  3. Controller code

    // get contract with id=1 with all categories
    $contract = App\Contract::with('category')->where('id', 1)->first();
    
    // or get all contracts by category with id=1 
    $category = App\Category::with('contract')->where('id', 1)->first();
    
    // get all categories
    $categories = App\Category::all();
    
    return view('contractTable')
       ->with([
       'categories' => $categories,
       'contract' => $contract
       ]);
    
    
  4. View code

 ```php
 <h3>all categories</h3>
 @foreach($categories as $category)
     <ul>
         <li>{{ $category->name }}</li>
         </ul>
 @endforeach 

 <h3>contract number</h3>
 {{ $contract->number }}

 ```
pantox's avatar

or you fetch all contracts with categories and filter the collection by id without querying the database again


$contracts = App\Contract::with('category')->get();
$contract1 = $contracts->firstWhere('id', 1);
//...
$contract9 = $contracts->firstWhere('id', 9); 

//or a subset

$some_contracts = $contracts->where('id', '>', 1); 

ameenmathers's avatar

@pantox thannks for your suggestion, but if i alter my schema, the rest of the project will fall apart, also this is my models

contract_category model ...

pantox's avatar

like @36864 suggested, you have to be explicit in your definition of the manyToMany relationship

with your schema you have to define your models (Category and Contract) like this

class Category extends Model
{
    public function contract() {
        return $this->belongsToMany(Contract::class, 'contract_categories', 'ctid' , 'cid');
    }
}

class Contract extends Model
{
    public function category() {
        return $this->belongsToMany(Category::class, 'contract_categories', 'cid', 'ctid');
    }
}

Please or to participate in this conversation.