kirkbushell / eloquence Goto Github PK
View Code? Open in Web Editor NEWA drop-in library for certain database functionality in Laravel, that allows for extra features that may never make it into the main project.
License: MIT License
A drop-in library for certain database functionality in Laravel, that allows for extra features that may never make it into the main project.
License: MIT License
If I have a pivot table, upon attaching the database throws an error saying that the ID does not have a default value. I assume that laravel isn't using the your Model when inserting the new record.
Any thoughts?
Hi,
I put the trait in my User model and get error:
Fatal error: Method 'getAttribute' declared in multiple traits in
I also use these:
use Authenticatable, CanResetPassword, Metable, Rememberable, CamelCaseModel;
What to do?
Thanks.
What is the correct formatting for pivot tables? Usually, with normal integer ID's it would generate:
Schema::create('domain_user', function (Blueprint $table) {
$table->integer('domain_id')->unsigned()->index();
$table->foreign('domain_id')->references('id')->on('domains')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['domain_id', 'user_id']);
});
I'm thinking below should be enough (simply changing to string 36 and removing unsigned):
Schema::create('domain_user', function (Blueprint $table) {
$table->string('domain_id',36)->index();
$table->foreign('domain_id')->references('id')->on('domains')->onDelete('cascade');
$table->string('user_id',36)()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['domain_id', 'user_id']);
});
But that generates the following error:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key const
raint (SQL: alter table `domain_user` add constraint domain_user_
domain_id_foreign foreign key (`domain_id`) references `domains`
(`id`) on delete cascade)
Hi. I check Sluggable trait but not fire sluggable methods on update.
Can you update it to recreate slug if slugStrategy
check different value?
Thanks
When testing models with phpunit. I get an Field 'id' doesn't have a default value
. For some reason all second tests fail when creating the id
. It looks like the static::creating
callback is never getting called the second time. This is only prevalent in unit testing and not in regular laravel. Thoughts?
use Eloquence\Behaviours\CamelCasing; class SomeModel extends Model { use CamelCasing; }
Should be using:
composer require kirkbushell/eloquence:~1.2
instead of
composer require kirkbushell/eloquence ~1.2
Otherwise, ~
is being expanded to the home directory. The use of the colon prevents this.
I've come across this package and noticed that only basic relation types are currently supported. I'm currently working on a pull request to add support for polymorphic relationships. I should have the PR ready in the next couple of days but I wanted to see what the initial reaction would be to adding such a feature.
When I enable CamelCasing on my Model it breaks the hasMany relationship.
<?php
namespace App\BuildCtrl;
use Illuminate\Database\Eloquent\Model;
use \Eloquence\Behaviours\CamelCasing;
class GlobalDetails extends Model
{
use CamelCasing;
protected $connection = 'competentpersons';
protected $table = 'vwGlobalDetails';
protected $primarykey = 'RecordSetKey';
public $timestamps = false;
public function workDescription() {
return $this->hasMany(WorkDescription::class, 'RecordSetKey', 'RecordSetKey');
}
}
Using:
return GlobalDetails::where('recordSetKey', '=', $recordSetKey)
->with('workDescription')
->get();
returns:
"workDescription":[]
If I disable CamelCasing:
"work_description":[{"RecordSetKey":"287","ID":"361","DescriptionOfWorkItem":"Dwelling house"},{"RecordSetKey":"287","ID":"362","DescriptionOfWorkItem":"New consumer unit"},{"RecordSetKey":"287","ID":"363","DescriptionOfWorkItem":"Kitchen"},{"RecordSetKey":"287","ID":"364","DescriptionOfWorkItem":"Cooker"},{"RecordSetKey":"287","ID":"365","DescriptionOfWorkItem":"New installation, rewire or partial rewire"},{"RecordSetKey":"287","ID":"366","DescriptionOfWorkItem":"One or more new circuits"}]
Trouble is I'm using this with vue.js so CamelCasing is really helpful.
Laravel 7 has been released on March 3rd, 2020, it would be lovely to be able to use eloquence with the new version ๐
Just wondering if you are planning a new release soon. :)
A recent security release for Laravel has put in an extra constraint that you can only mass assign to existing columns in the table, which has broken some of my models where I've used camelCasing, since the column check happens before the attribute is set (and written to the database).
It is possible to overload the Eloquent isGuardableColumn
method so that it looks for the snake_cased field name instead. I propose adding the following to the CamelCasing behaviour:
/**
* Overloads the eloquent isGuardableColumn method to ensure that we are checking for the existence of
* the snake_cased column name.
*
* @param string $key
* @return bool
*/
protected function isGuardableColumn($key)
{
return parent::isGuardableColumn($this->getSnakeKey($key));
}
I'm happy to do a PR for this - I've already confirmed it doesn't break any existing tests.
The $attempts variable in generateTitleSlug() is not appended properly to the $titleSlug.
I avoid duplicate slugs by having a loop in my application code:
// Loop until a unique slug could be generated
$saved = false;
do {
try {
$saved = $user->save();
}
catch (\Illuminate\Database\QueryException $e);
}
while (!$saved);
This loop never finishes.
Does this package support the validator?
Receiving camelCase.
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for kirkbushell/eloquence 1.1.x -> satisfiable by kirkbushell/eloquence[1.1.0].
- kirkbushell/eloquence 1.1.0 requires rhumsaa/uuid 3.0.*@dev -> no matching package found.
Hi!
I've been attempting to use this trait in a Laravel 5.1 application, but have hit a problem with the $hidden
property.
If I define a property to be hidden in camel case, it is ignored:
...
protected $hidden = [
'lastLogin', // Ignored (model renders out lastLogin field)
'is_active', // Hidden as expected
];
...
This appears to be the case for the $dates
and $visible
properties as well (and there could be others I guess).
Digging in to the core Eloquent model, it seems that getHidden
could be overridden to correct this behaviour, but the withHidden
method would not work properly either because it uses the $hidden
array directly instead of through the accessor.
Are there any plans to support these properties in a consistent way?
This one came as a bit of a surprise and it has some security implications if the developer expects their fields to be hidden and they are not.
Thanks,
Chris
for example:
{firstName: "Example", email: "[email protected]", password: "asdasd"}
On saving to database , the firstName value is not saved. I see you have setAttribute function but seems to not working convert it to snake case.
Just updated to the latest version and added the service provider and can't seem to avoid getting this error bombing the site:
ReflectionException
Class schema does not exist
Countable is easy to use , but It doesn't work in my case .
post has two state : active or inactive
user can has three relation to post : all post , active posts and inactive posts,
and I want to cache all three count
thanks
Another issue here...
Added use UUIDModel; and public $incrementing = false; to my User model.
I build the schema in migration with $table->char('id', $length = 36)->index(); instead of increments('id')
(Btw I think you should use binary(16) for performance http://iops.io/blog/storing-billions-uuid-fields-mysql-innodb/)
This seem to work so far. Now I want to seed my database and I get:
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint fai
led: users.id
Seed looks like this:
User::insert(
[
'email' => '[email protected]',
'app_id' => 1,
'department_id' => 0,
'firstname' => 'Super',
'lastname' => 'Admin',
'username' => 'admin',
'active' => 1,
'blocked' => 0,
'password' => Hash::make('admin')
]
);
Thanks.
I have a Category model that has many Services. My categories table has columns for category_id (the primary key) and services_count, among other fields. The services table has service_id (primary key) and category_id, among others.
My Service model looks something like:
class Service extends Model implements CountCache
{
use CamelCaseModel;
protected $primaryKey = 'service_id';
...
public function countCaches()
{
return [
'services_count' => [Category::class, 'category_id', 'category_id'],
];
}
}
Finally, in App/Providers/AppServiceProvider.php, I have in my register()
method:
class AppServiceProvider extends ServiceProvider
{
...
public function register()
{
Service::observe(new CountCacheObserver());
}
}
When I create a new Service model, or edit an existing on, the services_count field on the corresponding Category model isn't increasing. Where did I goof?
I've noticed the documentation mentions to add \Eloquence\EloquenceServiceProvider
to config/app.php
. Most packages use Laravel's automatic package discovery, so it surprised me when this did not show up in the discovered packages list.
Is there a reason why this doesn't use that feature? It's a small amount of work to add so I'm happy to do it if need be.
Hi,
when i follow your readme.md, il loaded with composer, i included the service provider in app.php but when i create a new model object it doesn't execute the uuid generation. By the way the closure is loaded in the core laravel Model object but it doesn't affect this class...
Even when i try to call
$user= new User();
$user->generateNewUuid();
it throw undefined method in user (and it try to search the method in query builder)
any idea?
Thanks in advance
Cedric LE ROY
Laravel 8 was released this week - and various support packages were updated in line with this, including illuminate/database
which is a dependency for eloquence.
Current version of illuminate/database
(and the Laravel framework) is v8.1.0:
https://packagist.org/packages/illuminate/database#v8.1.0
https://packagist.org/packages/laravel/framework#v8.1.0
Hey Kirk,
I've been trying to use a pivot model to write back some JSON from an API. My biggest issue is I can't get the pivot model itself to respect Eloquence's enforceCamel rules. Do you know if that is possible? You can see in this response, the properties inside the pivot table are still snake cased.
Hello,
Can this package generate unique slugs ?
Thx.
In Laravel 5, if I have a Many to Many relationship, such as:
public function roles()
{
return $this->belongsToMany(Role::class);
}
and have the tables all set up correct, when I do this in a controller:
return User::with('roles')->all();
it will always return an empty array for roles if the User
model has use CamelCaseModel
in it. If I remove that line, it grabs the relationship as expected.
I am still investigating, but I suspect it has something to do with Laravel doing something specific with attribute starting with pivot_ in these cases, but it's not there cuz we camel cased it, so it can't connect the relationship...?
if we are using confirmed
validator, it will not pass the validation.
are you sure Sluggable::getRouteKey
returns right value? its name suggests it should return field name, but you are returning $this->slug
i.e. a value...
As seen here setAttribute
in Laravel implementation returns a model.
As seen here setAttribute
in CamelCasing trait does not return anything.
So, if you happen to use CamelCasing and, for example, end up there --> crash. setAttribute
of CamelCasing
jumps in between and does not pass model to the caller.
Suppose easy fix is making setAttribute
from CamelCasing
return whatever call to parent returns, see the PR
I can't think of a use case off the top of my head, except for what I just did when I added cache counting to an existing set of models and relations.
The behaviour only calculates if the count should be incremented or decremented from the current value, but there is no way to tell it to do a "hard" recalculation of the value (i.e. get $model->relation()->count()
and store it in $model->count_field
).
I'm not sure how that could be handled (maybe an artisan command?), or if there should be an option in the countCaches()
configuration to tell it to do full recalculations on updates instead of simple increments/decrements.
Thoughts?
hashids/hashids 1.0.6 requires php ^5.3.3 || ^7.0
version hashids/hashids:4.1 support php 8
I've just noticed an interesting discrepancy in getAttribute()
between how the CamelCasing
trait works and the original Eloquent Model.
Consider the Eloquent Model function:
public function getAttribute($key)
{
if (array_key_exists($key, $this->attributes) || $this->hasGetMutator($key)) {
return $this->getAttributeValue($key);
}
return $this->getRelationValue($key);
}
Now look at CamelCasing:
public function getAttribute($key)
{
if (method_exists($this, $key)) {
return $this->getRelationValue($key);
}
return parent::getAttribute($this->getSnakeKey($key));
}
Eloquent allows attributes (even read-only ones) precedence over relations. CamelCasing favours relationships first.
It seems to me that following Eloquence's order of preference would be a good idea, so the behaviour is consistent between both.
I have User, Category and Service models. A Service belongs to a category, and Users have many Services. The primary keys on the models are user_id
, category_id
and service_id
respectively. The join keys are the same (i.e. service.user_id
and service.category_id
).
I want to create cache count for both the number of services per category, and the number of services per user. I can't do this in my Service class:
public function countCaches()
{
return [
'services_count' => [Category::class, 'category_id', 'category_id'],
'services_count' => [User::class, 'user_id', 'user_id'],
];
}
Because the column names are the same (category.services_count
and user.services_count
), I can't key my array using the same column name.
Am I missing something?
Your composer requires 4.1.*
composer.json
and CamelCasing.php
have tab-indented lines. they jump out when viewing through github's interface. PSR2 recommends always using spaces for indentation.
Your composer requires 4.1.*
i'm on laravel 6.0, trying to upgrade to 6.6. composer tells me eloquence is preventing me from upgrading. i believe the issue is due to the pinning of illuminate/database
to 5.5. there are new releases available.
my laravel version:
$ artisan --version
Laravel Framework 6.0-dev
my composer.json:
"require": {
"php": "^7.2",
"bacon/bacon-qr-code": "^2.0",
"doctrine/dbal": "^2.9",
"fideloper/proxy": "^4.0",
"kirkbushell/eloquence": "~2.0",
"laravel/framework": "^6.0",
"laravel/passport": "^7.3",
"laravel/tinker": "^1.0",
"pragmarx/google2fa-laravel": "^1.0",
"spatie/laravel-sluggable": "^2.1"
},
"require-dev": {
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.8",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
},
error with composer update
:
$ composer update laravel/framework
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Conclusion: remove kirkbushell/eloquence 2.0.8
full error log:
Problem 1
- Conclusion: remove kirkbushell/eloquence 2.0.8
- Conclusion: don't install kirkbushell/eloquence 2.0.8
- Installation request for kirkbushell/eloquence (locked at 2.0.8, required as ~2.0) -> satisfiable by kirkbushell/eloquence[2.0.8].
- Conclusion: don't install laravel/framework v6.6.0
- kirkbushell/eloquence 2.0.8 requires illuminate/database ~5.5 -> satisfiable by laravel/framework[5.5.x-dev, 5.6.x-dev, 5.7.x-dev, 5.8.x-dev], illuminate/database[5.5.x-dev, 5.6.x-dev, 5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, 5.8.x-dev, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v5.5.36, v5.5.37, v5.5.39, v5.5.40, v5.5.41, v5.5.43, v5.5.44, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.26, v5.7.27, v5.7.28, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9, v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.17, v5.8.18, v5.8.19, v5.8.2, v5.8.20, v5.8.22, v5.8.24, v5.8.27, v5.8.28, v5.8.29, v5.8.3, v5.8.30, v5.8.31, v5.8.32, v5.8.33, v5.8.34, v5.8.35, v5.8.4, v5.8.8, v5.8.9].
- Can only install one of: laravel/framework[6.x-dev, 5.5.x-dev].
- Can only install one of: laravel/framework[6.x-dev, 5.6.x-dev].
- Can only install one of: laravel/framework[6.x-dev, 5.7.x-dev].
- Can only install one of: laravel/framework[6.x-dev, 5.8.x-dev].
- don't install illuminate/database 5.7.17|don't install laravel/framework 6.x-dev
- don't install illuminate/database 5.7.18|don't install laravel/framework 6.x-dev
- don't install illuminate/database 5.7.19|don't install laravel/framework 6.x-dev
- don't install illuminate/database 5.7.x-dev|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.0|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.1|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.10|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.11|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.15|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.2|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.20|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.21|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.22|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.23|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.26|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.27|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.28|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.3|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.4|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.5|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.6|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.7|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.8|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.7.9|don't install laravel/framework 6.x-dev
- don't install illuminate/database 5.8.x-dev|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.0|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.11|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.12|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.14|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.15|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.17|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.18|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.19|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.2|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.20|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.22|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.24|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.27|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.28|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.29|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.3|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.30|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.31|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.32|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.33|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.34|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.35|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.4|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.8|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.8.9|don't install laravel/framework 6.x-dev
- don't install illuminate/database 5.5.x-dev|don't install laravel/framework 6.x-dev
- don't install illuminate/database 5.6.x-dev|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.0|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.16|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.17|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.2|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.28|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.33|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.34|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.35|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.36|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.37|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.39|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.40|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.41|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.43|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.5.44|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.0|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.1|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.10|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.11|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.12|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.13|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.14|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.15|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.16|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.17|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.19|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.2|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.20|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.21|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.22|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.23|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.24|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.25|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.26|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.27|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.28|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.29|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.3|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.30|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.31|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.32|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.33|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.34|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.35|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.36|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.37|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.38|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.39|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.4|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.5|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.6|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.7|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.8|don't install laravel/framework 6.x-dev
- don't install illuminate/database v5.6.9|don't install laravel/framework 6.x-dev
- Installation request for laravel/framework ^6.6 -> satisfiable by laravel/framework[6.x-dev, v6.6.0].
Is MySQL/MariaDB the only supported database? Because I tried to use the countCache with postgresql and the syntax is not compatible since it doesn't like the tilda (`)
When grabbing a relationship on a model (not a database column/attribute), it is set in snake_case rather than camelCase. Would be nice to have this added to Eloquence.
Example:
class User extends Eloquent {
public function myItems() {
return $this->hasMany('MyItem');
}
}
Accessing the Model/Relationship (2 different approaches):
$user = User::find(1);
$user->myItems; // Shorthand for $user->myItems()->get();
return $user;
OR:
$user = User::with('myItems')->find(1);
return $user;
Output:
{
id: 1,
fullName: 'John Smith',
my_items: [...]
}
The attributes from the database come out as expected in camelCase. Unfortunately the relationship accessor does not. I'll look into possible solutions and let you know if I find anything.
Any chance of a new minor release with the latest merges?
I'm not sure if I'm missing a step somewhere but my eager loaded belongsTo relationship breaks when I use this package. This code works without the CamelCasing trait but when I add it get an error: ErrorException
Undefined property: App\Models\User::$status. My versions of PHP, Laravel and Eloquence are all > 8.
namespace App\Models;
use Eloquence\Behaviours\CamelCasing;
class User extends Authenticatable
{
use CamelCasing;
protected $fillable = [
'status',
];
protected $with = ['status'];
public function status()
{
return $this->belongsTo(UserStatus::class, 'status', 'status');
}
}```
In the same way we can add count caching to a model, it would be nice to have a behaviour that cached the sum of some value.
As an example, a User has many Orders. Right now, we could add a order_count
field to the User model and use the existing behaviour to update that. I'm suggesting something that allows one to add a an order_total
field as well, that is a cached copy of the sum of some field on the Orders table for that User. e.g.
UPDATE users SET order_total = (
SELECT SUM(orders.amount) AS total FROM orders WHERE orders.user_id = users.id
) WHERE users.id = ?
Not sure how the configuration would look, but maybe something like this on the Order class:
public function totalCaches()
{
// short syntax
return [
// class => fieldToSum
User::class => 'amount'
];
// full syntax
return [
// fieldName => [ class, foreignKey, key, fieldToSum ]
'order_total' => [User::class, 'user_id', id', 'amount'],
];
}
I've tried using camel casing its not working properly even I put fillable on my model.
Laravel 5.4 working properly
exception 'ErrorException' with message 'Declaration of Eloquence\Behaviours\CamelCasing::getAttributes() should be compatible with Illuminate\Database\Eloquent\Model::getAttributes($keys = Array)'
Using Laravel 5.3.17
ugly (?) fix :
public function getAttributes() => public function getAttributes($keys = [])
I've created a custom pivot model with the following code, and am getting an error:
Edit :: Sorry this isn't the custom pivot model, this is a normal model, but contains the newPivot method necessary to use a custom pivot model, which is related to this model. I'm adjusting the title too.
ErrorException: Declaration of LandUseType::newPivot() should be compatible with Illuminate\Database\Eloquent\Model::newPivot(Illuminate\Database\Eloquent\Model $parent, array $attributes, $table, $exists)
class LandUseType extends \Eloquent {
protected $table = "land_use_types";
protected $guarded = [
"id"
];
protected $fillable = [
"unit_name",
"ite_trip_gen",
"sf_per_unit",
"sp_per_unit",
"site_acreage_multiplier",
"gallons_per_unit_per_year",
"tons_per_unit_per_year",
"icon",
"short_description",
"long_description"
];
public function sites()
{
return $this->belongsToMany("Site", "site_land_use_types");
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
if($parent instanceof Site){
return new SiteLandUseType($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
Any thoughts on this?
I have added the service provider, adjusted my aliases, and ran a dump-autoload
Something I've used in a few of my projects is additional behaviours for models. In Laravel 3, I write a basic soft-delete mechanism (back then it wasn't supported in Eloquent). This led to a behavioural system whereby developers could add behaviours for certain conditions (such as soft-delete, count caches and more).
I believe the time is now right to re-implement this behaviour for Eloquence (and Laravel 5). My first order of business would be a count cache.
Count caches in effect remove the need for joins on associated tables to count the records. A good example is a user hasMany posts - how many posts have they added?
I am wondering if anyone would be interested in just such behaviour as a part of Eloquence?
Let me know.
Hi Kirk,
Thanks for this awesome package.
I was looking for a way to have a consistent coding convention on my Laravel applications and I started using only the CamelCaseModel trait on a base class for all my models.
I believe that the documentation has to explain that adding the service provider does some things that the owner of the application might not want (for instance: the uuids), apart from changing the binding for the eloquent model. And, most importantly, that the user can just use the trait without registering the service provider.
If you agree, I can add the necessary changes on the readme.md and make a pull request.
I'm pretty sure these methods don't work with camel-casing enabled on the model, since the array of attributes passed to each is simply passed through to the where()
clause of a query.
I think the fix is reasonably simple: overload all these methods in the CamelCasing
trait, snake_case all the array keys, then return the parent method.
Not sure if that's the best way to do it, though. Thoughts?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.