Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Piero's avatar
Level 1

Separate collection

I’m developing an app to visualise the calendar of separate collection but I cannot visualise the name of the day, the type of waste and the day of collection.

0 likes
48 replies
automica's avatar

I'm trying to visualise your question. Can you flesh it out a bit to explain what you're trying to do.

Piero's avatar
Level 1

I want to create something like that

Day Type of waste Day of collection

I visualised only the day's ID and the type of waste

tykus's avatar

That is no clearer @piero - what Models are we working with here; can you describe what your database table(s) look like (columns and their types)?

Piero's avatar
Level 1

@tykus

Tables: notes, days and categories.

Days: id -> bigint, name -> varchar.

Categories: id -> bigint, category -> varchar.

Notes:id -> int, day_id -> int, type_id -> int, day_collection_id -> int, hour_begin -> time, hour_end -> time.

automica's avatar

and what do you want the data to look like in your view?

Sounds like you want a model called CollectionDate

Each CollectionDate needs a date_start and date_end which will both be dateTime fields. You can get the day and the time from the DateTime by using Carbon.

Each CollectionDate will belongTo Category

CollectionDate should also have a notes field or BelongTo Note (if notes are applicable to many dates).

so

// CollectionDate

- id
- date_start
- date_end
- category_id
- note_id
Piero's avatar
Level 1

I want to view day (ex. Monday ), type of waste (paper) and day of collection (Thursday) and hour of begin and hour of end.

automica's avatar

@piero so only want to list days of week and each week will be the same as the last?

in which case:

// CollectionDate

- id			// $table->id('id')->primary();
- day_id		// $table->string('day_id');
- start_time	// $table->time('start_time');
- end_time	// $table->time('end_time');
- category_id	// $table->string('category_id');
- note_id		// $table->string('note_id');
Piero's avatar
Level 1

I have 3 models: day, category and note. Day model : <?php

namespace App\Models;

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

class Day extends Model { protected $fillable = []; protected $guarded = [];

use HasFactory;




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


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

}

Note model:

automica's avatar

ensure models are Capitalised:

eg

  • Category
  • Day
  • Note
automica's avatar

I would modify your Note class to:

<?php

namespace App\Models;

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


class Note extends Model // class name should be 'Note'. Filename should be Note.php
{
    use HasFactory;

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

    public function day(){ // this should be singular 
        return $this->hasOne(Day::class);
    }

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

looking at your index.blade.php I would suspect you are not seeing the values you want yet.

if you follow convention you should change your field names to match the respective relationships

eg:

   $table->unsignedInteger('day_id');
   $table->unsignedInteger('category_id');

and set a column called name on both the categories and days tables.

you can the do:

// day
{{ $note->day->name }}

// category
{{ $note->category->name }} 

to get access to the linked relationship.

Piero's avatar
Level 1

@automica So in calendar- > index.blade.php I have to write something like this $note->day->day

automica's avatar

@piero standard practice is to give models a title or name and reference that.

Piero's avatar
Level 1

I have change them with category->name and day->name

I have the following error : Undefined property: stdClass::$day (View: C:\Users\Piero\Desktop\Prova\recycle\resources\views\calendar\index.blade.php)

in notes migration I have this :

          $table->unsignedInteger('day_id');
          $table->unsignedInteger('category_id');
automica's avatar

show me what you have got that causes this error:

I have the following error : Undefined property: stdClass::$day (View: C:\Users\Piero\Desktop\Prova\recycle\resources\views\calendar\index.blade.php)
Piero's avatar
Level 1

I wrote as you advised me

          <h2>{{$n->day->name}}</h2>
automica's avatar

in your blade it should be something like:

            @foreach ($notes as $n )
                <div class="p-1 bd-highlight">
                    <div class="card text-center text-dark " style="width:16rem; left:13rem; top:12rem">
                        <div class="card-header">

                            <h2>{{$n->day->name}}</h2>
                            <h3>{{$n->category->name}}</h3>
						</div>
					</div>
			</div>
@endforeach
automica's avatar

you will need to rerun your migrations if you've made changes to your database.

php artisan migrate:fresh
Piero's avatar
Level 1

In my blade file I have this

                       @foreach ($notes as $n )
                   <div class="p-1 bd-highlight">
                     <div class="card text-center text-dark " style="width:16rem; left:13rem; top:12rem">
                           <div class="card-header">
        
                                <h2>{{$n->day->name}}</h2>
                           </div>
                          <div class="card-body">
                           <h3 class="card-title">{{ $n->category->name}} </h3>
                           <p class="card-text">Giorno di raccolta: {{ $n->day->name }} </p>
                           <p class="card-text">Ora Inizio: {{ $n->ora_inizio }}</p>
                           <p class="card-text">Ora fine:  {{ $n->ora_fine }}</p>
                         </div>
                         <div class="card-footer">
        
                               <div class="row align-items-start ">
                                               <div class="col-md-6">
                                                  <a href="{{route('calendar.edit',$n->id)}}">
                                                      <span class="material-icons">
                                                                 edit
                                                      </span>
                </a>
            </div>
            <div class="col-md-6">
                <form class="delete" action="/calendar/{{$n->id}}" method="POST">
                   @method('DELETE')
                  @csrf
                  <button type="submit" class="btn-link">
                    <span class="material-icons">
                        delete
                    </span>
                  </button>
                </form>
            </div>
        
      </div> 
      </div>
     
    </div>
  </div>
  @endforeach
Piero's avatar
Level 1

ok

I have updated the repository

automica's avatar

I would do:

    $notes = App\Models\Note::all()
        ->orderBy('day_id','asc')
        ->get();

Which will get eloquent relationships from your model.

Piero's avatar
Level 1

If I use the string as suggested I get the following message: Class 'App\Http\Controllers\App\Models\Note' not found

automica's avatar

change

    $notes = App\Models\Note::all()
        ->orderBy('day_id','asc')
        ->get();

to

    $notes = Note::all()
        ->orderBy('day_id','asc')
        ->get();

and add

use App\Models\Note;

in your controller.

your model is still called app/Models/note.php in lower case.

rename to app/Models/Note.php and run the following:

composer dump-autoload to update your autoload.php with the path the models.

Piero's avatar
Level 1

I followed the procedure you described and I have another problem:

Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.

Piero's avatar
Level 1

I’ll try tomorrow in the afternoon, thanks

1 like
Piero's avatar
Level 1

now it works, but I can't see the days and the type of rejection.

automica's avatar

@piero I suspect you manually added your data, which would be why its not there when you refreshed your migrations.

Using seeders in your codebase will make it easier to eventually deploy and allow you to make db changes much easier.

Have a read of https://laravel.com/docs/8.x/seeding to explain how to set them up.

Piero's avatar
Level 1

Now I have the following error:

Trying to get property 'name' of non-object (View: C:\Users\Piero\Desktop\Prova\recycle\resources\views\calendar\index.blade.php)

  <h2>{{$n->days->name}}</h2>
automica's avatar

That’s because your model relationship is for day

Eg $n->day->name

Piero's avatar
Level 1

what do i need to change for it to work?

automica's avatar

@piero just check your index.blade.php uses day (this will match what your relationships in your Note model)

Piero's avatar
Level 1

@automica 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` = 2 and `days`.`note_id` is not null limit 1) (View: C: \ Users \ Piero \ 
  Desktop \ Prova \ recycle \ resources \ views \ calendar \ index.blade.php)
Piero's avatar
Level 1

I've update the repo.

Thanks.

automica's avatar

@piero you can change the relationship for days to belongsTo

class Note extends Model
{
    use HasFactory;

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

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

    public function category(){
        return $this->belongsTo(Category::class);
    }
}
Piero's avatar
Level 1

in the day’ and category model which relationship should I use?

Piero's avatar
Piero
OP
Best Answer
Level 1

@automica Thank you, it functions now, but the day and day of collection are equals.

I have this:

  <h2>{{$n->day->name}}</h2>  // day

 <p class="card-text">Giorno di raccolta: {{ $n->day->name }} </p> // day of collection

How can I change to have collection day?

1 like
Piero's avatar
Level 1

I haven't resolved the problem.

Please or to participate in this conversation.