Get all of the current attributes on the model.
public getAttributes ( ) : array | ||
return | array |
use Illuminate\Database\Eloquent\Model; class User extends Model { protected $fillable = ['username', 'email', 'password']; } $user = User::create(['username' => 'john_doe', 'email' => 'john.doe@example.com', 'password' => 'secret']); $attributes = $user->getAttributes(); // Output: ['id' => 1, 'username' => 'john_doe', 'email' => 'john.doe@example.com', 'password' => 'secret']
use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $fillable = ['title', 'content', 'published']; } $post = Post::where('published', true)->first(); $attributes = $post->getAttributes(); // Output: ['id' => 1, 'title' => 'Lorem ipsum', 'content' => '...', 'published' => true]In this example, we retrieve the first published post from the database using the `where` method and `first` method. We then retrieve the attributes using the `getAttributes` method and store them in a variable.