mercredi 27 décembre 2017

How to save relationship between 2 tables

How to save data from the one form to two tables and save relationship between them?

I have model Company:

public function maps()
{
    return $this->belongsTo('App\Map');
}

and model Map:

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

and form with inputs:

  • company name
  • localisation (lat and long from google maps)

and mysql tables:

  • companies (id, name, etc.)
  • maps (id, lat, long)
  • company_map (company_id, map_id)

and store:

public function store(CreateCompanyFormRequest $request)
{
$input = Request::all();
$company = new Company($request->all());
Auth::user()->companies()->save($company);
$lat = $request->input('lat');
$long = $request->input('long');
$maps = new Map([
    'lat' => $lat,
    'long' => $long
    ]);
$maps->save();
return $input;
}

How to save relationship between Comapny and Map (company_id and map_id)? Best thanks! :)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2DmNqmF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire