Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

How to get last id in laravel?

I decided save latest id. How should I do?

public function store(Request $request)
{
    $school_id = School::latest()->id->get();
    if($request->ajax()) {
        $activity = new Activity();
        $activity->cluster_id = $request->cluster_id;
        $activity->group_id = $request->group_id;
        $activity->school_id = $school_id;
        $activity->save();

        return response()->json(['data_activity' => $request->all(), 'id' => $activity->id]);
    }
}
0 likes
19 replies
chrisgo's avatar

After you call ->save();

You can do

$id = $activity->id;
Snapey's avatar

$school_id = School::latest()->first()->id;

but this is REALLY bad since in a multi-user system, you don't know who created the last school.

It will probably work for your assignment

1 like
automica's avatar

To ensure you are acting on the current school, you should set the school_id as a hidden field in your form and then reference that within your store method. Alternatively, if your user has a relationship with a school then you can get this id via Auth::user()

Snapey's avatar

@brightstormhq nice try. We've been through all this on the OPs earlier question on the same subject. All we get is 'I want the last ID'

cmdobueno's avatar

read what you were told....

you skipped a part of the provided code... its just copy and paste... like really

From @Snapey

$school_id = School::latest()->first()->id;
1 like
Cronix's avatar

You should be passing school_id in the request anyway, not trying to get "last id" from the database. This is a really bad solution and you might not end up with the correct result. This is open to race conditions (2 people creating a new school at same time, which id is the correct one?)

Problem is you never actually explain what you're trying to do.

1 like
mozew's avatar
Level 6

We have many form or page with smart wizard. The first form or page or form that opens. It is the school. After filling out the school form We get the school ID, for example21. Then save it and click next open form activity with school_id 21.

How to get value `school_id` in value hidden.

I am very involved / please help me.

Help

jlrdw's avatar

Just have a search box and user enters the current school they want to see info on.

In a multi user system, 5 entries could be made about the same time.

Say as example you have these schools names:

  • Billy Bob truck school
  • Jims welding school
  • sues beauty school

Now have a dropdown or whatever, select school, click submit and have whatever information displayed for the school. No need to bring up data right after adding it.

As an example, in dropdown I select

Jims welding school

Then I click submit.

That brings up a page showing whatever I have programmed it to display.

As an example

This week we start vertical up with the 7018 rod.

Next week we will start practice on root pass.

See how easy.

mozew's avatar
Level 6

I get this error

Undefined property: Illuminate\Database\Eloquent\Builder::$id

$school_id = School::latest()->id->get();
$activity->school_id = $school_id;
ramkumawat's avatar

@mozew Please use this one $school_id = School::latest()->first()->id; $activity->school_id = $school_id;

Because id is table field not Eloquent function.

Saikishore's avatar

Hi @irankhosravi

why are you sending request back again. You can send the saved object which contains activity id also.

return response()->json(['data_activity' =>$activity]);

Saikishore's avatar

If you are feeling that multiple Users will submit activities at a time, and if you want send the latest activity then use the below after saving the activity.

$lastActivity = Activity::latest()->first();
return response()->json(['data_activity' =>$lastActivity]);
Snapey's avatar

Why do you keep using invalid code when I have already given you the answer?

1 like
cmdobueno's avatar

And that invalid code has literally been pointed out to you of EXACTLY what you did wrong.

Please or to participate in this conversation.