简体   繁体   中英

Add data into more than 1 table from Controller in Laravel

I have two table donor and address .

I want to insert all the basic details in donor table and address into address table with donor id.

捐赠者

地址

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Donor;

class DonorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('donor.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $donor = new Donor;
        $donor->salutation = $request->input('salutation');
        $donor->gender = $request->input('gender');
        $donor->first_name = $request->input('first_name');
        $donor->last_name = $request->input('last_name');
        $donor->phone = $request->input('phone_home');
        $donor->mobile = $request->input('phone_mobile');
        $donor->email = $request->input('email');
        $donor->occupation = $request->input('occupation');
        $donor->is_active = $request->input('status');
        $donor->is_deleted = 0;
        $donor->created_by = 1;
        $donor->updated_by = 1;
        $donor->save();


        return redirect('/donor')->with('success', 'Hurray!! New donor Created.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

How to enter details into address table.

If a donor has an address, and the address is a unique item which has its own table, you should create a model for Address, and use the eloquent relationships to connect these two together.

You can read more about eloquent relationships here

So in your Donor model, you would go something like this:

public function address(){
     return $this->hasOne('App\Address');
}

And in your Address class:

public function donor(){
     $this->belongsTo('App\Donor');
}

So, in your donor controller, you'll create a new instance of address, and connect it to the donor:

$address = new App\Address;
... Your stuff to populate the address
$donor->address()->save($address);

Note: this is not the entire code to achieve what you want, and I wrote it from memory. You should do some research to figure it out.

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