Reputation: 119
I want to calculate the percentage of two inputs which are total marks and obtained marks. When user fill these two inputs then in the percentage field i want to show the percentage automatically. I don't have the basic idea. Below is my controller
/** MyController.php */
public function store(ReportRequest $request)
{
$input = $request->all();
if ($file = $request->file('photo_id'))
{
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
Report::create($input);
return redirect()->back();
}
Upvotes: 1
Views: 1128
Reputation: 14281
You can create a computed Accessor attribute in your Request
model class:
/** Request.php */
public function getMarksPercentageAttribute
{
return (float) $this->obtained_marks / $this->total_marks;
}
So when you create your records.. you can use this attribute:
$request = Request:first();
dd($request->marks_percentage); // 0.35 for example
PS: I'm assuming that total_marks
and obtained_marks
are columns of your table. In case this aren't the correct names, replace this with the ones you actually use.
Note, after the creation of the element you are returning back.. you could redirect to a show
view for example. From the docs:
Redirecting To Named Routes
When you call the
redirect
helper with no parameters, an instance ofIlluminate\Routing\Redirector
is returned, allowing you to call any method on theRedirector
instance. For example, to generate aRedirectResponse
to a named route, you may use theroute
method:return redirect()->route('login');
If your route has parameters, you may pass them as the second argument to the route method:
// For a route with the following URI: profile/{id} return redirect()->route('profile', ['id' => 1]);
Upvotes: 1