Update hidden value after soft deleting in Laravel

I have four tables:

. Here are their Models:

Agroindustria

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Agroindustria extends Model
{
    use SoftDeletes;

    protected $table = "agroindustria";
    protected $primaryKey = "CodAgroindustria";
    public $incrementing = false;
    protected $keyType = 'string';

    public $fillable = ['CodAgroindustria, Porte'];
    public $hidden = ['created_at', 'updated_at', 'deleted_at'];

    public function pessoa () {
        return $this->setConnection('diana')->hasOne(Pessoa::class, 'CodPessoa', 'CodAgroindustria');
    }

    public function pessoajuridica()
    {
        return $this->setConnection('diana')->hasOne(PessoaJuridica::class, 'CodPessoa', 'CodEndereco');
    }

    public function endereco()
    {
        return $this->setConnection('diana')->hasOne(PessoaJuridica::class, 'CodEndereco', 'CodEndereco');
    }

    public function estado(){
        return $this->setConnection('diana')->hasOne(Estado::class, 'CodEstado', 'estado');
    }

    public function cidade(){
        return $this->setConnection('diana')->hasOne(Cidade::class, 'CodCidade', 'cidade');
    }
}

Pessoa:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Pessoa extends Model
{
    // use SoftDeletes;

    protected $table = "pessoa";
    protected $primaryKey = "CodPessoa";
    public $incrementing = false;
    protected $keyType = 'string';
    protected $connection = "diana";

    public $hidden = ['created_at', 'updated_at', 'EXCLUIDO', 'LastSend'];
    public $fillable = ['email', 'TelRes', 'TelCel'];

    public function endereco()
    {
        return $this->hasOne('App\Endereco', 'CodEndereco', 'CodEndereco');
    }

    public function pessoafisica()
    {
        return $this->hasOne('App\PessoaFisica', 'CodPessoaFisica', 'CodPessoa');
    }

    public function pessoajuridica()
    {
        return $this->hasOne('App\PessoaJuridica', 'CodPessoaJuridica', 'CodPessoa');
    }

}

The PessoaJuridica and Endereco Models are pretty much the same as the Pessoa Model.

When I soft delete my Agroindustria, the deleted_at column updates successfully, but I'm struggling with updating the EXCLUIDO column values from 0 to 1 in my other models. Here's the delete function I created in my AgroindustriaController:

public function deletar (Request $request)
{
    try {
        $Agroindustria = Agroindustria::where('CodAgroindustria', $request['CodAgroindustria']);
        $Agroindustria->delete();

        $Pessoa = Pessoa::findOrFail($request['CodPessoa']);
        if ($Agroindustria->delete()) {
            DB::table('Pessoa')->where('CodPessoa', $Pessoa->CodPessoa)
                ->update(array('EXCLUIDO' => 1));
        }

        return response()->json([
            'error' => false,
            'data' => [
                'message' => 'Dados deletados com sucesso',
            ]
        ]);
    } catch (Exception $e) {
        return response()->json([
            'error' => true,
            'message' => [$e->getMessage()]
        ]);
    }
}

Upvotes: 0

Views: 906

Answers (2)

Solved it by doing:

$Agroindustria = Agroindustria::where('CodAgroindustria', $request['CodAgroindustria']);
$dlt = $Agroindustria->delete();

if ($dlt) {
    Pessoa::where('CodPessoa', $request['CodPessoa'])
        ->update(array('EXCLUIDO' => 1));
    PessoaJuridica::where('CodPessoaJuridica', $request['CodPessoaJuridica'])
    ->update(array('EXCLUIDO' => 1));
    Endereco::where('CodEndereco', $request['CodEndereco'])
    ->update(array('EXCLUIDO' => 1));
}

Thank you all!

Upvotes: 0

Sunil Tiwari
Sunil Tiwari

Reputation: 26

second line in try

$Agroindustria->delete();

write this line like this

$dlt = $Agroindustria->delete();

after that in your if condition put this variable $dlt like this

if ($dlt) {
        DB::table('Pessoa')->where('CodPessoa', $Pessoa->CodPessoa)
            ->update(array('EXCLUIDO' => 1));
    }

Upvotes: 1

Related Questions