PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

[FIXED] How to inherit data from a create within the same store?

 November 15, 2022     laravel, laravel-6, php     No comments   

Issue

I've 2 create in the same function store, but one field of the 1th create it must be the same of a field in the 2th create.

Is it possible?

DataController.php

public function store(Request $request)
    {
        SendData::create($request->validated());

        // How I take here data from firt ::create ??
        $random_data_from_form1 = ??

        $data_from_form2 = $request->input('data_form2');

        SendMoreData::create([
           'field1' => $random_data_from_form1,
           'field2' => $data_from_form2
        ]);
    }

Solution

the create() method with return a new model instance of SendData.

You can update your code like this:

public function store(Request $request)
    {
        $new_object = SendData::create($request->validated());

        // How I take here data from firt ::create ??
        $random_data_from_form1 = $new_object->your_field;

        $data_from_form2 = $request->input('data_form2');

        SendMoreData::create([
           'field1' => $random_data_from_form1,
           'field2' => $data_from_form2
        ]);
    }


Answered By - Allen
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing