Giter Site home page Giter Site logo

laravel-fluent's People

Contributors

attia-ahmed avatar danharrin avatar hsmfawaz avatar laravel-shift avatar lepikhinb avatar zembrowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-fluent's Issues

Call to a member function getName() on null

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)

Composer requires php 8.0

Root composer.json requires php 8.0 but your php version (8.0.11) does not satisfy that requirement.

More PHP 8 attributes for adding validation constraints to fields

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.

Confusion with Laravel Fluent class

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.

Default properties are never overwritten by retrieval

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.

Incrementing property of laravel throw an error

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

Laravel 9 compatibility

Hi there!

Opening this issue to track compatibility with Laravel 9. Might open a PR in the near future myself, though!

Fillable attributes for mass-assignment protection

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.

Support for accessors

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.

Using BelongsTo relation correctly?

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.