Dylano236
Dylano236

Reputation: 317

Laravel Request Input is not usable data

Ok maybe this is a noob laravel question but when I'm trying to store data from a form I used a $request->input in a query to get a needed field for insert but the query will not run. Note: does run if I just set something like $project_id = 6.

 public function store(Request $request)
    {
         $project_id = $request->input('project_id');

         $company = Project::where('id', $project_id)->first();

         if(Auth::check()){
            $task = Task::create([
                'name' => $request->input('name'),
                'project_id' => $project_id,
                'company_id' => $company->id,
                'days' => $request->input('days'),
                'hours' => $request->input('hours'),
                'user_id' => Auth::user()->id
            ]);
            if($task){
                return redirect()->route('tasks.index')
                ->with('success' , 'Task created successfully');
            }
        }

            return back()->withInput()->with('errors', 'Error creating new task');
    }

Note:

I've tried a couple different things I've found online like $project_id = $request->project_id or $project_id = $request['project_id']

Is request->input just used for inserts and can't be used as a normal varible?

Update: here is the create.blade form it's coming from

@extends('layouts.app')

@section('content')



<div class="row col-md-9 col-lg-9 col-sm-9 pull-left " >
<h1>Add a Task </h1>

      <!-- Example row of columns -->
      <div class="col-md-12 col-lg-12 col-sm-12" style="background: white; margin: 10px;" >

      <form method="post" action="{{ route('tasks.store') }}">
                            {{ csrf_field() }}



                            <div class="form-group">
                                <label for="project-name">Name<span class="required">*</span></label>
                                <input   placeholder="Enter name"  
                                          id="project-name"
                                          required
                                          name="name"
                                          spellcheck="false"
                                          class="form-control"

                                           />
                            </div>




                            <div class="form-group">
                                <label for="task-days">Days Taken<span class="required"></span></label>
                                <input     
                                          id="task-days"
                                          required
                                          name="days"
                                          type="number"
                                          spellcheck="false"
                                          class="form-control"

                                           />
                            </div>

                            <div class="form-group">
                                <label for="task-hours">Hours Taken<span class="required"></span></label>
                                <input     
                                          id="task-hours"
                                          required
                                          name="hours"
                                          type="number"
                                          spellcheck="false"
                                          class="form-control"

                                           />
                            </div>



                                  <input   
                                  class="form-control"
                                  type="hidden"
                                          name="project_id"
                                          value="{{ $project_id }}"
                                           />
                     @if($projects != null)
                                           <div class="form-group">
                                           <label for="company-content">Select Project</label>
                                           <select name="project_id" class="form-control">
                                           @foreach($projects as $project)
                                           <option value="{{$project_id}}">{{ $project->name }}</option>
                                           @endforeach
                                           </select>

                            </div>
                            @endif

                            <div class="form-group">
                                <input type="submit" class="btn btn-primary"
                                       value="Submit"/>
                            </div>
                        </form>


      </div>
</div>

      <div class="col-sm-3 col-md-3 col-lg-3 col-sm-3 pull-right">
          <div class="sidebar-module sidebar-module-inset">
          <h4>Actions</h4>
            <ol class="list-unstyled">

            <li><a href="/tasks">All tasks</a></li>
            </ol>
          </div>


        </div>


    @endsection

Upvotes: 1

Views: 535

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29306

Let's examine your <form> below:

<input class="form-control" type="hidden" name="project_id" value="{{ $project_id }}"/>
@if($projects != null)
<div class="form-group">
    <label for="company-content">Select Project</label>
    <select name="project_id" class="form-control">
        @foreach($projects as $project)
        <option value="{{ $project_id }}">{{ $project->name }}</option>
        @endforeach
    </select>
</div>
@endif

In this code, you have a hidden input with the name "project_id", and if $projects is not null, you also have a select with the name "project_id". Having multiple elements with the same name is invalid, and can cause issues.

Secondly, in this line:

<option value="{{ $project_id }}">{{ $project->name }}</option>

$project_id is the same value you have in the hidden input above. When you're looping over $projects, this should be $project->id:

<option value="{{ $project->id }}">{{ $project->name }}</option>

Lastly, make sure that $project_id is a valid value if you're going to send it, and consider adjusting your logic to only send the hidden input if $projects is null:

@if($projects != null)
<div class="form-group">
    <label for="company-content">Select Project</label>
    <select name="project_id" class="form-control">
        @foreach($projects as $project)
        <option value="{{ $project->id }}">{{ $project->name }}</option>
        @endforeach
    </select>
</div>
@else
<input class="form-control" type="hidden" name="project_id" value="{{ $project_id }}"/>
@endif

With all that adjusted, you should be able to retrieve the expected value with $request->input("project_id")

Upvotes: 1

Related Questions