How to add a relationship if the field value is 1 using hasmany() doctrine, php - doctrine

How to add a relationship if the field value is 1 using hasmany() doctrine, php
Example
I am having user table and user_locations with multiple locations.
I need to get a record of non deleted locations from the user location table using user Obj.
By default the relation should be added to the records if the locations were not deleted from the user_locations
Note: The location will be deleted by the field is_deleted=1

Related

Laravel 6 check if a json field contains a value in a collection

I am trying to see if a user id is contained in a json field in my laravel collection. In my postgresql database I have a user_id field which contains a list of integers that pertain to my user model and table. I am aware in eloquent I can use the whereJsonContains method but I am not finding an equivalent method for a collection. How can I go about searching this user_id column for a specific user id?
check the manual for the aviable methods list:
https://laravel.com/docs/8.x/collections#available-methods
In your case i guess that is about:
where

What to backfill with in type-column on polymorphic table?

I am changing my File-model to go from one to many to be a one to many polymorphic relation. Before only my User-model could have a file but now my model Guest will also be able to have a file.
So before I had a column in the file table named user_id I have renamed that one following the pattern here: https://laravel.com/docs/7.x/eloquent-relationships#many-to-many-polymorphic-relations
So it is now named fileable_id.
In addition to the renaming I have added a column named fileable_type. I now want to backfill that column so everything existing points on the User-model. What should I write in that column?
I have tried App\User and User without success.
App\User should be correct if that is how you have your User model namespaced. The polymorphich relationships use the getMorphClass() method on the Eloquent model. You can verify this by doing:
$u = new User();
dd($u->getMorphClass());
If you have not created a custom polymorphic type for the User model, then getMorphClass() should be equivalent to User::class.

How Laravel Eloquent references the right table

I'm currently watching the Laravel Tutorial Episode 7
https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
I created the database and populated its data on the previous episode,
it is only this time, the tutorial introduces model, upon creating the model name "Task" via php artisan make:model Task, it automatically connects to my table "tasks" without any clue how it happened.
So how did a freshly out of the box model knows it?
It's general definition standard.
If you create a table with plural name and create model for this table as singular, then model automatically connects corresponding table, otherwise you should define table name in model like:
protected $table = 'task_table';
It's a convention Laravel follows. Laravel will search for a table that is the snake cased Model's name in plural unless you indicate another table.
If you generate the model with the migration flag (e.g. php artisan make:model Task --migration), it will also create a table with the model's name in plural automatically (in this case, Tasks).
You can check more about it in the documentation.
Table Names
Note that we did not tell Eloquent which table to use for our Flight
model. By convention, the "snake case", plural name of the class will
be used as the table name unless another name is explicitly specified.
So, in this case, Eloquent will assume the Flight model stores records
in the flights table. You may specify a custom table by defining a
table property on your model (...)

Laravel Eloquent : two keys relationship

I have a first table named "Building", and this one gets a Type and a Level. There are multiple building with same type and level.
I have another table named "BuildingInfo", who also gets a Type and a Level. The couple Type/Level is unique on this table.
Is there a way to have a HasOne relationship on Building, giving me the matching BuildingInfo ? The problem being that there are two keys on each table to get it, and I can only specify one key for each table with HasOne().
Thank you.

Unable to add a table from an existing database to EDMX using Database First Entity Framework

I am using Database First EF to generate model from the existing database. When I first generated the models, it ignores only one of the table, the entity was not added to EDMX, no model file is created for the table and no context is created for the entity.
When I tried to explicitly add the table to EDMX (when generating the model, selected the specific table first and then updated the model with all the other tables from the database), it complained with the following error.
Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.
This specific table has two columns which are primary keys of some other tables and both the columns are specified as Primary keys for the table.
Am I doing something wrong or should I handle this table differently since it has two columns defined as Primary Keys? Any suggestions greatly appreciated!
You are not doing anything wrong. Your table is junction table for many-to-many relation. You don't need that table in the model because EF (in contrast to database) can handle many-to-many relation directly without any intermediate. The table is actually mapped on behind of the many-to-many relation - you will see that in mapping details window.
Btw. you are not using code first. Code first = no EDMX.

Resources