I like to add a mutator that changes any comma to a dot in a text input posted from a form.
However, I don't understand how to call the element with the key of "weight_can"
I try to achieve it in a mutator in my model called "setWeigtCanAttribute" that should handle $weigt_can from my form.
The result is I get a failed validation error (The weight can must be a number.) of "weight_can" when I pass in e.g. "4,2" hoping that the mutator will turn it into "4,2"
Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = ['title_must', 'body', 'age_can', 'age_must', 'weight_can'];
public $timestamps = false;
public function setWeigtCanAttribute($weight_can){
$request->post['weight_can'] = str_replace(',', '.', $request->post['weight_can']);
}
}
PostRequest:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = ['title_must', 'body', 'age_can', 'age_must', 'weight_can'];
public $timestamps = false;
public function setWeigtCanAttribute($weight_can){
$request->post['weight_can'] = str_replace(',', '.', $request->post['weight_can']);
}
}
Controller:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\PostRequest;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
public function create()
{
return view('posts.form')->withPost(new Post());
}
/**
* Store a new post.
*
* @param \App\Http\Requests\PostRequest $request
* @return Illuminate\Http\Response
*/
public function store(PostRequest $request) // Inject the PostRequest
{
// return $request->all();
// return $request->post('weight_can');
Post::create($request->validated());
return redirect('/home')->with('success', 'Post created successfully!');
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
public function edit(Post $post)
{
return view('posts.form', compact('post'));
}
public function update(PostRequest $request, Post $post) // Inject the PostRequest and the Post (found with Laravel "id convention")
{
// return $request->all();
$post->update($request->validated());
return redirect('/home')->with('success', 'Post updated successfully!');
}
public function destroy(Post $post)
{
$post->delete();
return redirect('/home')->with('success', 'Post deleted successfully!');
}
}
View:
@extends('layouts.app')
@section('content')
<?php #dd($post);?>
<div class="container">
<div class="row justify-content-center mt-5">
<div class="col-6">
@if (session('status'))
<div class="alert alert-success mb-3" role="alert">
{{ session('status') }}
</div>
@endif
</div>
</div>
<div class="row justify-content-center">
<div class="col-4">
@isset($post->id)
<h4>Edit Item</h4>
@else
<h4>New Item</h4>
@endif
</div>
<div class="col-2">
<a class="btn btn-outline-secondary float-right" href="/home" role="button">Cancel</a>
</div>
</div>
<div class="row justify-content-center">
<div class="col-6">
@isset($post->id)
<form method="post" action="/post/{{ $post->id }}">
@method('PUT')
@else
<form method="post" action="/post">
@endif
@csrf
<div class="form-group">
<label for="title">Title must</label>
<input type="text" class="form-control" name="title_must" value="{{ old('title_must', $post->title_must) }}"
id="title_must">
@error('title_must')<span class="text-danger">{{ $message }}</span>@enderror
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" id="body"
rows="3">{{ old('body', $post->body) }}</textarea>
@error('body')<span class="text-danger">{{ $message }}</span>@enderror
</div>
<div class="form-group">
<label for="title">Age can</label>
<input type="text" class="form-control" name="age_can" value="{{ old('age_can', $post->age_can) }}"
id="age_can">
@error('age_can')<span class="text-danger">{{ $message }}</span>@enderror
</div>
<div class="form-group">
<label for="title">Age must</label>
<input type="text" class="form-control" name="age_must" value="{{ old('age_must', $post->age_must) }}"
id="age_must">
@error('age_must')<span class="text-danger">{{ $message }}</span>@enderror
</div>
<div class="form-group">
<label for="title">Weight can</label>
<input type="text" class="form-control" name="weight_can" value="{{ old('weight_can', $post->weight_can) }}"
id="weight_can">
@error('weight_can')<span class="text-danger">{{ $message }}</span>@enderror
</div>
<div class="mt-3">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
@endsection