laravel-favorite icon indicating copy to clipboard operation
laravel-favorite copied to clipboard

Missing id column in default favorites table migration

Open belamov opened this issue 3 years ago • 4 comments

Missing column id in default favorites table migration

when i try to add some model to favorites it generates following query, which requires id column

SQL: insert into "favorites" ("user_id", "favoriteable_id", "favoriteable_type", "updated_at", "created_at") values (1, 2, App\Models\Institution\Institution, 2021-04-19 06:19:59, 2021-04-19 06:19:59) returning "id"

i dont really realize why and where this is happening, i guess it has something to do with relationtship registration

Possible implementation

add id column to default migration, or figure out why eloquent adds requiring "id" to its query and get rid of it

My environment

  • php 7.4
  • laravel 8.36.2

belamov avatar Apr 19 '21 06:04 belamov

im not the only one who has this issue, see #13

belamov avatar Apr 19 '21 06:04 belamov

ok so I found out those code in eloquent:

// If the model has an incrementing key, we can use the "insertGetId" method on
// the query builder, which will give us back the final inserted ID for this
// table from the database. Not all tables have to be incrementing though.
$attributes = $this->getAttributes();

if ($this->getIncrementing()) {
    $this->insertAndSetId($query, $attributes);
}

seems like we need to add this code to Favorite model

public function getIncrementing()
{
    return null;
}

ill add pr shortly

belamov avatar Apr 19 '21 06:04 belamov

App\Models\Course::isFavorited must return a relationship instance. (View: E:\laragon\www\Courses\resources\views\admin\courses\show.blade.php)

What is the solution to this error?

BaraaDev avatar Apr 30 '21 08:04 BaraaDev

Hey, have you resolved your problem?

mdbtekny avatar Mar 02 '22 16:03 mdbtekny