1

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
7
  • What does your <input name="project_id"> element look like? This would definitely help if you posted the HTML <form> or AJAX request you're submitting. Also, sidenote, $request->input("project_id"), $request->project_id and $request["project_id"] should all work if you're actually sending project_id in the request; they're syntax alternatives (not sure about the last one, array access, but it should work) Commented Nov 13, 2019 at 15:19
  • Ok give me a second I'll post it Commented Nov 13, 2019 at 15:20
  • I see some issues; gimme a sec. Commented Nov 13, 2019 at 15:26
  • Add a type="text" to <input id="project-name> Commented Nov 13, 2019 at 15:27
  • ok let me try this Commented Nov 13, 2019 at 15:28

1 Answer 1

1

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")

Sign up to request clarification or add additional context in comments.

3 Comments

Ok tim. It works like this when creating projects with adding companies though. If I go from a certain company I pass the id and that field is not shown. but if I go from just "add a project" location the dropdown appears. I have to logout now but this is probably the answer here, and when I log back in and work it I'll give you the answer. Appreciate your help.
Tim Lewis. Thanks! the $project_id turned into $project->id was exactly what it was. And makes obvious sense. Sorry for the delay, had an urgent issue yesterday and couldn't get back on until now.
No problem at all! Glad you got this figured out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.