lundi 30 mai 2016

Soft Deleting through relationships laravel 5

I want to soft delete a row and the rows of another table which references this model, but keep getting mysql foreign key constraint error, which i presume is due to the fact that the foreign key still exists and the row just has an updated deleted_at column

User Migration:

public function up()
{
    Schema::create(self::TABLE_NAME, function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->nullable();
        $table->string('name')->nullable();
        $table->string('email')->nullable();
        $table->string('img_url')->nullable();
        $table->string('location')->nullable();
        $table->string('bio')->nullable();
        $table->string('following_user_ids')->nullable();
        $table->string('following_channel_ids')->nullable();
        $table->string('follower_ids')->nullable();
        $table->string('social_id')->unique();
        $table->string('role')->default(3);
        $table->string('password')->nullable();
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
    });
}

Report Migration:

public function up()
{
    Schema::create(self::TABLE_NAME, function (Blueprint $table) {
        $table->increments('id');
        $table->string('location')->nullable();
        $table->string('channel_ids')->nullable();
        $table->unsignedInteger('user_id');
        $table->string('url')->unique();
        $table->string('title');
        $table->string('video_name')->nullable();
        $table->string('description');
        $table->double('lat')->nullable();
        $table->double('lng')->nullable();
        $table->string('comments')->nullable();
        $table->integer('like_status')->default(0);
        $table->string('liker_ids')->nullable();
        $table->string('thumb_url_small')->nullable();
        $table->string('thumb_url')->nullable();
        $table->string('thumb_url_large')->nullable();
        $table->integer('view_count')->default(0);
        $table->timestamps();
        $table->softDeletes();
    });

    Schema::table(self::TABLE_NAME, function (Blueprint $table) {
        $table->foreign('user_id')->references('id')->on(CreateUsersTable::TABLE_NAME);
    });
}

User Model

public static function boot()
{
    parent::boot();

    static::deleting(function($user)
    {
        $user->reports()->delete();
    });
}

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

public function reports()
{
    return $this->hasMany('App\Report', Report::COL_USER_ID, self::COL_ID);
}

Report Model

public function user()
{
    return $this->hasOne('App\User', User::COL_ID, self::COL_USER_ID);
}

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

Deleting user

 public static function deleteUser($id)
{
    // Added this because Model static::deleting/deleted had no effect
    // this soft deletes the report, but i fail to delete user on key constraint error at delete user
    try {
        // delete users reports if they exist
        $reports = Report::where(Report::COL_USER_ID, $id)->get();

        if (isset($reports)) {
            foreach ($reports as $report) {
                DeleteReportController::deleteReport($report->id);
            }
        }
    } catch (\Exception $ignored) {
    }

    try {
        User::where(Report::COL_ID, $id)->delete();
    } catch (\Exception $e) {
        throw new ModelNotFoundException('Could not delete user. ' . $e->getMessage(), 404);
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1X7Cf8Y
via IFTTT

Aucun commentaire:

Enregistrer un commentaire