I'm working on a part of the application where i see that the best way to obtain the information that i need is via a view on the database. On that view i can apply things line show unique, sort, etc. It seems to me easier than creating the relationships in Laravel, maybe is not but is my upbringing using SQL speaking.
I'm creating a model to handle the view - Is anything special that i should do to the model? I have seen protected $guarded=[] but the posts are several years old.
Sql views are meant to show in a single table the result of a query.
You can keep implementing that solution as long as you just need to manipulate the result and not to insert, edit or update the data in your source tables.
$guarded and $fillable arrays are used to specify wich coloumns should be mass assignable by your model.
If you leave those arrays empty youre basically guarding every single column, wich is what you actually want.
Specifying 1 or more guarded coloumn will set others as fillable.
Specifying 1 or more fillable coloumn will set others as guarded.
You cant use both arrays in you model expecting everything to work fine.
We leverage SQL views for our reporting purposes. One challenge I have faced with a rapidly growing company and changes to logic is how to manage SQL views effectively. I wrote an article a few months ago as to how I do that with my projects.
I like to create a model for my view as in our case we apply filters etc in our frontend and I can then leverage Model scopes which are much cleaner and re-useable.
You don't need to put the update view in a console command if your view definition is not going to change.