jeudi 26 novembre 2015

Laravel - insert multiple models from appended inputs

I'm new to Laravel and I'm trying to insert n models from n appended html inputs Does Laravel's `Eloquent have a better way than this ?

public function store(Request $request, Domain $domain, Server $server)
{
    $new_domain = $domain->create($request->all());
    $server->fill($request->only(['srv_hostname','srv_ip','srv_port']));
    $servers = array();
    $total = count($server['attributes']['srv_hostname']);
    for ($i=0;$i<$total;$i++){
        $p = new Server;
        $p->domain()->associate($new_domain);
        $p->srv_hostname = $server['attributes']['srv_hostname'][$i];
        $p->srv_ip = $server['attributes']['srv_ip'][$i];
        $p->srv_port = $server['attributes']['srv_port'][$i];
        $p->save();
    }
    return redirect()->route('domains.index');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire