Mysql Can';t在存储方法中传递数据
我不明白为什么我无法在我的存储方法中传入“name”Mysql Can';t在存储方法中传递数据,mysql,laravel,eloquent,controller,Mysql,Laravel,Eloquent,Controller,我不明白为什么我无法在我的存储方法中传入“name” 此行:-'name'=>$id->name, 我检查过了,变量不是空的 这是我的控制器:- $id = DB::table('friends') ->where('created_by', '=', $request->created_by) ->where('friends_id', '=', auth()->id()) ->first(); dd($id->
此行:-
'name'=>$id->name,
我检查过了,变量不是空的 这是我的控制器:-
$id = DB::table('friends')
->where('created_by', '=', $request->created_by)
->where('friends_id', '=', auth()->id())
->first();
dd($id->name);
if ($id && $id->friends_id == auth()->id()) {
Post::create([
'title'=>$data['title'],
'body'=>$data['body'],
'created_by'=>$request->created_by,
'user_id'=>Auth::user()->id,
'filled_by'=>Auth::user()->uuid,
'name' => $id->name,
]);
} else {
Post::create([
'title'=>$data['title'],
'body'=>$data['body'],
'created_by'=>$request->created_by,
'user_id'=>Auth::user()->id,
'filled_by'=>Auth::user()->uuid,
]);
}
确保在
Post
中,列名字段是可填充的
protected $fillable = [
'title','body','created_by','user_id','filled_by','name'
];
我建议你按照这种方式来处理你的if情况
$post = [
'title'=>$data['title'],
'body'=>$data['body'],
'created_by'=>$request->created_by,
'user_id'=>Auth::user()->id,
'filled_by'=>Auth::user()->uuid
]
if ($id && $id->friends_id == auth()->id()) {
$post['name'] = $id->name;
}
Post::create($post);
你犯了什么错误?您的意思是插入值,但在表中,它显示为null?“name”在我的数据库中可为null,因此我没有收到任何错误,“if station”也在运行perfectlyrest所有内容都被传递到数据库,除了name。这是一个愚蠢的错误……我忘了将其添加到“protected filleble”中谢谢还有4分钟我才能绿色勾选你的答案