-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmynote.note
More file actions
229 lines (165 loc) · 7.31 KB
/
mynote.note
File metadata and controls
229 lines (165 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
How to call children relationship class example:
class Drawing extends Eloquent {
public function user() {
return $this->belongsTo('User', 'user_id');
}
// this throws the relation error above, also I beleive this does two queries, anyway to improve this?
public function userLink() {
return $this->user->link;
}
}
class User extends Eloquent {
public function link() {
return $this->morphTo('User', 'type', 'id');
}
}
class Retailer extends Eloquent {
public function user() {
return $this->belongsTo('User', 'id');
}
}
class Manufacturer extends Eloquent {
public function user() {
return $this->belongsTo('User', 'id');
}
}
3
Try this instead:
Drawing::first()->user->link;
or this:
// Drawing model
public function user()
{
return $this->belongsTo('User', 'user_id');
}
// Drawing model
// Requires above relation
public function userLink()
{
return $this->user->link;
}
Then:
$ulink = Drawing::first()->userLink();
Or newer::
Update: Just make changes to your method like this (Create an accessor):
public function getUserLinkAttribute()
{
return $this->user->link;
}
/////////////////////////////////////////////////////////
foreign key behich vajh nmitune null bashe
////////
hale moshkel seed nashodan berkhatere cache
composer dump-autoload
///////////////////////
$lesson->qbanks[0]->questions[0]->questionable
dastresi sade o bi dardesar baraye relation haye morphi besoorate mostaghim
//////
https://stackoverflow.com/questions/49205427/laravel-polymorphic-database-seeder-factory
//////////
dar mored relationship ha tafavot asasie halat adi ba polymorphism dar ine ke dar halat adi ma bayad dar jadavel kelid khareji ra ba ame moshakhas
an jadval tarif konim masanaln dar jadvan comments ha dashte bashim post_id category_id
ama ba polymorphism digar lozoomi be tarif sabet kelid hayekhareji ba namemoshakhas jadval nadarim
yani bejaye post_id ya category_id minevisim commentable_type va commentable_id
hal baraye commente marboot be post drim :
commentable_type=post(namejadval) commentable_id=post_id(id record post dar jadvale khodesh)
////////
dar taeein hasOne belongsTo deghat kon ke class zirin hatman belongsTo bashe
////////////////////
learn method overriding specially in constructor overriding in php
zemnan yadet nare ravabet ham mostaghim ham makoos ta akharin classe zirin anjam bedi
$student=new App\Users\Student
$student->id=1
$learner=new App\Users\Learner;
$learner->id=1
$learner->learnerable()->associate($student);
$user=new App\Users\User
$user->userable()->associate($learner);
$user->save();
////////////////
roles and permission
https://www.itsolutionstuff.com/post/laravel-8-user-roles-and-permissions-tutorialexample.html
///////////////////
public function learner()
{
return $this->morphOne(Learner::class,'learnerable');
}
public function user()
{
//return $this->hasManyThrough(User::class,Learner::class,null,'learnerable_id')->where('learnerable_type',User::class);
return $this->learner->user();//ye rahe sadetar baraye hasManyThrough in Polymorphic ha
}
//////////////////////
be in sabk relation ha deghat kon
$teacher->lessons[0]->sessions[1]->exam->qbank->questions[0]->questionable->output
age shey az class asli yani farzando bekhay az classnameable() estefade mikoni
classname::where(condition,value)
baraye gereftan besoorat collection va dadan be query ha be soorat jami az maghadir field ha
az pluck('fieldname1',...) estefade kon ke class ei az collection havi kolle maghadire field1 e
vaghti esm class masalan Sessione dg baghie tavabe age ba in esm shoroo shan
tabe ghavaed khasian pas moragheb bash
(age mikhay mostaghim class begiri () yadet nare)3
etimes you may want to count the number of related models for a given relationship without actually loading the models. To accomplish this, you may use the withCount method. The withCount method which will place a {relation}_count attribute on the resulting models:
use App\Models\Post;
$posts = Post::withCount('comments')->get();
foreach ($posts as $post) {
echo $post->comments_count;
}
///////////////////////////////////
baraye marboot kardn relation ha
Belongs To Relationships
If you would like to assign a child model to a new parent model, you may use the associate method. In this example, the User model defines a belongsTo relationship to the Account model. This associate method will set the foreign key on the child model:
use App\Models\Account;
$account = Account::find(10);
$user->account()->associate($account);
$user->save();
To remove a parent model from a child model, you may use the dissociate method. This method will set the relationship's foreign key to null:
$user->account()->dissociate();
$user->save();
when you can use associate() when your main class has belongsTo relationship
Here's my notes on how to save and update on all the Eloquent relationships.
in One to One:
You have to use HasOne on the first model and BelongsTo on the second model
to add record on the first model (HasOne) use the save function
example: $post->comments()->save($comment);
to add record on the second model (BelongsTo) use the associate function
example: $user->account()->associate($account); $user->save();
in One to Many:
You have to use HasMany on the first model and BelongsTo on the second model
to add record on the first table (HasMany) use the save or saveMany functions
example: $post->comments()->saveMany($comments);
to add record on the second model (BelongsTo) use the associate function
example: $user->account()->associate($account); $user->save();
in Many to Many:
You have to use BelongsToMany on the first model and BelongsToMany on the second model
to add records on the pivot table use attach or sync functions
both functions accepts single ID or array of ID’s
the difference is attach checks if the record already exist on the pivot table while sync don’t
example: $user->roles()->attach($roleId);
in Polymorphic One to Many:
You have to use MorphMany on the main model and MorphTo on all the (***able) models
to add records on all the other models use the save
example: $course->tags()->save($tag);
the pivot table should have the following columns:
. main model ID
. (***able) ID
. (***able) Type
in Polymorphic Many to Many:
You have to use MorphByMany on the main model and MorphToMany on all the (***able) models
to add records on all the other models use the save or saveMany
example: $course->tags()->save($tag);
example: $course->tags()->saveMany([$tag_1, $tag_2, $tag_3]);
the pivot table should have the following columns:
. main model ID
. (***able) ID
. (***able) Type
in Has Many Through (shortcut):
You have to use HasManyThrough on the first table and have the normal relations on the other 2 tables
this doesn’t work for ManyToMany relationships (where there’s a pivot table)
however there’s a nice and easy solution just for that.
yeki az rahhaye kahesh khataye gpg too commit signings
echo test | gpg --clearsign
baraye privatekey hayer daraye password
hatman yadet bashe dasturat @if o ba @endif bebandi
pluck() maghadir khaso bedoon keys mide ama ba Model::get([]) ba keys mide
baraye estesna kardn file ha tooye git be taghir dadan HEAD deghat kon