After you call ->save();
You can do
$id = $activity->id;
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]);
}
}
After you call ->save();
You can do
$id = $activity->id;
Not that again..
@chrisgo looks like @irankhosravi is already doing that in their return response()
$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
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()
@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'
@Snapey I admire your persistence.
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;
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.
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.
I am very involved / please help me.
perhaps get smart wizard
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:
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.
I get this error
Undefined property: Illuminate\Database\Eloquent\Builder::$id
$school_id = School::latest()->id->get();
$activity->school_id = $school_id;
@mozew Please use this one $school_id = School::latest()->first()->id; $activity->school_id = $school_id;
Because id is table field not Eloquent function.
@ramkumawat NO this is not safe practice
why are you sending request back again. You can send the saved object which contains activity id also.
return response()->json(['data_activity' =>$activity]);
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]);
Why do you keep using invalid code when I have already given you the answer?
And that invalid code has literally been pointed out to you of EXACTLY what you did wrong.
Please or to participate in this conversation.