Tuesday, July 11, 2017

how to save data to multiple tables in the same form

Asked by: mohamed gaber


I have 2 tabels and one form. tabel customers and tabel address. i made the relation like that. Address model

public function customer()
{
    return $this->belongsTo(Customer::class);
}

and in the Customer model i have that

public function address()
{
    return $this->hasMany(Address::class);
}

how can i save the address tabel?? i already done with customer i can save, update and delete. i read the DOC Eloquent of laravel but i don't really get it. Here is my controller

class CustomerController extends Controller {

public function index()
{
     $customer = Customer::all();
    $customer = Customer::with('address')->get();;

    return view('customer.index', compact('customer'));

}

   public function create()
{       
    return view('customer.create');
}


public function store(CustomerRequest $request , Customer $customer)
{
    $customer = Customer::create($request->all());
    $address = Address::create($request->all());


    return redirect()->route('customer.index',compact('customer','adderss'));
}


public function show(Customer $customer)
{
    return view('customer.show',compact('customer'));

}


public function edit(Customer $customer)
{
    return view('customer.edit', compact('customer'));

}


public function update(CustomerRequest $request, $id)
{

    $customer = Customer::find($id)->update($request->all());

    return redirect()->route('customer.index',compact('customer'));
}

public function destroy($id)
{
    Customer::destroy($id);

    return redirect()->route('customer.index');
}
}

any suggestions.



Source

No comments:

Post a Comment

Adbox