Laravel QueryException / SQLSTATE[42S02] - php

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';

Related

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'

Fatal Exception Route::get does not work Laravel 5

I´m trying to get the date from an user in my DDBB, them, I have 2 customer in a table.
The schema is called - customers
The table is called - names
And names has two columns, id and name, its very simple. I have added two persons in my table, Tony and Bob.
In laravel, in the app folder, I have created a new php file, and I put this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model{
}
And I have this code in my .env file:
DB_HOST=localhost
DB_DATABASE=customers
DB_USERNAME=root
DB_PASSWORD=root
Then, I just want get my first customer, Tony, and in routes.php I have write this:
Route::get('customer', function(){
$customer = App\Customer::find(1);
print_r($customer);
});
When I run the application, I get this message:
FatalErrorException in routes.php line 23:
Class 'App\Customer' not found
I would like to get something, but I don´t know what happen. Thanks.
$customer = new Customer;
$customer->name = $request->name;
$customer->mobile_number = $request->mobile_number;
$customer->mail = $request->mail;
$request->name.......this name and $request->Mobile_number,$request->mail is .... form field name.
It says - Class 'App\Customer' not found.
Seems your class Customer is not loaded. Make sure, your Customer model is in App folder. Also the file name must be Customer.php. Also the class name must start with C(Capital) - Customer. And also make sure, you have defined the namespace as follows in Customer model.
namespace App;
Example
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $table = 'customer';
}
And try like this and see ?
Route::get('customer', function(){
$customer = \App\Customer::find(1);
print_r($customer);
});

Calling Correct Database Table with Laravel all()

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.

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