lepikhinb / laravel-fluent Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Hello ,
I'm facing the following exception
Error : Call to a member function getName() on null
/var/www/codebase/itrainer_dashboard/vendor/based/laravel-fluent/src/HasProperties.php:56
When i override Eloquent model public properties that doesn't have types like [ $timestamps ]
Simple Solution is to exclude public properties without types with the following
->reject(fn (ReflectionProperty $property) => $property->getType() === null)
Maybe this package should be renamed to eloquent-fluent if it's specifically about eloquent models?
Root composer.json requires php 8.0 but your php version (8.0.11) does not satisfy that requirement.
I really like the possibilities for this library. What do you think about the idea of letting people add laravel validation constraints to fields as well. So when you setAttribute() something, it'll validate and throw exceptions when invalid data is set into the model.
Since it's optional and built when the class is constructed, it should be fairly transparent. Something like this might be nice
#[Validate('int|between:3,100')]
public int $someNumber;
Then if I set a valid of 200, it'll throw a validation exception that if I want to catch myself and handle, I can do so. Or perhaps add a default or fallback values for situations where it might be appropriate.
Now that Laravel implements a Illuminate\Support\Fluent
class there is a risk of confusion when using the trait:
class User extends Model {
use Fluent; // Here comes the confusion, are we using this package or Laravel's class?
}
A simple workaround is to alias the class, eg:
use Based\Fluent\Fluent as HasFluentAttributes;
class User extends Model {
use HasFluentAttributes;
}
One could say there's a great risk to forget doing this when generating a model with artisan make:model
.
Publishing then editing the stubs could be a workaround, but this would create a lot of friction for simpler projects.
I'd suggest renaming the trait so users don't risk any error.
Let me know if you're OK with that and I'll drop a PR.
Hi there. First of all, thank you for your work on this package, I loved using it so far.
Unfortunately, I'm running into an issue when using default attributes.
class User
{
use Fluent;
public int $amount = 20;
}
Or as documented in the Laravel Docs.
class User
{
use Fluent;
public int $amount;
public $attributes = [
'amount' => 20,
];
}
Using either of these snippets, whenever retrieving a User from the database the amount
property will always have the default value. Even when the amount
column is set to anything else than the default value.
$user = User::first();
echo $user->amount;
// 20
I haven't been able to find why this is happening exactly, but I still wanted to report this issue. Maybe we can find a fix, or we should add this to the documentation as an incompatibility.
Fantastic project!
Any chance we can get support for Custom Casts?
https://laravel.com/docs/8.x/eloquent-mutators#custom-casts
If no one with experience with the project has time, I can try implementing the feature, but in that case some help (i.e. direction) would be needed.
Hi, i was testing some models with uuid, so i need to add de $incrementing
property to false like the docs, that throws an error, i guess is for the public access of the property
class User extends Model
{
use Fluent;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
}
when i try to retrieve the model from database this throw
PHP Error: Call to a member function getName() on null
by the way, i love this package
This package applies specifically to eloquent. Eloquent can be used without laravel.
Hi there!
Opening this issue to track compatibility with Laravel 9. Might open a PR in the near future myself, though!
I was thinking it might be possible to mark properties as fillable or guarded for Laravel's mass-assignment protection.
class Person extends Model
{
use Fluent;
#[Fillable]
public string $first_name;
#[Fillable]
public string $last_name;
// not fillable since it does not have the Fillable attribute
public bool $is_admin;
}
If you are not interested in adding this feature, no big deal.
This is an idea of how to improve on Laravel's accessors so that you would get IDE autocompletion and other benefits of having it defined as a real property.
class Person extends Model
{
use Fluent;
public string $first_name;
public string $last_name;
#[Accessor('getFullNameAttribute')]
public readonly string $full_name;
#[Accessor(function() {
return $this->first_name . ' ' . $this->last_name;
})]
public readonly string $full_name2;
}
The readonly
keyword will not work until PHP 8.1 and also I do not think it is possible to pass a closure to an attribute until PHP 8.1. The first accessor lets you reference a method name and the second example defines the logic in the closure.
Feel free to close this issue if you are not interested in adding this feature.
Hi, I have a very simple example like this:
class User extends Model
{
use Fluent;
public string $name;
}
Within tests:
$user = UserFactory::new()->create();
dd($user->name);
Throws an error:
Error : Typed property App\Models\User::$name must not be accessed before initialization
Hi! Firstly thanks for this amazing library, I love that I can now write properties into the class and it'll help autocomplete on eloquent models!
I'm having a doubt whether I'm using BelongsTo relation correctly, I've also noticed in your tests that you are sometimes using the BelongsTo attribute and then sometimes using a typical Laravel `function products(): BelongsTo" style function, is the BelongsTo attribute not working as expected sometimes?
This is what I've got in my class
class InvoicePosition {
#[BelongsTo('invoice_id', 'id')]
public Invoice $invoice;
}
The InvoicePosition table has a foreign key "invoice_id" to the Invoice table with the primary key "id", but when I try to set the Invoice and then call save(), I get this error back
I do this in my code
$invoice = new Invoice([...attrs];
$invoice->save();
$position = new InvoicePosition([...attrs]);
$position->invoice = $invoice;
$position->save();
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
and then in the returned query I get back with the error, the invoice_id field is not being set on the query, so that's pretty clear why it's not working, but I'm not sure what information that I'm missing to get it working.
I've seen your tests use the default eloquent style of handling belongsTo as well, so I was wondering about whether this is still a WIP or it does actually update foreign relations for you correctly.
Thanks!
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.