Write data to db based on user input

Posted 2 years ago by FailWhale

I'm trying to implement a form where users can select some high-level attributes, which will then be used to look up specific data and store it in a DB. I currently have a form where the user provides the specific details themselves, but this requires lots of effort required to get the specifics and type them in. My idea is to have a user form set some variables which will be used in a background query that will feed the create (or store method) on my controller, but I haven't been able to get it working.

The user input form is just a standard html form with a post method. Here's my controller:

<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;
use App\User;
use DateTime;
use App\Role;
use App\Sesh;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SeshController extends Controller
{
    public function index()
    {
      $inputs = Sesh::query()
        ->orderBy('id', 'desc')
        ->take(10)
        ->get(); 

      return view('sesh.index', compact('inputs'));
      
    }



    public function define()
    {
        return view('sesh.define');

    }
    public function create()
    {
      $id = Auth::user()->id;
        $hours = '-3 hour';  // I want the user form to set this variable
        $buoy = 'pau';       // I want the user form to set this variable as well
      $date = new DateTime;
          $date->modify($hours);
          $formatted_date = $date->format('Y-m-d H:i:s');

        $stats = DB::table('report.stations')
            ->where([
                    ['Time', '>=', $date],
                    ['BUOY', $buoy],
                    ])
                ->first();

          $tide = DB::table('report.hourly_tide_level')
                ->where('date', '>=', $formatted_date)
                ->first();


        return view('sesh.create', compact('stats', 'tide'));
    }
  

  
    public function store(Request $request)
    {
       $this->validate($request, [
            'location' => 'required|max:255',  // user form input field
            'direction' => 'max:255',          // set based on the results of the $stats query
            'size' => 'required|max:5',        // set based on the results of the $stats query
            'duration' => 'max:255',           // set based on the results of the $stats query
            'tide' => 'max:4',                 // set based on the results of the $tide query
            'rating' => 'max:1',               // user form input field
            'comments' => 'max:255',           // user form input field
            'date' => 'date',                  // generated automatically
            'reporter' => 'max:255'            // generated automatically from {{Auth::user->username}}
            
        ]);



        Sesh::create($request->all());

        return redirect()->route('pau.index');
    }

Thank you

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