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;
        //return redirect()->route('products.create.step.two');


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

                <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" 
                                    {{ $current_user_data->preference == 'Second Year' ? 'checked' : '' }}
                                     autocorrect="off" type="radio" />
                                     Second Year 
                                <label for="preference">
                                    <input  id="preference" name="preference" value="Third Year" 
                                    {{ $current_user_data->preference == 'Third Year' ? 'checked' : '' }}
                                     autocorrect="off" type="radio" />
                                     Third Year 
                                <p class="help text-danger">{{ $errors->first('preference') }}</p>

                                <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 }}" 
                                        {{ $current_user_data->branch == $branch->branch ? 'selected' : '' }}
                                        >{{ $branch->branch }}
                                <p class="help text-danger">{{ $errors->first('branch') }}</p>

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

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