I'm trying to visualise your question. Can you flesh it out a bit to explain what you're trying to do.
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.
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
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)?
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.
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
I want to view day (ex. Monday ), type of waste (paper) and day of collection (Thursday) and hour of begin and hour of end.
@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');
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:
ensure models are Capitalised:
eg
- Category
- Day
- Note
What do you mean?
in your code snippet
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Category;
use App\Models\note;
you have note as lowercase. Classes should be capitalised eg Note
see https://github.com/alexeymezenin/laravel-best-practices#follow-laravel-naming-conventions
I capitalized on the Note model. The following link show the calendar: https://i.ibb.co/wSGtLSC/calendar.jpg
where the first number should be the day (Monday, ...), the second represents the type of waste and the third the collection day.
Github repo: https://github.com/pierre1590/recycle
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.
@automica So in calendar- > index.blade.php I have to write something like this $note->day->day
@piero standard practice is to give models a title or name and reference that.
@automica It doesn't function.
@piero can you share the code you can't get to work?
with your current naming convention you can do
$n->category->categoria // category
$n->day->giorno // day
but you will need to have
$table->unsignedInteger('category_id');
$table->unsignedInteger('day_id');
in your notes migration.
eg: https://github.com/iammikek/recycle/pull/1/files
See the documentation for how hasOne https://laravel.com/docs/8.x/eloquent-relationships#one-to-one
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');
you should reference it as $note->day->name and $note->category->name where $note is one Note object.
Check my PR to see what I mean. https://github.com/iammikek/recycle/pull/1/files
so what should i change?
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)
I wrote as you advised me
<h2>{{$n->day->name}}</h2>
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
you will need to rerun your migrations if you've made changes to your database.
php artisan migrate:fresh
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
ok
I have updated the repository
I would do:
$notes = App\Models\Note::all()
->orderBy('day_id','asc')
->get();
Which will get eloquent relationships from your model.
If I use the string as suggested I get the following message: Class 'App\Http\Controllers\App\Models\Note' not found
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.
I followed the procedure you described and I have another problem:
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
I’ll try tomorrow in the afternoon, thanks
now it works, but I can't see the days and the type of rejection.
@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.
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>
That’s because your model relationship is for day
Eg
$n->day->name
what do i need to change for it to work?
@piero just check your index.blade.php uses day (this will match what your relationships in your Note model)
@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)
Ah. We might need to flip the relationship
see https://stackoverflow.com/questions/30058949/should-i-use-belongsto-or-hasone-in-laravel/30058999
Would be good to see your latest changes, so if you can push up your code to your repo, I'll give that a look tomorrow.
I've update the repo.
Thanks.
@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);
}
}
in the day’ and category model which relationship should I use?
You need belongsTo for both these relationships (as per my code snippet).
It’s all in https://laravel.com/docs/8.x/eloquent-relationships
@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?
@piero So mark this thread as solved.
I haven't resolved the problem.
Please or to participate in this conversation.