我有一个Flair
和Link
模型,并且我还创建了一个名为“ flair_link”的迁移,以保持两者之间的关系。因为Link可以是flairable
,也可以不是基于其Category
,而create_flair_linl_table
也是另一个模型。
这是我针对Schema::create('flair_link', function (Blueprint $table) {
$table->increments('id');
$table->integer('link_id')->unsigned();
$table->foreign('link_id')->references('id')->on('links');
$table->integer('flair_id')->unsigned();
$table->foreign('flair_id')->references('id')->on('flairs');
$table->timestamps();
});
的迁移文件:
LinkController.php
在new()
功能下的public function makeLink(Request $request){
$request->validate([
'title' => 'required',
'url' => 'required|url',
'category_id' => 'required|numeric'
]);
$link = new Link;
$link->title = $request->title;
$link->url = $request->url;
$link->user_id = Auth::id();
$link->category_id = $request->category_id;
if($request->flair) {
// $link->attachFlair((int)$request->flair);
// or
$link->flairs()->attach(Flair::where('id', (int)$request->flair)->firstOrFail());
};
if( $link->save() ){
return redirect()->route('link.show', [ 'id' => $link->id ]);
}
}
上:
tinker
尽管当我使用$link
在$flair
和$link = App\Link::find(1)
$flair = App\Flair::find(1)
$link->flairs()->attach($flair)
之间创建关系时,它以某种奇怪的方式起作用:
null
返回:$link->flairs
,但是当我检查flair
时,我可以看到link
已成功连接到<select name="flair">
@foreach ($category->flairs as $flair)
<option value="{{ $flair->id }}">{{ $flair->name}}</option>
@endforeach
</select>
。
当我制作这样的表格时:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: flair_link.link_id (SQL: insert into "flair_link" ("flair_id", "link_id") values (1, ))
这就是我得到的:
import os
import sys
import pandas as pd
import numpy as np
import re
import random
df = pd.DataFrame({'A' : ['howdy_man_dude', 'howdy_dude', 'howdy_man_dude', 'howdy_dude',
'howdy_dude', 'howdy_dude', 'howdy_man_dude', 'howdy_dude'],
'01-18' : np.random.randn(8).round(2),
'02-18' : np.random.randn(8).round(2),
'03-18' : np.random.randn(8).round(2),
'04-18' : np.random.randn(8).round(2),
'05-18' : np.random.randn(8).round(2),
'06-18' : np.random.randn(8).round(2)})
pattern = '_man_'
df['man'] = df['A'].str.contains(pattern)
print(df)
01-18 02-18 03-18 04-18 05-18 06-18 A man
0 -1.47 -1.21 -0.06 0.12 -1.54 0.10 howdy_man_dude True
1 1.27 0.11 -0.42 -0.20 -1.76 0.47 howdy_dude False
2 -0.24 1.02 1.33 -0.59 -1.67 0.21 howdy_man_dude True
3 -0.36 0.54 1.79 0.14 1.40 1.11 howdy_dude False
4 0.31 0.40 0.21 -0.00 0.81 -1.29 howdy_dude False
5 1.09 -0.47 2.43 0.30 -1.28 0.54 howdy_dude False
6 -0.24 -0.19 0.48 1.94 -0.61 -0.51 howdy_man_dude True
7 1.71 -0.80 0.94 2.47 -0.76 -0.26 howdy_dude False
答案 0 :(得分:1)
我发现了我的错误。为了使@Effect()
addComment$ = this.actions$
.ofType(fromFeature.CommentsActionTypes.AddComment)
.pipe(
switchMap((action: fromFeature.AddComment) => {
return this.commentsService.post(action.payload).pipe(
map(commentWithId => new fromFeature.AddCommentSuccess(commentWithId)),
catchError(error => of(new fromFeature.AddCommentFailed(error)))
);
})
);
函数起作用,我们需要模型。保存链接之前,我正在调用export const adapter: EntityAdapter<Comment> = createEntityAdapter<Comment>({
selectId: (comment: Comment) => comment.id,
sortComparer: (commentA, commentB) => commentA.date === commentB.date ? 0 : commentA.date > commentB.date ? -1 : 1
});
export interface State extends EntityState<Comment> {}
const initialState = adapter.getInitialState({});
export function reducer(state = initialState, action: CommentsActions): State {
switch (action.type) {
case CommentsActionTypes.LoadSuccess:
return adapter.addAll(action.payload, state);
case CommentsActionTypes.AddCommentSuccess:
return adapter.addOne(action.payload, state);
default: return state;
}
}
方法。解决方法是:
const getGuestbookState = createFeatureSelector<fromFeature.State>('guestbook');
export const getCommentsState = createSelector(
getGuestbookState,
(guestbook) => guestbook.comments
);
export const {
selectEntities: selectCommentsEntities,
selectAll: selectAllComments
} = fromComments.adapter.getSelectors(getCommentsState);