Hassaan69's avatar

Updating laravel foreign keys updated_by in services table

I have created two tables that are users and services. Services table have two foreign keys as created_by and updated_by. Now when i try to update the field updated_by in services table it is not updating infact it is returning null value.

Thats the Services model specifying the relationship with users.

public function user(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); }

Thats index, store and update method

public function index() { $service = Service::with('user')->latest()->get(); return view('admin.service.index', compact('service')); }

Service::insert([ 'created_by' => auth()->user()->id, 'description' => $request->description 'title' => $request->title, 'slug' => strtolower(str_replace(' ', '-',$request->title)), 'created_at' => Carbon::now(), 'image' => $save_url, ]);

Update Method

Service::findOrFail($service_id)->update([ 'updated_by' => auth()->user()->id, 'description' => $request->description, 'title' => $request->title, 'slug' => strtolower(str_replace(' ', '-',$request->title)), 'updated_at' => Carbon::now(), 'image' => $save_url, ]);

Thats the table in Index.blade.php

0 likes
5 replies
nexxai's avatar

Is the updated_by field listed in the $fillable array in the Service model?

PovilasKorop's avatar

Another possibility is that you're performing the update as non logged in user, so auth()->user()->id is null, then.

Snapey's avatar

are the other columns updated correctly?

Please or to participate in this conversation.