I'm learning about Lumen Framework and reading that tutorial: Developing RESTful APIs with Lumen
Currently is working, but when i access my api
http://example.com/api/accounts/2
it returns
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'youframe.accounts' doesn't exist (SQL: select * from
accounts
whereaccounts
.id
= 2 limit 1)
I have a model file called Account.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
protected $fillable = [
'username', 'email', 'password', 'type'
];
protected $hidden = [];
}
and a controller file called AccountController.php
<?php
namespace App\Http\Controllers;
use App\Account;
use Illuminate\Http\Request;
class AccountController extends Controller
{
public function getAccount($id, Request $request)
{
$this->validate($request, [
'token' => 'required'
]);
return response()->json(Account::find($id), 400);
}
}
i have no table called accounts
. My only table is account
, from where it's calling accounts
?
Add table name to your model to associate your model with backend DB table.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
protected $fillable = [
'username', 'email', 'password', 'type'
];
protected $hidden = [];
protected $table = 'account';
}
If you don't mention the table name, Laravel will assume the plural form of your model name as the table name. For more reference, see Eloquent Model Conventions.
UPDATE
if you want to select only certain columns, use select command in your eloquent query.
$rows = Account::select(['column_name_1','column_name_2'])->get();
OR if you want them in an array
$rows = Account::select(['column_name_1','column_name_2'])->get()->toArray();