0

Calendar

I'm developing an app with Laravel 8 to show the weekly calendar of separate waste collection, but when I go to press the add day button I have the following error: SQLSTATE [23000]: Integrity constraint violation: 1048 Column 'giorno_raccolta_id' cannot be null (SQL: insert into notes ( collection_day_id, start_time, end_time, updated_at, created_at) values (?,?,?, 2021-05-28 12:48:04, 2021- 05-28 12:48:04))

  1. Form.blade.php

         <div class="input-group mb-3">
         <label class="input-group-text" for="inputGroupSelect01">Giorno</label>
        <select name="giorno" class="form-select" id="inputGroupSelect01">
       @foreach ($days as $day)
        <option value="{{$day->id}}" {{$note->giorno_id == $day->id ? 'selected' : ''}} >{{$day->giorno}}</option>
      @endforeach
    </select>
    </div>
    
     @if($errors->has('giorno'))
      <div class="alert alert-danger" role="alert">
       {{ $errors->first('giorno')}}
      </div>
     @endif
    
      <div class="input-group mb-3">
      <label class="input-group-text" for="inputGroupSelect01">Tipologia</label>
      <select name="tipologia" class="form-select" id="inputGroupSelect01">
       @foreach ($categories as $c)
         <option value="{{$c->id}}" {{$note->tipologia_id == $c->id ? 'selected' : ''}} >{{$c->categoria}}</option>
       @endforeach
     </select>
     </div>
    
    @if($errors->has('tipologia'))
      <div class="alert alert-danger" role="alert">
     {{ $errors->first('tipologia')}}
     </div>
    @endif
    
    <div class="input-group mb-3">
    <label class="input-group-text" for="inputGroupSelect01">Giorno raccolta</label>
    <select name="giorno_raccolta_id" class="form-select" id="inputGroupSelect01">
      @foreach ($days as $day)
         <option value="{{$day->id}}" {{$note->giorno_raccolta_id == $day->id ? 'selected' : ''}} >{{$day->giorno}}</option>
      @endforeach
    </select>
    </div>
    
      @if($errors->has('giorno_raccolta_id'))
     <div class="alert alert-danger" role="alert">
        {{ $errors->first('giorno_raccolta_id')}}
     </div>
     @endif
    
     <div class="input-group mb-3">
       <label class="input-group-text">Ora inizio</label>
       <input type="time" name="ora_inizio">
      </div>
    
      @if($errors->has('ora_inizio'))
        <div class="alert alert-danger" role="alert">
          {{ $errors->first('ora_inizio')}}
        </div>
      @endif
    
      <div class="input-group mb-3">
        <label class="input-group-text">Ora fine</label>
        <input type="time" name="ora_fine">
      </div>
    
      @if($errors->has('ora_fine'))
        <div class="alert alert-danger" role="alert">
         {{ $errors->first('ora_fine')}}
       </div>
      @endif
    
     @csrf
    

2.Table Notes

          public function up()
{
    Schema::create('notes', function (Blueprint $table) {
        $table->increments('id')->start_from(1);
        $table->unsignedInteger('giorno_id');
        $table->unsignedInteger('tipologia_id');
        $table->unsignedInteger('giorno_raccolta_id');
        $table->time('ora_inizio');
        $table->time('ora_fine');
        $table->timestamps();
    });
}

3.WeekController

           <?php

              namespace App\Http\Controllers;


               use App\Models\note;
               use App\Models\Day;

               use Illuminate\Http\Request;



             class WeekController extends Controller
              {
               public function index(){
    $notes = note::all();

    return view('calendar.index',
    compact('notes'));
   
}

public function create(){
    $days = Day::all();
    $categories = Category::all();
    $notes = new note();
    
    return view('calendar.create',
    compact('days', 'categories', 'notes'));
}


public function store(){
        
        note::create($this->validateRequest());
        return redirect()->route('calendar.index');
}




public function show(note $note){
    $note = note::find($note)->first();
    return view('calendar.show',compact('note'));
}


public function edit(note $note){
    $days = Day::all();
    return view('calendar.edit',compact('note','days'));
}



public function update(note $note){
    $note ->update($this->validateRequest());


    return redirect()->route('calendar.show',$note->id);
}


public function destroy(note $notes){
    $notes->delete();
    return redirect()->route('calendar.index');
}


private function validateRequest(){
    return request()->validate([
        'giorno_id' => 'required|unique:notes',
        'tipologia' => 'required',
        'giorno_raccolta' => 'required',
        'ora_inizio' => 'required',
        'ora_fine' => 'required'
    ]);
}



 }

I also have another problem, when I go to save the data in the database nothing is saved. Can you help me troubleshoot the app.

The days and types of refusals I recover from two other tables:

  1. Days Table

          public function up()
        {
           Schema::create('days', function (Blueprint $table) {
             $table->id();
             $table->string('giorno');
             $table->timestamps();
          });
        }
    

2.Categories table

      public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->string('categoria');
        $table->timestamps();
    });
}

Note Model

       <?php

           namespace App\Models;

           use Illuminate\Database\Eloquent\Factories\HasFactory;
           use Illuminate\Database\Eloquent\Model;
           use App\Models\Day;



          class note extends Model
         {
           use HasFactory;

            protected $fillable = [];
            protected $guarded = ['id'];


            public function days(){
            return $this->hasOne(Day::class);
           }
         }

Day model

      <?php

         namespace App\Models;

          use Illuminate\Database\Eloquent\Factories\HasFactory;
          use Illuminate\Database\Eloquent\Model;
          use App\Models\note;
          use App\Models\Category;

          class Day extends Model
    {
         use HasFactory;


        public function notes(){
          return $this->hasOne(note::class);
    }

       public function category(){
      return $this->belongsTo(category::class);
     }
   }

Category model

        <?php

         namespace App\Models;

        use Illuminate\Database\Eloquent\Factories\HasFactory;
        use Illuminate\Database\Eloquent\Model;
        use App\Model\Day;

        class Category extends Model
       {
         use HasFactory;

        public function days(){
        return $this->hasMany(Day::class);
       }

      }

1 Answers1

0

I suggest you make $fillable and pass it the attributes you want to make mass assignable https://laravel.com/docs/8.x/eloquent#mass-assignment. In the other hand, I see that all your select elements has the same id, since DOM id element must be unique. And finally, check on the create method the $request received data

....(Request $request)
{
   dd($request)
}
Prospero
  • 2,190
  • 2
  • 4
  • 18
  • Now I have the following problem: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'days.note_id' in 'where clause' (SQL: select * from `days` where `days`.`note_id` = 1 and `days`.`note_id` is not null limit 1) (View: C:\Users\Piero\Desktop\Prova\recycle\resources\views\calendar\index.blade.php) –  May 30 '21 at 15:39
  • Check the one-one relationship between Notes and Days, its' wrong. https://laravel.com/docs/8.x/eloquent-relationships#one-to-one – Prospero May 30 '21 at 22:15
  • I have two problems, the first I cannot add and save the data in the database; the second I would like to retrieve the name of the day, the day of collection and the type of waste. –  May 31 '21 at 14:02