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.
Related
I'd like to be able to modify the Auth->user() data that Laravel 5.6 uses. I have table called settings with a column called user_id in it that corresponds to a user id.
I tried modifying app\User.php and adding a __construct function:
public function __construct() {
$this->settings = Settings::where('user_id',
Auth->user->id()
)->first();
}
And I created a file app\Settings.php with the following:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class settings extends Model
{
protected $table = "settings";
}
However I'm getting a user error on the Auth->user()->id line in User.php, although I'm sure thats the correct way to reference it?
How can I load the data from the settings table to the User class?
You can just use load() method to lazy load the relation:
auth()->user()->load('settings');
You need to do this just once per request, in a middleware for example. Then you'll be able to use the data in any part of your app:
{{ auth()->user()->settings->theme }}
Of course, to make this work you need to define relationship in the User model, for example:
public function settings()
{
return $this->hasOne(Settings::class);
}
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.
I have created an example model:
class ExampleModel extends Eloquent {
protected $table = 'example_model';
}
Then I tried to call ExampleModel::all() which returned an error function all() does not exist (seems like the class is created but without Eloquent functions). After changing model name (and file name) to Examplemodel (now the model name is one word) it works. The problem is not that I misspelled something as I have checked that like a hundred of times and my file+class names are matching.
Am I missing something about auto loading?
I am on windows so I also tried changing file's name to Examplemodel without changing the class name but it still seems to load the class without Eloquent methods.
Did you do Autoload (dumpautoload)?
Because of this case works for me.
Do composer dump-autoload
As the following works for me:
class ExampleTest extends \Eloquent {
protected $table = 'users';
}
Then on the routes file I have:
Route::get('/', function()
{
$data = ExampleTest::all();
return $data;
});
This gives me the data from my users table.
create a file example.php in models folder and then write this:
class Example extends Eloquent {
protected $table = 'example_model';
}
and then try:
Example::all();
taken from here
I have found a problem. I had a migration with class same as my model. Laravel is auto loading models with migrations and it thought I was referring to migration instead of a model.
After fixing that of course I had to run dumpautoload composer command.
I have just a table on my database named "ficha_seg".
The name of my model file is "Ficha.php" and the name of the controller is "FichasController.php".
So, why i'm getting the error:
Error: Table fichas for model Ficha was not found in datasource default.
after configured my index() method of controller like this:
public function index() {
$this->set('ficha_seg', $this->Ficha->find('all'));
}
By default, the model uses the lowercase, plural form of the model’s class name for the database table name.
If you need to use another table name for your model, you can use the useTable attribute:
class Ficha extends AppModel
{
public $useTable = 'ficha_seg';
}
See http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable
and Model conventions in the Cookbook
To follow CakePHP conventions your table name should be in plural: ficha_segs and your model name should be 'FichaSeg'.
If you don't want to follow it for any reason, do what #nlcO says.
I'm just studying CakePHP so sorry for any obvious mistakes.
I have created model class and changed default table name.
class Weathers extends AppModel {
public $tablePrefix = 'weather_';
public $useTable = 'forecasts';
function saveCountries($countries){
...
}
}
And my controller function
if (!$this->loadModel('Weather'))
exit;
$Weather = $this->Weather;
$Weather->saveCountries($countries);
I'm getting error on $Weather->saveCountries($countries);
Error: Table weathers for model Weather was not found in datasource
default.
Please help find out what I do wrong.
The Model class you defined is Weathers not Weather. So just change the class name Weather instead of Weathers and this is done.
Note the declaration of your Model class.
You've called it Weathers.
There is no problem with this. However, as you are trying to then load the model Weather (not the lack of plural in this case) CakePHP is constructing a model dynamically for you, instead of using your Weathers (plural) class.
The CakePHP standard is to use a singular name for models. I suggest that you rename your model class to Weather to avoid this issue. Once you make this change, the code that you have for loading the model will work as intended.