Giter Site home page Giter Site logo

laravel-meta's Introduction

Fluent Meta Data for Eloquent Models

Laravel Source Build Status License

Metable Trait adds the ability to access meta data as if it is a property on your model. Metable is Fluent, just like using an eloquent model attribute you can set or unset metas. Follow along the documentation to find out more.

Changelog

visit CHANGELOG.md

Installation

Composer

Laravel can be installed on laravel 8.x or higher.

Run:

composer require kodeine/laravel-meta

For laravel 7.x or below visit this link.

Upgrade guide

Change this line in composer.json:

"kodeine/laravel-meta": "master"

to:

"kodeine/laravel-meta": "^2.0"

after that, run composer update to upgrade the package.

Upgrade notice

Laravel meta 2 has some backward incompatible changes that listed below:

  1. Laravel 7 or lower not supported.

  2. Removed the following methods: __get, __set, __isset. If you have defined any of these methods, then you probably have something like this in your model:

    class User extends Model{
        use Metable{
            __get as __metaGet
        }

    You need to remove as operator of the methods.

  3. Removed legacy getter. in older version if you had a method called getSomething() then you could access return value of this method using $model->something. this is no longer the case, and you have to call $model->getSomething().

  4. Added new method setAttribute that overrides parent method.

  5. Renamed getMetaDefaultValue method to getDefaultMetaValue.

  6. Second parameter of getMeta method is now default value when meta is null.

  7. Removed whereMeta method in favor of scopeWhereMeta. example: User::whereMeta($key,$value)->get();

  8. Removed getModelKey method.

Migration Table Schema

Each model needs its own meta table.

This is an example migration. you need change parts of it.

In this example we assume you have a model named Post.

Meta table name should be your model's table name + _meta which in this case, model's table name is pluralized form of the model name. so the table name becomes posts_meta.

If you don't want to follow this naming convention and use something else for table name, make sure you add this name to your model's body:

protected $metaTable = 'custom_meta_table';

the foreign key name should be your model's name + _id = post_id

If you used something else for foreign key, make sure you add this to your model's body:

protected $metaKeyName = 'custom_foreign_key';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
    Schema::create('posts_meta', function (Blueprint $table) {
        $table->bigIncrements('id');

        $table->bigInteger('post_id')->unsigned();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');

        $table->string('type')->default('null');

        $table->string('key')->index();
        $table->text('value')->nullable();

        $table->timestamps();
    });
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
    Schema::drop('posts_meta');
}

Configuration

Model Setup

Next, add the Metable trait to each of your metable model definition:

use Kodeine\Metable\Metable;

class Post extends Eloquent
{
    use Metable;
}

Metable Trait will automatically set the meta table based on your model name. Default meta table name would be, models_meta where models is pluralized form of the model name. In case you need to define your own meta table name, you can specify in model:

class Post extends Eloquent
{
    protected $metaTable = 'posts_meta'; //optional.
}

Default Model Attribute values

Additionally, you can set default values by setting an array called $defaultMetaValues on the model. Setting default has two side effects:

  1. If a meta attribute does not exist, the default value will be returned instead of null.
  2. if you attempt to set a meta attribute to the default value, the row in the meta table will be removed, which will cause the default value to be returned, as per rule 1.

This is being the desired and expected functionality for most projects, but be aware that you may need to reimplement default functionality with your own custom accessors and mutators if this functionality does not fit your needs.

This functionality is most suited for meta entries that note exceptions to rules. For example: employees sick out of office (default value: in office), nodes taken down for maintenance (default value: node up), etc. This means the table doesn't need to store data on every entry which is in the expected state, only those rows in the exceptional state, and allows the rows to have a default state upon creation without needing to add code to write it.

   public $defaultMetaValues = [
      'is_user_home_sick' => false,
   ];

Gotcha

When you extend a model and still want to use the same meta table you must override getMetaKeyName function.

class Post extends Eloquent
{

}

class Slideshow extends Post
{
    protected function getMetaKeyName()
    {
        return 'post_id' // The parent foreign key
    }   
}

Working With Meta

Setting Content Meta

To set a meta value on an existing piece of content or create a new data:

Fluent way, You can set meta flawlessly as you do on your regular eloquent models. Metable checks if attribute belongs to model, if not it will access meta model to append or set a new meta.

$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->content = 'some content goes here'; // meta data attribute
$post->save(); // save attributes to respective tables

Or

$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->setMeta('content', 'Some content here');
$post->save();

Or set multiple metas at once:

...
$post->setMeta([
    'content' => 'Some content here',
    'views' => 1,
]);
$post->save();

Or set multiple metas and columns at once:

...
$post->setAttributes([
    'name' => 'hello world'; // model attribute
    'content' => 'Some content here',
    'views' => 1,
]);
$post->save();

Note: If a piece of content already has a meta the existing value will be updated.

You can also save metas with saveMeta without saving the model itself:

$post->content = 'some content goes here'; // meta data attribute
$post->saveMeta(); // will save metas to database but won't save the model itself

Unsetting Content Meta

Similarly, you may unset meta from an existing piece of content:

Fluent way to unset.

$post = Post::find(1);
$post->name // model attribute
unset($post->content) // delete meta on save
$post->save();

Or

$post->unsetMeta('content');
$post->save();

Or unset multiple metas at once:

$post->unsetMeta('content,views');
// or
$post->unsetMeta('content|views');
// or
$post->unsetMeta('content', 'views');
// or array
$post->unsetMeta(['content', 'views']);

$post->save();

Note: The system will not throw an error if the content does not have the requested meta.

Checking for Metas

To see if a piece of content has a meta:

Fluent way, Metable is clever enough to understand $post->content is an attribute of meta.

if (isset($post->content)) {

}
// or
if ($post->hasMeta('content')){

}

You may also check if model has multiple metas:

$post->hasMeta(['content','views']); // returns true only if all the metas exist
// or
$post->hasMeta('content|views');
// or
$post->hasMeta('content,views');

Retrieving Meta

To retrieve a meta value on a piece of content, use the getMeta method:

Fluent way, You can access meta data as if it is a property on your model. Just like you do on your regular eloquent models.

$post = Post::find(1);
dump($post->name);
dump($post->content); // will access meta.

Or

$post = $post->getMeta('content');

Or specify a default value, if not set:

$post = $post->getMeta('content', 'Something');

Note: default values set in defaultMetaValues property take precedence over default value passed to this method.

You may also retrieve more than one meta at a time and get an illuminate collection:

// using comma or pipe
$post = $post->getMeta('content|views');
// or an array
$post = $post->getMeta(['content', 'views']);
// specify default values
$post->getMeta(['content', 'views'],['content'=>'something','views'=>0]);
// or specify one default value for all missing metas
$post->getMeta(['content', 'views'],'none');// result if the metas are missing: ['content'=>'none','views'=>'none']
// without specifying default value result will be null
$post->getMeta(['content', 'views']);// result if the metas are missing: ['content'=>null,'views'=>null]

Disable Fluent Access

If you don't want to access metas in fluent way, you can disable it by adding following property to your model:

protected $disableFluentMeta = true;

By setting that property, this package will no longer handle metas in the following ways:

$post->content='something';// will not set meta. original laravel action will be taken
$post->content;// will not retrieve meta
unset($post->content);// will not unset meta
isset($post->content);// will not check if meta exists

Retrieving All Metas

To fetch all metas associated with a piece of content, use the getMeta without any params

$metas = $post->getMeta();

Retrieving an Array of All Metas

To fetch all metas associated with a piece of content and return them as an array, use the toArray method:

$metas = $post->getMeta()->toArray();

Meta Table Join

When you need to filter your model based on the meta data , you can use meta scope in Eloquent Query Builder.

$post = Post::meta()
    ->where(function($query){
          $query->where('posts_meta.key', '=', 'revision')
                ->where('posts_meta.value', '=', 'draft');
    })

Eager Loading

When you need to retrieve multiple results from your model, you can eager load metas

$post = Post::with(['metas'])->get();

Prevent metas attribute from being populated

When you convert a model to an array (or json) and you don't need all meta fields, you can create a model's property to prevent metas from being added to the resulting array. You can also use it on eloquent relations.

/* Post model */
public $hideMeta = true; // Do not add metas to array

Events

Laravel meta dispatches several events, allowing you to hook into the following events: metaCreating, metaCreated, metaSaving, metaSaved, metaUpdating, metaUpdated, metaDeleting and metaDeleted. Listeners should expect two parameters, first an instance of the model and second, name of the meta that event occurred for it. To enable events you need to add HasMetaEvents trait to your model:

use Kodeine\Metable\Metable;
use Kodeine\Metable\HasMetaEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Metable,HasMetaEvents;
}

After that, you can listen for events the same way you do for models.

If you return false; in listener of any event ending with ing, that operation will be aborted.

Additional events

There are some additional events that extend existing laravel events. These events don't need HasMetaEvents trait and like default laravel events, the event listeners should expect one parameter. Event names: createdWithMetas, updatedWithMetas, savedWithMetas. These events fire exactly like default laravel events except that they are only fired after all metas saved to database. For example, you may need to access metas inside a queue job after model created. But because metas have not been saved to database yet (metas will be saved to database in saved event and this event has not been fired yet), job can't access them. by using createdWithMetas event instead of created event, the problem will be solved.

There are 3 ways to listen for events:

1. By Defining $dispatchesEvents Property

use App\Events\UserMetaSaved;
use Kodeine\Metable\Metable;
use Kodeine\Metable\HasMetaEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Metable,HasMetaEvents;

    protected $dispatchesEvents = [
        'metaSaved' => UserMetaSaved::class,
    ];
}
use Kodeine\Metable\Metable;
use Kodeine\Metable\HasMetaEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Metable,HasMetaEvents;

    protected static function booted()
    {
        static::metaCreated(function ($user, $meta) {
            //
        });
    }
}
class UserObserver
{
    public function metaCreated(User $user,$meta)
    {
        //
    }
}

laravel-meta's People

Contributors

aoumis avatar benyanke avatar beonami avatar bfiessinger avatar christhompsontldr avatar darrencoutts118 avatar dhcmega avatar eduardoarandah avatar goodevilgenius avatar hammat avatar ifnotfr avatar kodeine avatar laravel-shift avatar olsgreen avatar philipnewcomer avatar plivius avatar ray1618 avatar rimthong avatar shemi avatar siamak2 avatar sneherador avatar stephandesouza avatar sumityadav avatar temepest74 avatar todiadiyatmo avatar vortechron 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-meta's Issues

__set() can also set guarded and not fillable attributes

Below code in __set() method apart that make a query for each field when you create a new model, make fillable also guarded and not fillable attributes of the model:

        if (\Schema::connection($this->connection)->hasColumn($this->getTable(), $key)) {
            parent::setAttribute($key, $value);

            return;
        }

I am not yet sure if below solution could be better for avoid this security break, since you can't anymore use something like:

User::create($request->all())

since also not fillable and guarded attributes are set.

This is proposed solution:

        if(in_array($key, parent::getFillable())) {
            parent::setAttribute($key, $value);

            return;
        }

or even better:

        if(in_array($key, parent::getFillable()) && !in_array($key, parent::getGuarded())) {
            parent::setAttribute($key, $value);

            return;
        }

What do you think?

whereMeta with table name

Hi! I think that whereMeta trait should control that the column names "key" and "value" are properly prepended with table name to avoid any case of ambiguity:

->where($this->getMetaTable() . '.key', '=', $key)->where($this->getMetaTable() . '.value', '=', $value)

    /**
     * whereMeta scope for easier join
     * -------------------------
     */
    public function scopeWhereMeta($query, $key, $value, $alias = null)
    {
        $alias = (empty($alias)) ? $this->getMetaTable() : $alias;
        return $query->join($this->getMetaTable() . ' AS ' . $alias, $this->getQualifiedKeyName(), '=', $alias . '.' . $this->getMetaKeyName())->where($this->getMetaTable() . '.key', '=', $key)->where($this->getMetaTable() . '.value', '=', $value)->select($this->getTable() . '.*');
    }

cant use metable with other packages

when trying to use metable with laravel-stapler package i get a collision of method getattribute and i have to disable laravel-meta or laravel-stapler in order for one of them to work.

Update to Laravel Meta

This library looks great, and a feature like this is definitely needed in Laravel. However, no commit has been done since 2 years ago.

What about updating this package to make it stable and ready for production for Laravel applications of these days?

Is there a way to query multiple meta at once

I tried this but its not getting the results I want

$post = Post::meta()
    ->where(function($query){
          $query->where('posts_meta.key', '=', 'revision')
                ->where('posts_meta.value', '=', 'draft')
                ->where('posts_meta.key', '=', 'type')
                ->where('posts_meta.value', '=', 'blog')
    })

Unnecessary where clause on collection

I think this where can be removed because the relationship method already gets model specific meta.
I get problem with that where because depending on the environment returned collection fields which must be int (e.g foreign key) are casted to string and this where returns null.

protected function getMetaData()
{
if (!isset($this->metaLoaded)) {
$this->setObserver();
if ($this->exists) {
$objects = $this->metas
->where($this->metaKeyName, $this->modelKey);
if (!is_null($objects)) {
$this->metaLoaded = true;
return $this->metaData = $objects->keyBy('key');
}
}
$this->metaLoaded = true;
return $this->metaData = new Collection();
}
}

laravel/framework#11780

Can't retrieve with eager loading

Hi guys,

I'm trying to use the eager load in my laravel project. I have a Term table with his meta table.
When i do this :

$terms = Term::with(['metas'])->get();

I have an error which is saying that the relationship "metas" doesn't exist. Which is true because i dont have a function "metas" in my model. But i don't know what to put inside this function "metas".

Can you help me ?
Thanks

Different branch or release for diff PHP version

Since many of them are still using PHP < 7 and recent commit #52 made it for PHP 7 only, please add support for PHP 7 on a different branch so that no conflict shall occur.

My old project has 5.6 and i had to use dev-master#d3b53b8464de55a4e7cdafd25c5000f74452c759 in my composer.json file

Thanks.

setMeta() doesn't remove if set default value

I notice that setMeta() doesn't remove record if we set default value.

    public $defaultMetaValues = [
        'about'         => 'default'
    ];

    $tester->about = 'default';
    $tester->save(); // remove record from database

    $tester->setMeta('about', 'default')->save();  // doesn't remove

I see that the check if is default value is only in __set() method, but not in setMeta(), this:

https://github.com/kodeine/laravel-meta/blob/master/src/Kodeine/Metable/Metable.php#L383-L391

So it's not an issue, I would like to know why this behaviour?

Thanks for this simple but powerful package!

scopeMeta should use $this->getTable() instead of $this->table

The scopeMeta function uses the table member $this->table, which is only set if there is an explicit override of the table name on a model. Instead it should use the $this->getTable() method and getQualifiedKeyName() method.

If the scopeMeta method is altered as follows it should work in all cases:

public function scopeMeta($query)
{
        return $query->join($this->getTable(), $this->getQualifiedKeyName(), '=', $this->getMetaTable().$this->getMetaKeyName())
            ->select($this->getTable().'.*');
}

Can't save meta with factory builder/seeder

If I create a table seeder for a table that uses metable, the meta columns don't get translated into key/value pairs on the related table. For example:

In database/factories/ModelFactory.php:

$factory->define(App\Models\Business::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->company,
        'short_description' => $faker->text(rand(15,60)),
        'description' => $faker->text,
        // Meta
        'address' => $faker->streetAddress, 
        'city' => $faker->city, 
        'state' => $faker->stateAbbr, 
        'zip_code' => $faker->postcode, 

        'facebook_url' => $faker->url,
        'youtube_url' => $faker->url,
        'google_plus_url' => $faker->url,
        'twitter_url' => $faker->url,
    ];
});

If I were to call (via artisan tinker): factory(App\Models\Business::class,10)->create(), I get this error:

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'address' in 'field list' (SQL: insert into businesses (name, short_description, description, address, city, state, zip_code, facebook_url, youtube_url, google_plus_url, twitter_url, slug, updated_at, created_at) values (Donnelly Inc, Maxime et et sunt pariatur., Impedit autem voluptas necessitatibus blanditiis aut dolores. Est fugiat sunt quisquam. Cumque aut velit ut inventore., 9658 Hane Crest, Elmorestad, AR, 33942, http://www.wilderman.biz/ea-ullam-molestias-aut-quisquam-aspernatur, http://www.wuckert.org/, http://www.quitzon.net/, http://www.reilly.biz/assumenda-nam-iste-in-ea-laboriosam-dolorem-sapiente-fuga, donnelly-inc, 2017-01-26 16:59:25, 2017-01-26 16:59:25))'

What I have to do instead as a workaround, is setting the meta data into a separate factory definition:

$factory->define(App\Models\BusinessMeta::class, function (Faker\Generator $faker) {
    return [
        // Meta
        'address' => $faker->streetAddress, 
        'city' => $faker->city, 
        'state' => $faker->stateAbbr, 
        'zip_code' => $faker->postcode, 

        'facebook_url' => $faker->url,
        'youtube_url' => $faker->url,
        'google_plus_url' => $faker->url,
        'twitter_url' => $faker->url,
    ];
});

And then calling:

factory(App\Models\Business::class,10)->create()->each(function ($business) { $business->setMeta(factory(App\Models\BusinessMeta::class)->make()->toArray()); $business->save(); });

If there's a way to make a direct call to factory()->create() work without the each(), it would make things simpler.

Simplifying querying meta

Instead of:

$post = Post::meta()
    ->where(function($query){
          $query->where('posts_meta.key', '=', 'revision')
                ->where('posts_meta.value', '=', 'draft');
    })

It would be really helpful/nice to have something like this:

$post = Post::whereMeta(['revision'=>'draft'])

Laravel 5.3 support

Hi,

I have upgraded from Laravel 5.2 to 5.3 and Laravel Meta stopped working. I have tested it on a fresh 5.3 installation and there is the same problem. On a fresh 5.2 installation everything works fine. There is no error notification, it just shows me the boolean false after saving and it doesn't save.

Anyone a solution for this problem?

Thanks!

best,
Marc

Eager loading problem

Hi,
after updating to the latest version of this package I'm trying to use eager loading method but when I use it the meta data is selected twice. I'm using debugbar to check the queries.

Here is snippet of debugbar:

screen shot 2017-11-10 at 9 38 15 am

Different types

I realized that even though we created a meta da as Integer, the database appears as String.
I do not think a problem, but you think about upgrading?

scopeMeta doesn't work with tables with custom names

Hello guys!

See scopeMeta

public function scopeMeta($query)
    {
        return $query->join($this->table.'_meta', $this->table.'.id', '=', $this->table.'_meta.'.$this->getMetaKeyName())
            ->select($this->table.'.*');
    }

$this->table.'_meta' will assume that your meta table has "meta" in the name. In my case, my table is called "users_data", then this method will fail.

I just rewrite the function in my model and it's working fine now but I think that it should be fixed to work with tables that doesn't follow that pattern.

I'm posting this issue cause this package allow us to define a custom name for our meta tables.

See you! :)

Delete on cascade

When I delete a parent record, will the meta records also be deleted?

Save meta

It is possible to save the meta with the created method?

$post = Post::find(1);
$post->setMeta('content', 'Some content here');
$post->create($request->all);

Feature Request: Default Values

Currently, I have a situation where I need to note if a user is, for example, out sick for the day.

Of course, the logical way to store this using this plugin would be to only store rows for sick users (since any individual user would rarely be sick). This allows us to reduce DB bloat.

However, to do this, I have to write custom mutators and accessors which will unset the row if the meta value is being set to the default value, and will return the default value if the row does not exist.

Currently:


    public function setArtistactiveMeta($value)
    {

        $default = true;
        if($value == $default) {
          $this->unsetMeta('artistactive');
        } else {
          $this->setMeta('artistactive', $value);
        }
        $this->save();

    }


    public function getArtistactiveMeta()
    {
        $default = true;
        $value = $this->getMeta('artistactive');
        if(is_null($value)) {
          return $default;
        } else {
          return $value;
        }

    }

This isn't horrible for one attribute, but if I was going to add ten attributes with defaults... it gets messy.

This seems like something that could be added into the package, perhaps with a syntax like:

   protected $metaDefaults = [
       'artistactive' => 'true',
   ];

Thoughts?

whereMeta should be static

Hi!
I think whereMeta should be static, so you can do something like:

$posts = Post::whereMeta($key, $value);

    /**
     * Query Meta Table functions
     * -------------------------.
     */
    public function whereMeta($key, $value)
    {
        return $this->getModelStub()
            ->whereKey(strtolower($key))
            ->whereValue($value)
            ->get();
    }

Thanks!

For some reasons, the model try to save metaLoaded and metaData when I try to save my model

When I try to save my model with this:

$client = Client::findOrFail($clientId);
$client->setMeta([
   'extra_info_demand' => $data['demand'],
   'extra_info_financial' => $data['financial'],
   'extra_info_other' => $data['other'],
]);
$client->save();

I get this error:

Unknown column 'metaLoaded' in 'field list' (SQL: update `Client` set `updated_at` = 2017-08-07 10:52:10, `metaLoaded` = 1, `metaData` = [] where `id` = 111

Laravel 7

Hi! how can I install it with laravel 7? thanks!

Scope is no longer using meta table

The b65ca6b commit has caused the scope to no longer select the meta table. As a result I have any queries trying to join in on itself causing major issues. I don't see anything different in the documentation that I am not using in the model.

Can this please be resolved/reverted, or is there a different implementation I am missing to restore this to a working state?

I cannot use laravel-meta in laravel 6 ?

image
I have install this package in my laravel 6 project.
Here is the model that i want to use for
`<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Permissions\HasPermissionsTrait;
use Laravel\Passport\HasApiTokens;

use Kodeine\Metable\Metable;

class User extends Authenticatable
{
use Notifiable, HasPermissionsTrait, HasApiTokens;

use Metable;

protected $metaTable = 'users_meta';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

public function products()
{
    return $this->hasMany(Product::class);
}

}
`
I respect all steps that you ask to install it but i have this error. Do you know why and how i can do to resolve this problem ? Thank You !

Undefined property: App\Models\User::$metaData

Hi,

Using your package for my meta data which works great. However I run into issues when I try to add more traits and stuff to my user model.

This is my setup:

  • User model with your Metable added (works as expected)
  • A trait called AccountBalance which I added after yours which consists of:
trait AccountBalance
{
    public function scopeGetAccountBalance($query)
    {
        return $query->orderBy('id', 'DESC')->first()->pluck('balance');
    }

}

  • My controller which is called from my route:
public function getAccountBalance(Request $request, $id)
    {
        $response = User::getAccountBalance()->find($id);

        return $response;
    }

This results in the error

Undefined property: App\Models\User::$metaData

I can't figure out why.

Thanks.

P.S. can this be caused by the fact that the account balance is in another SQL table which does not have meta data relationships?

Tag Package

Can you add a tag if the package is stable for use?

Thanks

Is it possible to add an extra column or group metas?

Hi
I have 2 type of metas, config metas and content metas.
I would like to create a CRUD for each one, but I will need to find a way to separate them. Maybe adding an extra column like extra.
Have someone faced this situation? Any ideas?
Thanks!

laravel 6 support

I can't update to laravel 6 because composer is telling me:
kodeine/laravel-meta dev-master requires illuminate/support ~5.0|~5.1
and tells me to remove this package or don't update to laravel 6

`getMeta` function parameter

Hi ,
In the readme it states the getMeta function accept meta key params and default value.

$post = $post->getMeta('content', 'Something');

But on the code i check that the params is key and raw, is this the intended behaviour ? using raw = true will return the meta as json

Use Models attributes

I have a Item model and I want to add a new Item to it in the database.
When I try to create an attribute within the model it will be added as Meta attribute.
My model contains an 'title' colmn in the database

// when I create a new item it will store 'title' as meta data and leave the 'title' in my model empty
$item = new Item;
$item->title = 'Blaat2';
$item->save();

result : title will be added as a meta value, title in model will be empty

// When i already stored my item and edit the value like this it works:
$item = Item::find(1);
$item->title = 'Blaat2';
$item->save();

result: now it works. The title attribute of Item will be stored

I'm using the latest version of Laravel, any way I can fix this?

Get meta where key contains

First up, thanks very much for taking the time to develop and release this package - just started using it but it's looking like a very useful tool indeed.

I was wondering if you had any plans to include a method that would allow a query to get all meta for an entity where the key contains a certain string? Not sure if this might be considered a bit 'edgy' but I've run into a situation where I need to save meta data for a model where the individual pieces of metadata need to be unique yet must all relate to one another and should be able to be queried as a group.

I'm dealing the the uniqueness by saving input (which by default is in an array) and changing the array keys to unique keys using uniqid() and a prefix e.g.

location_mYr4nd0mIdent1fi3r
location_4noth3rRand0mIden

Each key value pair would then be saved as separate metadata but I'd like to be able to find them as a group by their shared prefix.

I'll probably be cooking up a solution for my project anyway but thought I'd check to see if you had any plans for something similar. If my solution can be made to fit the package then happy to do a PR if I have time and if you think it would be a useful addition?

Support for 4.2 dropped?

Hey there

The recent update removed laravel 4.2 support, is there a change in the code that requires laravel 5.0/5.1? From what I can see, there doesn't seem to a reason to exclude 4.2, but I may be mistaken.

Meta is always fetch from database

Hello!

For example: I have a list of 10 movies and these movies have some metas. So, when a get all these movies, I have notice that 10 queries where executed to get all metas for each movie.
In a situation where I don't need these metas, I have this queries in the same way.

So, is there any way to not get all that data automatically? Cause I have a situation where I count some data and I don't need any meta. I'm thinking about it cause for each new data in the database, a new query will be executed later. See the image below:

log

Any help? Thanks!

how to query without metas?

Hi
I want to count users by account:
$account->users->count()
but this is getting all the metas for each user, is there a "not eager" way?
thanks

Why is meta querying the whole table?

Hi
I'm using dev-master, reference: d75b7d8

Somehow a whole table select is being triggered which is slowing every request.

What I'm doing is using a relation, and the problem comes up when I call $user->hidden_menu_options which is a meta attribute.

    public function children()
    {
        $user = null;
        $account_id = null;
        if (auth()->check()) {
            $account_id = auth()->user()->account_id;
            if (get_guard() == 'api' || get_guard() == 'web') {
                $user = \App\Models\User::find(auth()->id());
            }
        }

        return $this->hasMany('App\Models\Menu', 'parent_id')
//            ->withoutGlobalScope(AccountScope::class)
            ->when($user, function ($query) use ($user, $account_id) {
                $query->where('account_id', $account_id);
                $query->whereNotIn('id', $user->hidden_menu_options ?? []);
            })
            ->with('children')
            ->where('hide', 0)
            ->orderBy('parent_id', 'asc')
            ->orderBy('order', 'asc');
    }

I'm firing an exception if 'select * from users_meta' is ran.

#0 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(347):
App\Providers\AppServiceProvider->App\Providers\{closure}(Object(Illuminate\Database\Events\QueryExecuted))
#1 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(196):
Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('Illuminate\\Data...', Array)
#2 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(833):
Illuminate\Events\Dispatcher->dispatch('Illuminate\\Data...')
#3 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(687):
Illuminate\Database\Connection->event(Object(Illuminate\Database\Events\QueryExecuted))
#4 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(640):
Illuminate\Database\Connection->logQuery('select * from `...', Array, 0.86)
#5 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(338):
Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#6 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2132):
Illuminate\Database\Connection->select('select * from `...', Array, true)
#7 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2120):
Illuminate\Database\Query\Builder->runSelect()
#8 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2586):
Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#9 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2121):
Illuminate\Database\Query\Builder->onceWithColumns(Array, Object(Closure))
#10
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(523):
Illuminate\Database\Query\Builder->get(Array)
#11
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(507):
Illuminate\Database\Eloquent\Builder->getModels(Array)
#12
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasMany.php(17):
Illuminate\Database\Eloquent\Builder->get()
#13
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(429):
Illuminate\Database\Eloquent\Relations\HasMany->getResults()
#14
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(401):
Illuminate\Database\Eloquent\Model->getRelationshipFromMethod('metas')
#15
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(333):
Illuminate\Database\Eloquent\Model->getRelationValue('metas')
#16 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(321):
Illuminate\Database\Eloquent\Model->getAttribute('metas')
#17
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1523):
App\Models\User->getAttribute('metas')
#18 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(362):
Illuminate\Database\Eloquent\Model->__get('metas')
#19 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(239):
App\Models\User->__get('metas')
#20 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(359):
App\Models\User->getMetaData()
#21 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(125):
App\Models\User->__get('metaData')
#22 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(111):
App\Models\User->getMetaString('hidden_menu_opt...', false)
#23 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(327):
App\Models\User->getMeta('hidden_menu_opt...')
#24
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1546):
App\Models\User->getAttribute('hidden_menu_opt...')
#25
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1591):
Illuminate\Database\Eloquent\Model->offsetExists('hidden_menu_opt...')
#26 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(434):
Illuminate\Database\Eloquent\Model->__isset('hidden_menu_opt...')
#27 /home/vpssigesedev/sigesedev.huayra.com.ar/app/Models/Menu.php(397): App\Models\User->__isset('hidden_menu_opt...')
#28
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(157):
App\Models\Menu->App\Models\{closure}(Object(Illuminate\Database\Eloquent\Builder), Object(App\Models\User))
#29
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php(23):
Illuminate\Database\Eloquent\Builder->when(Object(App\Models\User), Object(Closure))
#30
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(385):
Illuminate\Database\Eloquent\Relations\Relation->forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), 'when',
Array)
#31 /home/vpssigesedev/sigesedev.huayra.com.ar/app/Models/Menu.php(398):
Illuminate\Database\Eloquent\Relations\Relation->__call('when', Array)
#32
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(588):
App\Models\Menu->children()
#33
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(90):
Illuminate\Database\Eloquent\Builder->Illuminate\Database\Eloquent\{closure}()
#34
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(592):
Illuminate\Database\Eloquent\Relations\Relation::noConstraints(Object(Closure))
#35
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(560):
Illuminate\Database\Eloquent\Builder->getRelation('children')
#36
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(540):
Illuminate\Database\Eloquent\Builder->eagerLoadRelation(Array, 'children', Object(Closure))
#37
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(508):
Illuminate\Database\Eloquent\Builder->eagerLoadRelations(Array)
#38 /home/vpssigesedev/sigesedev.huayra.com.ar/app/Models/Menu.php(345): Illuminate\Database\Eloquent\Builder->get()
#39 /home/vpssigesedev/sigesedev.huayra.com.ar/app/Models/Menu.php(321): App\Models\Menu::getMenuElement(169, 'api/v3')
#40 /home/vpssigesedev/sigesedev.huayra.com.ar/app/Http/Controllers/API/v3/UserNotificationController.php(136):
App\Models\Menu::getAppScreen(169, 378)
#41 [internal function]:
App\Http\Controllers\API\v3\UserNotificationController->fullList(Object(Illuminate\Http\Request))
#42 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54):
call_user_func_array(Array, Array)
#43
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45):
Illuminate\Routing\Controller->callAction('fullList', Array)
#44 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219):
Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route),
Object(App\Http\Controllers\API\v3\UserNotificationController), 'fullList')
#45 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176):
Illuminate\Routing\Route->runController()
#46 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(681):
Illuminate\Routing\Route->run()
#47 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130):
Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#48 /home/vpssigesedev/sigesedev.huayra.com.ar/app/Http/Middleware/AccountAPI.php(50):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#49 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
App\Http\Middleware\AccountAPI->handle(Object(Illuminate\Http\Request), Object(Closure))
#50
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#52
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(59):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#53 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 60, '1')
#54
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#55 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
#56 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#57 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(683):
Illuminate\Pipeline\Pipeline->then(Object(Closure))
#58 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658):
Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#59 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(624):
Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#60 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(613):
Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#61 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(170):
Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#62 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130):
Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#63 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(65):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#64 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Barryvdh\Debugbar\Middleware\InjectDebugbar->handle(Object(Illuminate\Http\Request), Object(Closure))
#65
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#66 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#67
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#68 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#69
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#70 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#71
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(63):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#72 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#73 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/fideloper/proxy/src/TrustProxies.php(57):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#74 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171):
Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#75 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105):
Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#76 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(145):
Illuminate\Pipeline\Pipeline->then(Object(Closure))
#77 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110):
Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#78 /home/vpssigesedev/sigesedev.huayra.com.ar/public/index.php(55):
Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#79 {main}#0
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(347):
App\Providers\AppServiceProvider->App\Providers\{closure}(Object(Illuminate\Database\Events\QueryExecuted))
#1 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(196):
Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('Illuminate\\Data...', Array)
#2 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(833):
Illuminate\Events\Dispatcher->dispatch('Illuminate\\Data...')
#3 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(687):
Illuminate\Database\Connection->event(Object(Illuminate\Database\Events\QueryExecuted))
#4 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(640):
Illuminate\Database\Connection->logQuery('select * from `...', Array, 0.69)
#5 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Connection.php(338):
Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#6 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2132):
Illuminate\Database\Connection->select('select * from `...', Array, true)
#7 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2120):
Illuminate\Database\Query\Builder->runSelect()
#8 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2586):
Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#9 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2121):
Illuminate\Database\Query\Builder->onceWithColumns(Array, Object(Closure))
#10
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(523):
Illuminate\Database\Query\Builder->get(Array)
#11
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(507):
Illuminate\Database\Eloquent\Builder->getModels(Array)
#12
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasMany.php(17):
Illuminate\Database\Eloquent\Builder->get()
#13
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(429):
Illuminate\Database\Eloquent\Relations\HasMany->getResults()
#14
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(401):
Illuminate\Database\Eloquent\Model->getRelationshipFromMethod('metas')
#15
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(333):
Illuminate\Database\Eloquent\Model->getRelationValue('metas')
#16 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(321):
Illuminate\Database\Eloquent\Model->getAttribute('metas')
#17
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1523):
App\Models\User->getAttribute('metas')
#18 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(362):
Illuminate\Database\Eloquent\Model->__get('metas')
#19 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(239):
App\Models\User->__get('metas')
#20 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(359):
App\Models\User->getMetaData()
#21 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(125):
App\Models\User->__get('metaData')
#22 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(111):
App\Models\User->getMetaString('hidden_menu_opt...', false)
#23 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(327):
App\Models\User->getMeta('hidden_menu_opt...')
#24
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1546):
App\Models\User->getAttribute('hidden_menu_opt...')
#25
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1591):
Illuminate\Database\Eloquent\Model->offsetExists('hidden_menu_opt...')
#26 /home/vpssigesedev/sigesedev.huayra.com.ar/vendor/kodeine/laravel-meta/src/Kodeine/Metable/Metable.php(434):
Illuminate\Database\Eloquent\Model->__isset('hidden_menu_opt...')
#27 /home/vpssigesedev/sigesedev.huayra.com.ar/app/Models/Menu.php(397): App\Models\User->__isset('hidden_menu_opt...')
#28
/home/vpssigesedev/sigesedev.huayra.com.ar/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(157):
App\Models\Menu->App\Models\{closure}(Object(Illuminate\Database\Eloquent\Builder), Object(App\Models\User))

A new problem, I think

Hi again,

This time, I want to add posts with metas. How can I attach metas to a new post? Or this package is just for update, because I can create a Model just for Metas or I can update metas when I add a new post, in same query.

How to filter via multiple meta values?

Ok, i got the idea on how to filter via only one value, but how to do it on two?
From docs, this is how you filter on one value:

$post = Post::meta()
    ->where(function($query){
          $query->where('posts_meta.key', '=', 'revision')
                ->where('posts_meta.value', '=', 'draft');
    })

Following instructions but getting error

Trait 'Kodeine\Metable\Metable' not found

Here is my model:

<?php
namespace App\Models;

use Kodeine\Metable\Metable;
use Illuminate\Database\Eloquent\Model;

class Site extends Model
{ 
    protected $fillable = ['address', 'city', 'state', 'user_id', 'area', 'zip_code'];
    use Metable;
    protected $metaTable = 'sites_meta';


    public function user()
    {
        return $this->belongsTo('App\User');
    }


}

Please any advice is appreciated

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.