Vinelab / NeoEloquent

The Neo4j OGM for Laravel
MIT License
634 stars 200 forks source link

Hidden and fillable field not created with Model::createWith #144

Closed WorldSeso7 closed 7 years ago

WorldSeso7 commented 8 years ago

Hello! As the title, when you have a model with a field that is hidden and fillable (i.e. password) if you create a new model with a related one with Model::createWith() this field isn't added to the record. Ex.

<?php
namespace App;
class A extends Vinelab\NeoEloquent\Eloquent\Model {
   protected $hidden = ['test'];
   protected $fillable = ['a', 'b', 'test'];
   public function relation(){
        return $this->hasMany('App\B','TEST_RELATION');
    }
}

class B extends Vinelab\NeoEloquent\Eloquent\Model {
   protected $fillable = ['c', 'd'];
   public function related(){
        return $this->belongsTo('App\A','TEST_RELATION');
    }
}

A::createWith(
  [
     'a' => 1,
     'b' => 2,
     'test' => 'test'
  ],
  [
     [
         'c' => 3,
         'd'=> 4,
      ]
  ]
);

?>

I found the solution by adding $instance->setHidden([]); on line 838 of src/Vinelab/NeoEloquent/Eloquent/Builder.php

KinaneD commented 8 years ago

Sorry for the late reply, i believe you should do the following: 1- Use and Implement the following interfaces:

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class A extends Vinelab\NeoEloquent\Eloquent\Model implements AuthenticatableContract, CanResetPasswordContract
{}

2 - Use the following traits:

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;

class A extends Vinelab\NeoEloquent\Eloquent\Model implements AuthenticatableContract, CanResetPasswordContract
{
      use Authenticatable, CanResetPassword;
}

3- And set a protected hidden properties array:

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];

With this setup done properly you should be good to go.

jr2wolfgang commented 7 years ago

To those people out there whom just like me, experienced a terrible Auth::attempt() issue. The solution of @KinaneD solved my problem.

I'm using Laravel 5.3 and by doing only:

class User extends NeoEloquent 

is totally incomplete. I think the documentation should be updated.

jr2wolfgang commented 7 years ago

To others who has issues with:

Illuminate\Foundation\Auth\User

(this is probably an issue with Laravel 5.3)

The User model needs to be modified to this:

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;

class User extends NeoEloquent implements 
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use SoftDeletes, Authenticatable, CanResetPassword, Authorizable;