Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel hasMany detach

I am having an issue with Eloquent regarding removing child model: When this is executed in process2() I still have the deleted model which is not ok.

Model

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class Model1 extends Model
    {
        public function seasons() {
            return $this->hasMany('App\Models\Seasons', 'series_id', 'id');
        }
    }

Service

class Process {
    public function process1($model1Instance) {

        for($model1Instance->seasons() as $season) {
            if(//whatever//) {
                $season->delete();
            }
        }
    }
    public function process2($model1Instance) {
        for($model1Instance->seasons() as $season) {
            //At this point I still have the deleted instance
        }
    }
}

Usage

$proc = new Process();
......
$proc->process1($model1Instance);
$proc->process2($model1Instance);

When process1() removes a model from the parent, how can I have it removed in process2()?

Tried/Will try:

1.Method: $model1Instance->seasons()->detach($season); but got: Call to undefined method Illuminate\Database\Query\Builder::detach()

2.Another class I could make another simple class to store these but I do not think it is okay, although I could then set filtered seasons but still have to use Model1 instance:

class Model1Copy {
    private $seasons;
    public function __construct($seasons) {
        $this->seasons = $seasons;
    }
}
  1. Fatal when tried:

    public function process1($model1Instance) {

    for($model1Instance->seasons() as $season) {
        if(//whatever//) {
            $season->delete();
        } else {
        $childs[]=$season;
        }
    }
    $model1Instance->seasons = $childs
    

    }

  2. Would be to make my own repositories to skip the ORM`s behavior, but it is frustrating because I have to rewrite all queries just to remove an instance...

like image 439
ka_lin Avatar asked Sep 18 '15 23:09

ka_lin


People also ask

What is with () in Laravel?

with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.

What is BelongsTo in Laravel?

BelongsTo is a inverse of HasOne. We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with User and Phone models. I'm giving hasOne relation from User to Phone. class User extends Model { /** * Get the phone record associated with the user.

Has One relation Laravel?

Using the hasOne relationship in Laravel First of all, we can explain a simple example of the use of hasOne: if an article has comments and we want to take one of them with the details of the article, we can use the hasOne relationship or a user can have a profile table.


1 Answers

To delete hasMany records with condition:

$model1Instance->seasons()->where('condition', 'met')->delete();

To delete all:

$model1Instance->seasons()->delete();
like image 114
Stubbies Avatar answered Sep 19 '22 16:09

Stubbies