Daniel Logvin
Daniel Logvin

Reputation: 502

How is it possible to retrieve the id of a Model right after it was created in the same controller function?

Let me show you my code, and place comments for you guys to better understand:

$homework = new Homework([ // I create Homework (And I indeed want to get the ID of the one that was just created).
    'subject_id' => $request->subject_id,
    'user_id' => auth()->user()->id,
    'title' => $request->name,
    'image' => $path,
    'progress' => $request->progress,
    'description' => $request->description,
    'duedate' => $request->date
]);

$homework->save(); // I save it

$homeworkid = Homework::where('id', $id)->first(); // I try to retrieve it, but I'm not sure how to get it as I need to define `$id`.

$progress = newProgress([
    'user_id' => auth()->user()->id,
    'homework_id' => $homeworkid, // I need this for the relationship to work.
    'title' => 'Initial Progress',
    'description' => 'This progress is auto-generated when you create an assignment',
    'username' => auth()->user()->name,
    'progress' => $homeworkid->progress
]);

$progress->save(); // I save the progress

Well, as you saw, I'm trying to retrieve the ID of Homework right after it was created, but I'm not sure how to define $id in order to get it.

Upvotes: 1

Views: 57

Answers (3)

Remul
Remul

Reputation: 8252

There is no need to instantiate a new model and saving it if your are not doing anything between instantiating and saving, you can use the create method instead:

$homework = Homework::create([
    'subject_id' => $request->subject_id,
    'user_id' => auth()->user()->id,
    'title' => $request->name,
    'image' => $path,
    'progress' => $request->progress,
    'description' => $request->description,
    'duedate' => $request->date
]);

$homework->id; // get the id

After saving / creating the model you can access the id like you normally would:

$homework->id

What you then could do is setup the relationships between your models so you can do the following after creating a new homework:

$homework->newProgress()->create([
    'user_id' => auth()->user()->id,
    'title' => 'Initial Progress',
    'description' => 'This progress is auto-generated when you create an assignment',
    'username' => auth()->user()->name,
    'progress' => $homework->progress
]);

This way you don't have to pass the homework id when creating a new newProgress, laravel will pass it automatically for you.

Upvotes: 2

A.A Noman
A.A Noman

Reputation: 5270

This is very simple for you. No need to complex it.

$homework->save(); // I save it

After this line just use only

$progress = newProgress([
    'user_id' => auth()->user()->id,
    'homework_id' => $homework->id, // I need this for the relationship to work.
    'title' => 'Initial Progress',
    'description' => 'This progress is auto-generated when you create an assignment',
    'username' => auth()->user()->name,
    'progress' => $homework->progress
]);

You don't no need this line of code

$homeworkid = Homework::where('id', $id)->first(); // I try to retrieve it, but I'm not sure how to get it as I need to define `$id`.

Upvotes: 1

Hasnain
Hasnain

Reputation: 539

$data = $homework->save();

Get the ID this way: $data->id

Upvotes: 0

Related Questions