Calling Correct Database Table with Laravel all() - php

I am just getting started with Laravel 5. I am trying to set up a basic page that retrieves all data from a database table. I have a table call people and I have a controller called ContactController.php. The controller has the following code:
<?php namespace App\Http\Controllers;
use App\Contact;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ContactController extends Controller {
public function index()
{
$people = Contact::all();
return $people;
}
}
When I access my page, I get the following error:
QueryException in /home/vagrant/sites/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php line 614:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.contacts' doesn't exist (SQL: select * from `contacts`)
Why is it trying to access homestead.contacts? My table is called people. I never created a table called contacts. Where is it drawing the table name from and how do I correct this?
Thanks.

Quote from the docs:
Note that we did not tell Eloquent which table to use for our User model. The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified.
So if you define nothing else, Laravel will take the plural snake_case version of your class name:
Contact => contacts
FooBar => foo_bars
To fix your issue either change the model name accordingly. In this case Person will let Laravel search for the table people.
Or explicitly specify the table in your model:
class Contact extends Eloquent {
protected $table = 'people';
}

Do you have the right table name in your app/Contact.php model?
protected $table = 'people';
Or are you using the wrong model?

Add the following line to your Contact model (app/Contact.php).
protected $table = 'people';
This will work.

Related

Eloquent (WITHOUT Laravel) get data from table with dynamic name

I'm sure this is a totally simple question but for the life of me I'm stuck here- we're using Eloquent outside of Laravel due to PHP restrictions. I have a support ticket tracking app that I'm trying to update.
The data structure of this app is such that each ticket is assigned a UUID on submission and a table with that UUID as its name is generated and all changes to the ticket are tracked as new entries in that table.
Following some tutorials on Eloquent I got our models and controllers set up and working but for each one I see that I'm defining the table name in the model itself. IE our ticket model is
namespace Models;
use \Illuminate\Database\Eloquent\Model;
class Ticket extends Model {
protected $table = 'tickets';
protected $fillable = [table columns here];
}
and anything called in the tickets controller correctly and successfully reads and writes data to our tickets table.
So... my question is: how would I go about reading/writing/creating/deleting those previously mentioned UUID tables?
I've tried the built in table selector (ie- DB::table(uuid here) and DB::setTable(uuid here) but to no avail. I get Fatal error: Call to undefined method Models\Database::setTable()
What I'm after is a model/controller that I can reuse for ANY dynamically-named table.
You could create a generic model and dynamically set the table name, like this:
namespace Models;
use \Illuminate\Database\Eloquent\Model;
class FormerUUIDTicket extends Model {
protected $table = 'some_table';
protected $fillable = [table columns here];
}
class SomeController
{
public function someAction()
{
$uuid = $_POST['uuid_field']; //some uuid, the table name
$model = new FormerUUIDTicket;
$model->setTable($uuid);
return $model->get(); //do anything using eloquent with proper table
}
}
Make sure that you always set the table name before use, or it will fail. Don't use static function either, for the same reason.

laravel timestamps not working, delete the model but still can retrieve value?

this is my controller
<?php
namespace App\Http\Controllers\HR;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\models\HR\attendance;
class attendanceController extends Controller
{
public function select()
{
return attendance::get();
}
}
this is my route web.php
Route::get('attendance/select/','HR\attendanceController#select');
this is my model attendance.php
<?php
namespace App\models\HR;
use Illuminate\Database\Eloquent\Model;
class attendance extends Model
{
public $timestamps = false;
}
when i update on attendance model i had error unknown column updated_at which means i have to disable timestamps then i add
public $timestamps = false;
but it didn't work, i'm still getting unknown column updated_at. then i delete it
this is my directory structure with laravel, there are no attendance.php in HR folder neither elsewhere. But, i can retrieve value from it, how is that possible?
By default, Eloquent expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false:
you are missing update_at and created_at in your table attendance
in laravel, when you update or create data, laravel will default add someting like this set update_at="YYYY-mm-dd HH:ii:ss"
so, you should add update_at and created_at to every table.
laravel 5.6 document
i'm sorry. it is my mistake, i have copy attendance.php to other folder and it seems composer autoload_classmap.php point it to that folder

SQLSTATE[42S02]: Base table or view not found - cant find the typo Laravel

I am new to the laravel framework. I tried to pass data from the database to my view but it gives me the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.aanvragens' doesn't exist (SQL: select * from aanvragens).
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Aanvragen;
use Carbon\Carbon;
class aanvragenController extends Controller
{
public function index() {
$aanvragen = Aanvragen::all();
return view('aanvragen.index', compact('aanvragen'));
}
}
Route
Route::get('/overzicht', 'aanvragenController#index')->name('aanvragen.index');
Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Aanvragen extends Model
{
}
I know my database connection is working as I can migrate my migrations (the default ones). What is strange to me is that I can't recall I typed aanvragens somewhere. Not in my controller, view, model etc. My database table name is aanvragen. Is there someone who can help me? Maybe I forgot to include something or made a typo..
This is the normal convention, singular model names, and plural table names.
You can override this in your model.
public $table = 'aanvragen'

Laravel QueryException / SQLSTATE[42S02]

I'm new to Laravel and have an error.
When I try to check my page, I get this error:
QueryException in Connection.php line 713: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_tcc.colaboradors' doesn't exist (SQL: select * from colaboradors order by id_colaborador asc)
My table name is 'colaboradores', I know the error is 'colaboradors', but I dont know where is that 'colaboradors' in my project files.
I need to find in the code to fix, but I dont have idea what file take care of sql in laravel.
My model for colaboradores:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Colaborador extends Model {
protected $fillable = array('id_colaborador','nome','rg','orgao_expedidor','cpf','estado_civil','sexo','nome_pai','nome_mae','natura‌​lidade','data_nascimento','login','senha','siape','pis','rua','numero','bairro',‌​'cidade','estado','cep','telefone_fixo','telefone_celular','telefone_comercial','email');
}
My controller : http://pastebin.com/QYgRBRrc
The "snake case", plural name of the class will be used as the table
name unless another name is explicitly specified.
So this is your issue.
You have two options:
Either rename your model class or
Add in your model $table = 'colaboradores';

Database Table custom naming convention Laravel Eloquent

I'm trying to retrieve data from a table called completion_date using Eloquent. My Model name is Completion and as per laravel documentation, i should declare a (protected) table name or else Eloquent will use 'completions' as the default table name. So i did this declaration.
I'm getting problems with my Controller since i dont know which name to use to refer to my Model when i'm making the View. I'm getting an InvalidArgumentException that View [completion.lsz] not found. if i just use my model name to make the View.
Error:
InvalidArgumentException thrown with message "View [completion.lsz] not found."
Can someone pls help out?
Model
<?php
//Model (file name: Completion)
class Completion extends Eloquent{
protected $table = 'completion_date';
public $timestamps = false;
}
Controller
class CompletionController extends BaseController {
public function index() {
$lsz = Completion::all();
return View::make('completion.lsz', ['completion' => $lsz]);
}
}
Route
Route::get('/Completion', 'CompletionController#index');
View names in Laravel work like a path. The . gets converted to a /
That means, your view resolves to the file app/views/completion/lsz.blade.php (or app/views/completion/lsz.php without Blade)
So you either have to change the name of your directory in the views folder to "completion" or change the view make command to:
View::make('lsz.lsz', ['completion' => $lsz]);
The error message says that the view file completion/lsz.blade.php was not found.
It's not related to the model or database.

Categories