简体   繁体   中英

Laravel update one-to-many relationship

I have a request that contains the next data

{
  "path": "some path",
  "site_link": "http://example.com"  
  "features": [ ... ] //array of strings
}

I wish to update my two tables site and site features .

site table contains | id | path | site_link | | id | path | site_link | .

And site features these | id | feature | site_id | | id | feature | site_id | I don't know exact amount of "features" however I need to update them after request.

Is there any way to update them except for delete and then create once again. (some features may be changed, some be deleted and be added).

For now I don't have any code for updating features. Only this

public function update(Request $request, Site $site)
    {
        $site->update([
            "path" => $request->path,
            "site_link" => $request->link
        ]);

        return response()->json($site, 200);
    }

Well, in your situation you can't update your site_features rows something like that. Because you need to second identifier in your site_features table. Below query updates only first column again and again. So, only way to save these features create new rows with create method.

İf you want to update specific site_features column, you have to pass ids of those features to controller

public function update(Request $request, Site $site)
    {
       // first you need to select a row before updating it
        $site = $site->where("site_link",$request->site_link)->first(); 
        $site->update([
            "path" => $request->path,
           ]);

        foreach($request->features as $feature){
            SiteFeature::where("id",$feature->id)
             ->update([
                "feature" => $feature->content
             ]);
          }

        return response()->json($site, 200);
    }

If you don't have id parameter yet, if these are new features then you have to create new records about them

public function update(Request $request, Site $site)
    {
        $site = $site->where("site_link",$request->site_link)->first(); 
        $site->update([
            "path" => $request->path,
           ]);

        foreach($request->features as $feature){
            SiteFeature::create([
                "feature" => $feature
             ]);
          }

        return response()->json($site, 200);
    }

And, there third way to this. If your features are limited. For example, there are only 80 features about these websites. Some websites have some features and others not.. Then you may want to use many to many relationship. Create features table and add all features, and make many to many to relationship between features and sites. After that, you only need to sync these features. It's very easy to use. And also when you need to some changes about one of the feature. You don't have do mass changes. Just change this feature on features table then this effect all websites.

I hope you understand me, because my english is so awfull :)

Well first of all hope you've created a model for site-features table. It's pretty easy. Now after this

$updates =  SiteFeatures :: where ('feature',$feature)->where('site_id',$siteID) ->get();

for Updation:-

$updates->feature = $newFeature;
$updates->save();

for Deletion:-

$updates->delete();

for Addition of new feature you don't need the first statement instead

$newFeature = new SiteFeatures();
$newFeature->feature = 'the new feature';
$newFeature->site_id = the_site_id;
$newFeature->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM