Breaking News

php – Lareval set id to the other table at the DB transaction

Questions:

Here is my DB Transaction

 public function store(Request $request)
    {

        DB::beginTransaction();
        try {
            $superdoc = SuperDoc::create([

                'user_id' => $request->user_id,
                'project_name' => $request->project_name,
                'status' => $request->status,
                'start_date' => $request->start_date,
                'end_date' => $request->end_date,
                'lines' => $request->lines,
                'words' => $request->words,

            ]);

            $lastInsertedId=DB::select('SELECT MAX(ID) FROM super_docs');

        } catch (ValidationException $e) {
            DB::rollBack();
            return redirect()->back();
        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }
        try {


            $document = Document::create([
                'superD_id' => $request->superD_id,
                'url' => $request->url,
                'status' => $request->status,
                'text' => $request->text,
                'doc_type' => $request->doc_type,
                'language' => $request->language,
                'date' => $request->date,

            ]);
           return response()->json($superdoc, 201);

        } catch (ValidationException $e) {
            DB::rollBack();
            return redirect()->back();
        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }
        DB::commit();
    }

I want to set the super_doc Auto generated last id into document table superD_id in the above transaction. Can anyone help me?

How to&Answers:

I don’t have Knowledge about laravel bu i can give an idea, After inserting into SuperDoc table , you can get that model and the model will give you the model superD_id , that superD_id you can use in the Document Table.

So i think your code will look like this:

public function store(Request $request)
{

    DB::beginTransaction();
    try {
        $superdoc = SuperDoc::create([

            'user_id' => $request->user_id,
            'project_name' => $request->project_name,
            'status' => $request->status,
            'start_date' => $request->start_date,
            'end_date' => $request->end_date,
            'lines' => $request->lines,
            'words' => $request->words,

        ]);

        //$lastInsertedId=DB::select('SELECT MAX(ID) FROM super_docs');

    } catch (ValidationException $e) {
        DB::rollBack();
        return redirect()->back();
    } catch (Exception $e) {
        DB::rollback();
        throw $e;
    }
    try {


        $document = Document::create([
            //$superdoc will have object of saved data which will also have SuperD_id
            'superD_id' => $superdoc->superD_id,
            'url' => $request->url,
            'status' => $request->status,
            'text' => $request->text,
            'doc_type' => $request->doc_type,
            'language' => $request->language,
            'date' => $request->date,

        ]);
       return response()->json($superdoc, 201);

    } catch (ValidationException $e) {
        DB::rollBack();
        return redirect()->back();
    } catch (Exception $e) {
        DB::rollback();
        throw $e;
    }
    DB::commit();
}

About rankhawks

Check Also

string – How to replace Microsoft-encoded quotes in PHP

I need to replace Microsoft Word’s version of single and double quotations marks (“ ” …

php – How to sub directory project redirecting multiple old pages to new pages

Here is an example of some of the following pages I need to move, basically …

php – Customizing wordpress navigation header with woocommerce my account

I am new to wordpress and I was creating a eCommerce website with woocommerce, then …

php – How to return value to index page from an included script page?

Questions: index.php include.php This code works perfectly fine but I want to return the value …

php – How to add product variation SKU and PRICE in Woocommerce?

Questions: I try to put the price and SKU beside the variation dropdown woocommerce but …

Leave a Reply

Your email address will not be published. Required fields are marked *