vendredi 6 septembre 2019

How to attach a relationship the right way in Laravel

I want to a connect two tables with a relation. The the tables get updated but not as expected. In one case i get 2 times the same record, other method i got only one record with no data.

With the attach method i get two times the same record and each time i'm sending the same request it creates the same records as before.

With the sync method it only adds the last record from the loop. I think sync is the way to go, because it doesn't create new records when sending the same request.

    // Network Model
    public function accounts() {
        return $this->belongsToMany('App\Account');
    }

    // Account Model
    public function networks() {
        return $this->belongsToMany('App\Models\Network')- 
        >withPivot('url');
    }


    // Updateprofile function in accounts controller
    $accounts = $request->get('accounts');

    foreach ($accounts as $key => $account) {

       $accountModel = Account::where("id", "=", $account['id'])- 
       >first();

       /* 
          Some other fields that get updated ...
       */

       $networks = 
       Network::find(Helper::getValueOrDefault(AuthConst::NETWORK, 
       $account, []));

       foreach ($account['network'] as $key => $network) {
          $accountModel->networks()->sync($network, ['url' => 
          $network['url'], 'network_id' => $network['id']]);
        }

        $accountModel->save();
     }

DB structure

Accounts Table
ID | And some metafields

Network Table
ID | Name | slug

Account_network relation Table
ID | network_id | account_id | url

The request gives an array called network with one or more objects

array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    int(3)
    ["url"]=>
    string(19) "http://facebook.com"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(2)
    ["url"]=>
    string(18) "http://youtube.com"
  }
}

I expect that this wil attached to the right account with two records added to the Account_network table.

ID | network_id | account_id | url 
1 | 3 | 1| http://facebook.com
2 | 2 | 1| http://youtube.com

And i expect when sending the same request, it does nothing because the records already exists.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire