glasstream2000
4 months ago
507
8
Laravel

Pass data to another database if a checkbox is checked

Posted 4 months ago by glasstream2000

What I'm trying to achieve is when a user fills out a form and they check a checkbox, then the form is submitted into 2 separate DB's with the same structure.

database.php

'mysql' => [
            'driver' => 'mysql',

and second DB'

'mysql_external' => [
            'driver' => 'mysql',

Both Databases are on the same server and have the same credentials and are both referenced in the env file.

My form:

{!!Form::open(['action' => '[email protected]','method' => 'POST'])!!}
<form>
  <div class="form-group row">
    <label for="first_name" class="col-2 col-form-label">First Name</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="first_name" name="first_name" placeholder="First Name" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="last_name" class="col-2 col-form-label">Last Name</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="last_name" name="last_name" placeholder="Last Name" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="year_born" class="col-2 col-form-label">Year Born</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="year_born" name="year_born" placeholder="Year Born" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="last_four" class="col-2 col-form-label">Last Four of SSN</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="last_four" name="last_four" placeholder="Last Four" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="location" class="col-2 col-form-label">Location</label> 
    <div class="col-10">
      <select id="location" name="location" class="custom-select" required="required">
        <option value="">Please select a location</option>
          @foreach ($locations as $location)
        <option value="{{ $location->locations }}">{{ $location->locations }}</option>
        @endforeach
      </select>
    </div>
  </div>
  <div class="form-group row">
    <label for="email" class="col-2 col-form-label">Email</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="email" name="email" placeholder="Email" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div> 
  <div class="form-check">
    <input type="checkbox" class="form-check-input" id="MED">
    <label class="form-check-label" for="MED">Employee?</label>
  </div>
    <input name="module" type="hidden" value="Awareness Walks" id="module" />
  <div class="form-group row">
    <div class="offset-2 col-10">
      <button name="submit" type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>
                        {!! Form::close() !!}

And my controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Listing;
use App\Location;
use App\Module;
use DB;
use Yajra\Datatables\Datatables;

class ListingsController extends Controller
{
    
    public function __construct()
    {
        $this->middleware('auth'); 
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    // Pull all records from DB and modify route and controller
    public function getData() 
    {
       
     }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       $locations = Location::orderBy('locations', 'asc')->get();
   
       return view('createlisting', compact('locations'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|digits:4',
            'last_four' => 'required|numeric|digits:4',
            'location' => 'required',
            'email' => 'required|email',
            
        ]);
        
        // create record
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        
        $listing->save();

        $MED = $request->input('MED' == 1);
        $listing = DB::connection('mysql_external'){
        $listing->first_name = $request->input('first_name')
        $listing->last_name = $request->input('last_name'),
        $listing->year_born = $request->input('year_born'),
        $listing->last_four = $request->input('last_four'),
        $listing->location = $request->input('location'),
        $listing->email = $request->input('email'),
        $listing->module = $request->input('module'),
        $listing->user_id = auth()->user()->id 
        $listing->save();
        
        };
        return redirect('/dashboard')->with('success', 'Module Completed');
        
    }

I have tried multiple ways by searching online and the docs, just not sure how to implement it. Anyone give me any pointers or relevant docs online that is about what i'm trying to do specifically? Thank you in advance!

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