0

Let me explain about my problem. I am currently using Laravel 5.0. Here is my structure

  • Table: bgts, Model: Bgt, Controller: BgtController
  • Table: bgthistories, Model: BgtHistory

Now I want to do these:

Everytimes creating new item into bgts table, I want to make a copy and insert into bgthistories table. Then, everytimes that record is updated, i'll copy one more version, still insert into bgthistories.

Here is store() method.

public function store(Request $request) {
    $bgt = new Bgt();
    $history = $this->coppy($bgt);
    $uploader = new UploadController('/data/uploads/bgt');
    $bgt->name = $request['name'];
    $bgt->avatar = $uploader->avatar($request);
    $bgt->attachments($uploader->attachments($request));
    //dd($bgt);
    $bgt->save();
    $history->save();
    return redirect('bgt');
}

And this is the coping:

public function coppy($bgt) {

        $array = $this->$bgt->toArray();

        $version = new BgtHistory();
        foreach($array as $key => $value) {
            $version->$key = $value;
        }

        return $version;
    }

I create migration tables already. Everything is ready. But, when I call

 $bgt->save();
 $history->save();

It did not work. If I remove $history->save();, it create new record ok. I think the save() method that built-in in Model provided by Laravel is problem. Can anyone tell me how to solve this.

I tried to build the raw query then executed it by DB:statement but it did not work too. Every try to execute anything with DB is failing.

1 Answer 1

1

Please research before re-inventing the wheel.

(Same stuff different sites in case one is down)

http://packalyst.com/packages/package/mpociot/versionable

https://packagist.org/packages/mpociot/versionable

https://github.com/mpociot/versionable

Cheers and good luck ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.