I'm starting to learn Laravel. I've run through the example instructions from the site successfully and now I'm trying a second run through and I'm running into an issue.
I'm trying to connect to a database called zipCodes and has one table called zipCodeDetails.
In my Laravel project I have a model containing the following code:
<?php
class ZipCodeDetails extends Eloquent {}
And in my routes.php file I have the following code:
Route::get('zipCodes', function (){
$zipCodes = ZipCodeDetails::all();
return View::make('zipCodes')->with('zipCodes', $zipCodes);
});
The error I'm running into is when I try to load the URL:
http://localhost:8888/zipCodes
In my browser I'm getting the error code:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'zipcodes.zip_code_details' doesn't exist (SQL: select * from `zip_code_details`)
There's nothing written in my code where I define the database zipCodes as zipcodes or the table zipCodesDetails as zip_code_details. Something in laravel is changing the database and table names.
Does anyone know why this is happening and how I can prevent it? I don't want to just rename the database or table names because while that may get me by in testing it's not a viable solution in practice.
Thanks!
This is the behaviour that uses if no table is being explicitly defined. In your ZipCodeDetails class, you can set the table name that this model will be using.
class ZipCodeDetails extends Eloquent
{
protected $table = 'zipCodesDetails';
}
Related
I'm new to Lumen (ver. 8.3.4) and I got a strange issue during my tests.
In my DB I have the table "Pippo"; to query it I created the model App\Models\Pippo and the controller App\Http\Controllers\PippoController.php, that includes the aforementioned model.
To route the requests, in web.php I added the line:
$router->post('getdomain', 'PippoController#getdomain');
Now, in 'getdomain' function I've a simple
$var = Pippo::all();
but when I try to call it, I get the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'authserver.pippos' doesn't exist (SQL: select * from pippos)
I searched and researched multiple times in the code, but I don't understand why Lumen adds the 's' character to the table name.
Any suggestion?
you can put in modal protected $table = 'pippo'; To avoid this error
I am studying Laravel Framework for few hours and i am trying to do what's being done in the tutorial i am watching. I am executing a query through routes.php and it's giving me a different output.
My database has only 1 table and it is named 'customer' and i have a model named 'customer' and a controller named 'CustomerController'
My routes.php code is this
Route::get('customer', function() {
$customer = FirstLaravelApplication\Customer::find(1);
echo '<pre>';
print_r($customer);
But the localhost is giving me an error and it says i don't have any 'customers' table, it automatically added a letter 's' in the end of the table instead of 'customer' only. i really don't have any 'customers' table i don't know why it is passing the wrong name of the table but my code only says 'customer'.
i would appreciate any help! Thanks all!
Laravel/Eloquent ORM uses this convention, as do many ORMs. They pluralize table names.
Open up the Customer.php model and add in:
class Customer extends Model {
// Add this
protected $table = 'customer';
However, it's usually easier to stick with the framework's conventions.
https://laravel.com/docs/5.3/eloquent#eloquent-model-conventions
So, going into the problem straight away. someone told me that we dont need to make a pivot table if we only want to have ids of the table. laravel can itself handle this situation. I dont know how this works. I have a table community and another table idea. relation is like this;
One community can contain many ideas and an idea can be found in many
communities.
Relation in idea Model:
public function community() {
return $this->belongsToMany('App\Community')->withTimestamps();
}
Relation in community Model:
public function idea() {
return $this->belongsToMany('App\idea');
}
Now i want to fetch all the records related to a single community to show on its page Let's say the community is Arts.
Here is Controller function:
public function showCommunities($id) {
$community = Community::findOrFail($id)->community()->get();
return view('publicPages.ideas_in_community', compact('community'));
}
When i attach ->community()->get() to the Community::findOrFail($id) Then it throws the error
SQLSTATE[42S02]: Base table or view not found laravel
Any help would be appreciated.
Edit:
Logically, this piece of code Community::findOrFail($id)->community()->get() should be like this Community::findOrFail($id)->idea()->get(). Now it is true but it has little issue. it throws an error
Fatal error: Class 'App\idea' not found
The way you define the many-to-many relation looks ok - I'd just call them communities() and ideas(), as they'll return a collection of objects, not a single object.
Make sure you use correct class names - I can see you refering to your model classes using different case - see App\Community and App\idea.
In order to find related models, Eloquent will look for matching rows in the pivot table - in your case it should be named community_idea and have 3 fields: community_id, idea_id and autoincrement primary key id.
With that in place, you should be able to get all ideas linked to given community with:
$ideas = Community::findOrFail($communityId)->ideas;
If you need communities linked to given idea, just do:
$communities = Idea::findOrFail($ideaId)->communities;
You can read more about how to use many-to-many relationships here: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
someone told me that we dont need to make a pivot table if we only want to have ids of the table
The above is not true (unless I've just misunderstood).
For a many-to-many (belongsToMany) their must be the two related table and then an intermediate (pivot) table. The intermediate table will contain the primary key for table 1 and the primary key for table 2.
In laravel, the convention for naming tables is plural for your main tables i.e. Community = 'communities' and Idea = 'ideas'. The pivot table name will be derived from the alphabetical order of the related model names i.e.
community_idea.
Now, if you don't want/can't to follow these conventions that's absolutely fine. For more information you can refer to the documentation: https://laravel.com/docs/5.2/eloquent-relationships#many-to-many
Once you're happy that you have the necessary tables with the necessary fields you can access the relationship by:
$ideas = $community->ideas()->get();
//or
$ideas = $community->ideas;
So you controller would look something like:
public function showCommunities($id)
{
$community = Community::findOrFail($id);
//The below isn't necessary as you're passing the Model to a view
// but it's good for self documentation
$community->load('ideas');
return view('publicPages.ideas_in_community', compact('community'));
}
Alternatively, you could add the ideas to the array of data passed to the view to be a bit more verbose:
public function showCommunities($id)
{
$community = Community::findOrFail($id);
$ideas = $community->ideas
return view('publicPages.ideas_in_community', compact('community', 'ideas));
}
Hope this helps!
UPDATE
I would imagine the reason that you're receiving the App\idea not found is because the model names don't match. It's good practice (and in certain environments essential) to Capitalise you class names so make sure of the following:
Your class name is Idea and it's file is called Idea.php
The class has it's namespace declared i.e. namespace App;
If you've added a new class and it's not being found you might need to run composer dump-autoload from the command line to update the autoloader.
I have a CakePHP project having 3 plugins: plugin1, plugin2, plugin3. These are simple plugins, I've just tried to split up my project into 3 smaller & easier parts.
Plugin1 has to use a model "Model1", where there is no db table for this table. And Cake is showing error :
"Missing Database Table
Error: Table models1 for model Model1 was not found in datasource default."
Here, table-name and Model-name are in correct convention. I don't want to create a table for this, since I don't need it. What to do now ?
You can set that model is without table by setting $useTable in the model (from CakeBook)
class Example extends AppModel {
public $useTable = false; // This model does not use a database table
}
I am trying to remove a table from CakePHP. All the tables were created with the cake bake function and I have removed the table from all the models. But when I remove the table from the database I get an error message:
Error: Database table channels_offers for model ChannelsOffer was not found.
Notice: If you want to customize this error message, create app/views/errors/missing_table.ctp
So how do I remove a table that was originally baked in?
Well, it appears that you still have a model called ChannelsOffer. You would need to add a property to your ChannelsOffer model. Here's an example
class ChannelsOffer extends AppModel {
// this tells the model not to use a table, alternatively you could supply your
// own table name here.
public $useTable = false;