Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

I think your missing a param in the where

Program::where('name', '=' ,Input::get('name'))->delete();
Last updated 2 years ago.
0

TerrePorter said:

I think your missing a param in the where

Program::where('name', '=' ,Input::get('name'))->delete();

No, the default is "=" . It deletes the row in programs table so it works, it just doesn't fire the deleting event and the description isn't deleted

Last updated 2 years ago.
0

ckissi said: No, the default is "=" . It deletes the row in programs table so it works, it just doesn't fire the deleting event and the >description isn't deleted

Ok, I've never used it without so it looked off to me.

Not to insult, but have you check the value that is getting returned? Input::get('name')

Maybe the query log can show you the query, dd(DB::getQueryLog());

Or I think you can dump the query with ->toSql(); after the delete()

Last updated 2 years ago.
0

Humm,

Maybe,

 static::deleting(function()
        {
        $this->description()->delete();
        
        return parent::delete();
        });

I think you can also put a where in there, if you didn't have the relationship setup.

static::deleting(function()
        {
        $this->description()->where('id',$this->id)->delete();
        
        return parent::delete();
        });
Last updated 2 years ago.
0

Ok, the previous code just didn't look right.

So after some testing, this finally worked.

In the Program model, I changed it to just this

static::deleting(function($program)
        {
            $program->description()->delete();
        });

To delete,

Program::where('id', 1)->first()->delete();

I had to call with the ->first() or it just didn't work. I found a reference to the ->first() required at https://github.com/laravel/framework/issues/2536

The dd(DB::getQueryLog()); shows all of the queries,

array (size=4)
  0 => 
    array (size=3)
      'query' => string 'select * from `programs` where `id` = ? limit 1' (length=47)
      'bindings' => 
        array (size=1)
          0 => int 11
      'time' => float 0.82
  1 => 
    array (size=3)
      'query' => string 'select * from `descriptions` where `descriptions`.`program_id` in (?)' (length=69)
      'bindings' => 
        array (size=1)
          0 => int 11
      'time' => float 0.94
  2 => 
    array (size=3)
      'query' => string 'delete from `descriptions` where `descriptions`.`program_id` = ?' (length=64)
      'bindings' => 
        array (size=1)
          0 => int 11
      'time' => float 1.44
  3 => 
    array (size=3)
      'query' => string 'delete from `programs` where `id` = ?' (length=37)
      'bindings' => 
        array (size=1)
          0 => int 11
      'time' => float 1.28
Last updated 2 years ago.
0

TerrePorter said:

Humm,

Maybe,

static::deleting(function()
       {
       $this->description()->delete();
       
       return parent::delete();
       });

I think you can also put a where in there, if you didn't have the relationship setup.

static::deleting(function()
       {
       $this->description()->where('id',$this->id)->delete();
       
       return parent::delete();
       });

You will not have access to $this from a static method.

Last updated 2 years ago.
0

mcraz said: You will not have access to $this from a static method.

Yeah, it started looking off to me after a bit

Last updated 2 years ago.
0

@TerrePorter, thanx ! , it was the "first()" thank you for the reference to https://github.com/laravel/framework/issues/2536 They should mention this in the documentation. I wasted 6 hours because of this :(

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ckissi ckissi Joined 9 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.