Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mada72's avatar

updateOrCreate for 3 tables

I have 3 tables;countries(id, name), cities(id, name, postal_code, country_id) and addresses(id, street,city_id) in view/addresses/edit.blade: ...

@extends('layouts.admin') @section('content')

Addresses

{!! Form::model($address,['method'=>'PATCH', 'action'=>['AdminAddressesController@update', $address->id], 'files'=>true]) !!} {!! Form::label('street', 'Street:') !!} {!! Form::text('street', null, ['class'=>'form-control']) !!} {!! Form::label('city_name', 'City:') !!} {!! Form::text('city_name',$address->city->name, null, ['class'=>'form-control']) !!} {!! Form::label('city_postal_code', 'Postal Code:') !!} {!! Form::text('city_postal_code',$address->city->postal_code, null, ['class'=>'form-control']) !!} {!! Form::label('country_name', 'Country:') !!} {!! Form::text('country_name', $address->city->country->name, null, ['class'=>'form-control']) !!} {!! Form::submit('Update Address', ['class'=>'btn btn-primary']) !!} {!! Form::close() !!}

{!! Form::open(['method'=>'DELETE', 'action'=>['AdminAddressesController@destroy', $address->id],
   'files'=>true])
    !!}
<div class="form-group">
    {!! Form::submit('Delete Address', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}

@stop

...

how can use updateOrCreate? ex. I want to change 'country_name' in address -if a country exist -update address, if not -create a new country and update address

0 likes
0 replies

Please or to participate in this conversation.