Support the ongoing development of Laravel.io →
Requests Input Forms
Last updated 2 years ago.
0

Hi. It would help to know what's not working about it. Are you getting exceptions? Is it just not storing the data?

It's impossible to help without all the details.

FYI, in order to use $request->all() you have to have the private $fillable array filled out on your model. Otherwords you will get an exception saying that you can't populate the values.

Also, just some helpful suggestions; you could clean your code up quite a bit by using a for loop. Since all 4 of your form elements are the same, you could do something like;

@for ($i=0;$i<=6;$i++)
<div class="col">
    <input type="hidden" name="form[{{ $i }}][program]" value="{{ $program->id }}">
    <input type="hidden" name="form[{{ $i }}][day]" value="1">
    <select class="form-control" name="form[{{ $i }}][workout]">
        <option selected>---</option>
        <option value="work">Entraînement</option>
        <option value="free">Congé</option>
    </select>
</div>
@endfor

It will clean your template up quite a bit.

Last updated 6 years ago.
0

Oh yeah! My dd($request->all()); :

array:2 [▼
  "form" => array:7 [▼
    0 => array:3 [▼
      "program" => "1"
      "day" => "1"
      "workout" => "work"
    ]
    1 => array:3 [▼
      "program" => "1"
      "day" => "2"
      "workout" => "work"
    ]
    2 => array:3 [▼
      "program" => "1"
      "day" => "3"
      "workout" => "work"
    ]
    3 => array:3 [▼
      "program" => "1"
      "day" => "4"
      "workout" => "work"
    ]
    4 => array:3 [▼
      "program" => "1"
      "day" => "5"
      "workout" => "work"
    ]
    5 => array:3 [▼
      "program" => "1"
      "day" => "6"
      "workout" => "free"
    ]
    6 => array:3 [▼
      "program" => "1"
      "day" => "7"
      "workout" => "work"
    ]
  ]
  "_token" => "huR4e97aqa35ei9cQdlHVP0B8zA1OzwsFCA1yM1j"
]

Thank to your suggestion, I didn't think about it! How to sort the result?

0

I'm still not sure what issue you are having. Are you getting errors on your submit when trying to store the data?

0

Yeah!

I try foreach, fill and forceCreate and received some errors.

When use foreach $request->program isn't defined. The variable is empty.

When I use fill, I received : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'program_id' cannot be null (SQL: insert into workouts (program_id, day, workout, updated_at, created_at) values (, , , 2018-02-21 17:47:03, 2018-02-21 17:47:03))

$workout = new Workouts;
        
        $workout->fill([
            'program_id' => $request->program,
            'day' => $request->day,
            'workout' => $request->workout,
        ]);
        $workout->save();

When I use forceCreate : Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /Applications/MAMP/htdocs/leveluplifev2/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 681

Workouts::forceCreate($request->all());
$workout->save();

My Workouts model :

class Workouts extends Model
{
    protected $fillable = [
        'program_id', 'day', 'workout',
    ];
    
    public function program()
    {
        return $this->belongsTo(Programs::class);
    }
}

I don't know why I use to insert my request in my table.

0

You should try using

foreach ($request->form as $program) {
  $workout = new Workout;
  $workout->program_id = $program->program;
  $workout->day = $program->day;
  $workout->workout = $program->workout;
  $workout->save();
}

Try that, should work as you need it to

0

I received this error message : Trying to get property of non-object.

Laravel hightlight this line : $workout->program_id = $program->program;

When I try dd($program);, I received this result :

array:3 [▼
  "program" => "1"
  "day" => "1"
  "workout" => "work"
]
0

Hmm what does the table for programs look like? It may be $workout->program->program_id or whatever the column name is you are trying to add to this workout.

0

The schema I use to create table :

    public function up()
    {
        Schema::create('workouts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('program_id')->unsigned();
            $table->integer('day')->unsigned();
            $table->enum('workout', ['work','free']);
            $table->timestamps();
            $table->foreign('program_id')->references('id')->on('programs')->onDelete('cascade');
        });
    }

When I try dd($program->progam);, I received this error message : Trying to get property of non-object.

Last updated 6 years ago.
0
Solution

My apologies. $program['program'] $program['day'] and $program['workout'] they are an array not an object.

0

Thank you very much!

I congratulate now! It's fonctional.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Gwendolau gwendolau Joined 8 Feb 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.