[SOLVED] Laravel 8 – Eloquent Queries Without Database Migration

Issue

I’m a newbie with Laravel, but old school PHP developer. I’m in the process of adapting my framework to the Laravel framework.

My main goal at this moment is to use eloquent queries without doing a DB schema and migration inside Laravel, as I have my database is built already, up and running.

So far, I’ve been successful in using eloquent queries to interact with my database. Here’s a part of the code that’s working without any problems:

$results = DB::table('my_table')->where('id', '=', 123)->orderBy('title')->get()->toArray();

The problem is when I try to limit the fields I want to retrieve from the database. So far, I’ve tried:

->select('id', 'title', 'description')

and

->get('id', 'title', 'description')

It returns me an error like so:

Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘ title’ in ‘field list’ (SQL: select id, title, description from categories where id_parent = 781 and activation = 1 order by title asc)
A column was not found
You might have forgotten to run your migrations. You can run your migrations using php artisan migrate.
Pressing the button below will try to run your migrations.**

I know that I could probably accomplish this through raw queries, but my intention is to use some of Laravel´s features that are widely used in tech companies. So, is it even possible to accomplish what I’m trying to using eloquent queries without having to build a DB schema and all migration process? And if so, what would be the way?

One of the main reasons I don’t want to develop a migration and schema is that my database has a prefix for the tables inside my framework that I change according to each project / client and I don’t want to spend some time on how I could build a migration / schema with a prefix and so on.

I´ve checked this stackoverflow link:
Laravel without Eloquent & database migrations?
But it wasn’t quite what I was looking for.

Edit – Solution:
The problem was with a bug that had extra space on the column name. So, the following worked:

$results = DB::table('my_table')->select('id', 'title', 'description')->where('id', '=', 123)->orderBy('title')->get()->toArray();

Solution

please check your column names.
they have to be with no wihtespaces

Answered By – Milad Elyasi

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.