tarang19
1 week ago

update record if exists or create new record laravel 7

Posted 1 week ago by tarang19

i wanted to do update record if exists or create new record laravel 7 using updateOrCreate but its create new entry please help

public function poststepOne(Request $request)
    {
        $active_user = User::find(Auth::user()->id);
        $validate = $request->validate([
            'preference' => ['required'],
            'branch' => ['required'],
        ]);

        $validate['user_id'] = $active_user->id;
        $validate['is_saved'] = 1;
        //dd($validate);
        Preference::updateOrCreate($validate);
        //return redirect()->route('products.create.step.two');
    }

Form

<form action="{{ route('preference.create.step.one.post') }}" method="POST">
                @csrf

                <div class="card">
                    <div class="card-header">Step 1: Preferences</div>

                    <div class="card-body">
                            <fieldset class="mailing">  
                               <!-- <legend class="mailing__title">Fill All * Details</legend> -->
                                <div class="form-check">
                                <label for="preference">Select Year <span class="imp ml-1">*</span></label>
                                <label for="preference">
                                    <input  id="preference" name="preference" value="Second Year" 
                                    @if($current_user_data){
                                    {{ $current_user_data->preference == 'Second Year' ? 'checked' : '' }}
                                }
                                @endif
                                     autocorrect="off" type="radio" />
                                     Second Year 
                                    
                                </label>
                                <label for="preference">
                                    <input  id="preference" name="preference" value="Third Year" 
                                    @if($current_user_data){
                                    {{ $current_user_data->preference == 'Third Year' ? 'checked' : '' }}
                                }
                                @endif
                                     autocorrect="off" type="radio" />
                                     Third Year 
                                    
                                </label>
                                @error('preference')
                                <p class="help text-danger">{{ $errors->first('preference') }}</p>
                                @enderror
                                </div>

                                <div class="form-group">
                                <label for="inputState">Branch</label>
                                <select id="branch" name="branch" class="form-control">
                                    <option selected disabled>Choose...</option>
                                    @foreach($allbranch as $branch)
                                    <option value="{{ $branch->branch }}" 
                                        @if($current_user_data){
                                        {{ $current_user_data->branch == $branch->branch ? 'selected' : '' }}
                                        }@endif
                                        >{{ $branch->branch }}
                                    
                                </option>
                                    @endforeach
                                </select>
                                @error('branch')
                                <p class="help text-danger">{{ $errors->first('branch') }}</p>
                                @enderror
                                </div>
                            </fieldset>
                            
                        
                    </div>

                    <div class="card-footer text-right">
                        <button type="submit" class="btn btn-primary btn btn-success mb-5 mt-2" name="submit">Next</button>
                    </div>
                </div>
            </form>

Please sign in or create an account to participate in this conversation.