I have a Lumen app.
I just created a new TillSoftware model with this migration file
Schema::create('till_softwares', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name", 120);
$table->timestamps();
});
But when I just run a simple request like this
$softwareList = TillSoftware::all();
I receive an error telling
Next Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.till_software' doesn't exist
It doesn't seem to add the usual extra s at the end of the table name and trying to make a request to till_software instead of till_softwares.
I have more than 20 other models which aren't running just fine, and as far as I know plural of software is softwares.
Am I missing something obvious here?
I still can add an extra
protected $table = 'till_softwares';
on my model but I would prefer to understand what I did incorrectly.
Thank you for your help.
So laravel will use Str::plural() to find the plural of the model name to find the table name as this is standard.
>>> Str::plural('user')
=> "users"
So this is User model into users table
>>> Str::plural('software')
=> "software"
but the plural of software is software, so that is the table name.
If you want to use softwares you will need to do as you said
protected $table = 'till_softwares';
But I would say that using the standard table names would be best, so it should be till_software
Related
Is there a library where I am able to use a pivot table that points to the same model twice (i.e User) where I can have the PK as either column 2 or 3 for something like a friending or associating system?
Code Example
migration
Schema::create('friends', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('requestor_id');
$table->unsignedBigInteger('requested_id');
$table->boolean('accepted')->default(0);
});
model
public function friends(){
return $this->[LibraryMethod](Friend::class, 'requestor_id', 'requested_id')->where('accepted', 1);
}
So then I could then either look at 'requestor_id' or 'requested_id' for my PK and get the PK of an associated User.
Ideally this should also work with eloquent functions.
I recognise that friends isn't the best example as you could have a handshake-like system, but for simple associations (i.e, between products) something like this would be pretty useful.
I'm a newbie here and also in Laravel, so please excuse me. I have a table named 'products' and this table related to the 'recipes' table via many-to-one relation(One of the recipes has a lot of products). -'recipes' table keeps reference code- Here's where I stuck; the 'recipes' table has one-to-one relations to three different tables that keeping the "real" product recipes. Those tables have different recipe contents like,
Alkaline table;
Schema::create('alkalines', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('recipe_id');
$table->integer('sodium_bicarbonate');
$table->timestamps();
});
Acets table;
Schema::create('acets', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('recipe_id');
$table->integer('sodium_chloride');
$table->integer('acetic_acid');
$table->timestamps();
});
I'm able to fetch all relations if I start with one of these(e.g with Acet model). But if, I list all of products and try to fetch it's recipe, I have to use a bunch of 'if and else's. Just can't get the recipe like;
$product->recipe-> "one of the three recipe tables' content"
And my 'recipes' table:
Schema::create('recipes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ref');
$table->timestamps();
});
I believe it's easy, just missing something. Please help! Thanks in advance!
I think
You can get every relation individual them merge the arrays
like
$arr1=Alkalines::with('recipe')->get()->toArray();
$arr2==Acets::with('recipe')->get()->toArray();
$arr3=***************************;
array_merge($arr1,$arr2,$arr3)
Welcome to SO.
If you have relations set up properly, you can use 'collection->pluck()' to retrieve their results, no matter how deeply nested in different relations.
Example:
$game->players->stats won't work, because players is a collection that doesn't have a stats attribute, method or field.
So, what you can do is use pluck() and collapse() to retrieve result of relations:
$game->players->pluck('stats')->collapse()
I edit the code from #mohamedhassan a little bit, and it worked!
public function solutionMerge()
{
$arr1=Alkaline::with('recipe')->get();
$arr2=Acet::with('recipe')->get();
$arr3=Glucose::with('recipe')->get();
$solutionMerge = collect([$arr1,$arr2,$arr3]);
return $solutionMerge;
}
Just assigned arrays into a collection. And then use collapse().
And now, I'm able to fetch data like $solutionMerge->recipe->id
Thank you people, for your precious time and immense knowledge!
I have two tables(models) called Users and Books and need to create custom IDs (primary keys) values for my tables say, u25894r for users and b419k for books. I searched in the internet and stackoverflow a lot but I didn't understand WHERE I should define my custom ID generator function e.g., AppServiceProvider boot function or in Model constructor function. I'd be so appreciate it if you show the way with a little example.
EDIT:
I need to write functions that generate unique custom IDs for the tables.
You can set the custom id name in the Model class. Use
protected $primaryKey = "u25894r";
for user table and then make a migration.
You need to make a change in both the class/model definition (ie protected $primaryKey = "myUniqueName") and also in the migrations file:
public function up()
{
Schema::create('recruiters', function (Blueprint $table) {
// this is in the migration file:
$table->id('recruiter_id');
$table->timestamps();
});
}
you can set that in migration file
laravel documents is powerful for that you should read about migration from this link
https://laravel.com/docs/5.6/migrations
I have 3 entities:
Institution
Branch
Address
Institution & Branch will have many Addresses, so in order to not create two child models for storing the address of each entity, I want to create a pivot table which will have these columns as primary key: entity_id, address_id, entity_name. With this approach I will store the relationship of all addresses for Institution and Branch.
This is my code in entities_addresses pivot table
Schema::create('entities_addresses', function (Blueprint $table) {
$table->integer('entity_id')->unsigned();
$table->integer('address_id')->unsigned();
$table->string('model_name', 50);
$table->primary(['entity_id','address_id','model_name']);
Next, in Address Model I have the following code:
public function institutions()
{
return $this->belongsToMany(Institution::class, 'entities_addresses', 'entity_id', 'address_id');
}
Next, in Institution model I have the following code
public function buildings()
{
return $this->hasMany(Building::class);
}
I believe I need to add somewhere else which is the "custom" relationship, beacuse now I'm recieving this error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'addresses.institution_id'
So, my questions are:
Where do I have to specify the custom relationship?
It's a better approach to create an intermidate model?
I will have to customize the use to be attach and detach methods? In which place I willhave to do it?
Regards
By default, Laravel is assuming that the database table is the plural form of the model name. But what if my table name is "news" and i still want to use this feature? should i change it to "newses" or should i use "new" for the model name?
You may specify a custom table by defining a table property on your model as below
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'my_flights';
}
Ref:https://laravel.com/docs/5.1/eloquent
If you have a model ending with the letter 's', it will keep the table name the same. In your case, your model will name your table news by default.
If you want to use another name though, you can use:
protected $table = 'tablename';
inside of your model.
EDIT: I tested this in my application. I made a model named News. Then I made a new instance of News and retrieved the table name:
$news = new News();
dd($news->getTable());
It returns: news
Inside your eloquent model you have to define table name. For example if my model is named user and table in database is named user_of_application then i do it this way
class user extends Model
{
protected $table = 'user_of_application';
}
Laravel uses a "standard set" rule that defines:
A Model is a single instance of a record
A Collection is one or more records
Therefore it assumes that a table is a collection of records.
The nomenclature has a problem when it clashes with various features / gotchas of human languages. For example, what if I have a Model called Sheep? That does mean my Database Table should be called "Sheeps"?
It's up to the developer to avoid "Gollum/Smeagol" syntax. Indeed, you wouldn't want a table called "Newses" as much I'd like to end up with a table called "Sheeps".
Ultimately, I construct Migrations with:
sudo php artisan make:migration create_sheep_table --create=sheep
As for Models, you'll notice in the documentation that they have a different table name for "Flights" called "my_flights"
https://laravel.com/docs/master/eloquent#defining-models
Again, it's up to the developer / DB manager to make decisions on naming conventions that make sense in an application context.