0

enter image description here

enter image description here

Hello I have an error in my database. the user registers in the users table and now he has to post an ad. I'm trying to get his id in the ads table. But it returns me this error:

SQLSTATE [23000]: Integrity constraint violation: 1048 Column 'user_id' can not be null (SQL: insert intoads (user_id, services, head,level_study, language,experience, localization,description, contact,updated_at, created_at) values ​​(?, Household, Housekeeper, BAC + 2, a little, 2years, 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.');
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    What does `$user_id` contain? `var_dump($user_id);` – beingalex Jun 05 '19 at 12:54
  • Try dumping the content of `$user_id` using `dd($user_id);`. Also consider dumping the entire user object: `dd(Auth::user());` Can you post the result? – Jerodev Jun 05 '19 at 13:05
  • Try $user_id = Auth::user()->id instead Auth::user()['id']; Btw, small optimization: use mass assignable instead of individual property assignment. It will be way cleaner. – user10128333 Jun 05 '19 at 15:32
  • When I do this here is the error: Trying to get property 'id' of non-object And if I put in comment // $ user_id = Auth :: user () -> id; and I dump like that dd (Auth :: user ()); I have this result: null. – Kouassi DJE Jun 05 '19 at 15:44
  • You need to get authenticated first in order to use `Auth::user()` – Vipertecpro Jun 05 '19 at 19:02

3 Answers3

0

Make some changes in code as per below:

In controller:

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.');
    }    
}
0

You have to access auth user id like this $user_id = Auth::user()->id;

  • I do not have access – Kouassi DJE Jun 06 '19 at 16:28
  • public function login(Request $request) { $phone=$request->phone; if (Auth::attempt(['phone' => $phone]) ) { //return redirect()->intended('/'); dd(Auth::user()); } else { return redirect()->back()->with( 'error', "Echec!" ); } – Kouassi DJE Jun 06 '19 at 16:29
0

Instead of "$user_id = Auth::user()['id'];" use this : "$user_id = Auth::user()->id;" it will work.

  • It does not work,"Trying to get property 'id' of non-object" – Kouassi DJE Jun 06 '19 at 13:45
  • My apologies for this time out, I was a little unwell. I managed to register my user even when I do dd (Auth :: user ()) it returns me a good result. But I have another concern to connect an existing user. I have one field and I want him to connect with the phone number he used for his registration. – Kouassi DJE Jun 14 '19 at 14:07
  • public function authentification(Request $request) { if($request->isMethod('post')) { $data = $request->all(); if (Auth::attempt(['phone' => $data['phone']])) { return redirect('/accueil'); } else { return redirect()->back()->with('error','Veuillez vous inscrire!'); } } } – Kouassi DJE Jun 14 '19 at 14:08