Simon Suh Simon Suh - 4 months ago 18
SQL Question

How do I search a query in laravel where I'm using a foreign key that is not named 'id'?

I currently have this laravel code

$theCompany = App\bought::find($company->theName);

and I get this error

QueryException in Connection.php line 713:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from
= Mike limit 1)

the column I'm matching is the column named 'theName' in the table 'boughts'. I'm not using an id with numbers, and instead I'm using string variables for the 'theName' column. How can I search for it using the customer column name?


If you use the "find" method of your model, it will search using the "id" field by default. However if you want to search for other column, you can use the "where" method:

App\bought::where('theName', $company->theName)->get();

For further information you can check this link.