在此处输入图像描述
在此处输入图像描述
您好,我的数据库中有错误。 用户在用户表中注册,现在他必须发布广告。我正在尝试在广告表中获取他的 ID。但它返回我此错误:
SQLSTATE [23000]: 完整性约束冲突: 1048 列 'user_id' 不能为 null (SQL: 插入
ads
(user_id
,services
,head
,level_study
,language
,experience
,localization
,description
,contact
,updated_at
,created_at
( 值(?,家庭,管家,BAC + 2,一点,2 年, Yopougon, pnjjjk , 09789070, 2019-06-05 08:31:38, 2019-06-05 08:31:38((
public function store(Adstore $request)
{
$validated = $request->validated();
$user_id = Auth::user()['id'];
$ad = new Ad();
$ad->user_id = $user_id;
$ad->services = $validated['dropdown_service'];
$ad->titre = $validated['title'];
$ad->niveau_etude = $validated['dropdown_etude'];
$ad->langue = $validated['langue'];
$ad->experience = $validated ['experience'];
$ad->localisation = $validated ['local'];
$ad->description = $validated ['description'];
$ad->contact = $validated ['contact'];
$ad->save();
return redirect()->route('accueil')->with('success','Votre annonce a été postée.');
}
对代码进行一些更改,如下所示:
在控制器中:
public function store(Adstore $request)
{
$validated = $request->validated();
$user_id = Auth::user()->id;
$ad = Ad::create([
'user_id' => $user_id,
'services' => $validated['dropdown_service'],
'titre' => $validated['title'],
'niveau_etude' => $validated['dropdown_etude'],
'langue' => $validated['langue'],
'experience' => $validated['experience'],
'localisation' => $validated['local'],
'description' => $validated['description'],
'contact' => $validated['contact'],
]);
if($ad){
return redirect()->route('accueil')->with('success','Votre annonce a été postée.');
} else {
return redirect()->route('accueil')->with('error','Something went wrong.');
}
}
您必须像这样访问身份验证用户 ID $user_id = Auth::user((->id;
'];" 使用这个:"$user_id = Auth::user((->id;"它会起作用。