If priority column is not nullable then supply default value for it.
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'priority' at row 1
when i'm trying to store data with enum columns i got this error
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'priority' at row 1
my store method:
public function store(TicketRequest $request)
{
$validatedData = $request->validated();
$validatedData = array_merge($validatedData, [
'slug' => make_slug($request->title) . '-' . random_int(0, 12300),
'ticket_unique_id' => Str::random(20),
'user_id' => auth()->id(),
]);
$ticketCreate = $this->ticket->create($validatedData);
if ($ticketCreate) {
return redirect(route('tickets.index'));
}
}
any suggestion?
@ramjithap There is a default value already
$table->enum('priority', ['normal', 'medium', 'high'])->default('normal');
Go to your config/database.php change MySQL strict to false.
Show us the migration for the table.
It looks to me that the value you are trying to insert into priority is longer than the type specified, hence the truncation.
We can't see what you actually submit or how you validate the request?
priority is not mentioned anywhere in the code you show, so I'm not really sure why you posted it?
You are also not actually using $validatedData, instead you just store $request->validated() so how closely have you checked your code before posting the question?
sloppy solution
@snapey Agreed.
@snapey yeah you right about the stored data but that all what i got:
store method:
public function store(TicketRequest $request)
{
$validatedData = $request->validated();
$validatedData = array_merge($validatedData, [
'slug' => make_slug($request->title) . '-' . random_int(0, 12300),
'ticket_unique_id' => Str::random(20),
'user_id' => auth()->id(),
]);
$ticketCreate = $this->ticket->create($validatedData);
if ($ticketCreate) {
return redirect(route('user.tickets.index', auth()->id()));
}
}
Ticket Request
public function rules()
{
return [
'title' => ['required', 'min:3'],
'description' => ['required', 'min:10'],
'priority' => ['required', 'in:normal,meduim,high'],
'ticket_unique_id' => 'string',
'user_id' => 'integer',
'slug' => '',
'department_id' => 'required|integer',
];
}
view create form
<form action="{{route('user.tickets.store', auth()->user())}}"
method="post"
class="form bg-blue-100 rounded-md p-6 my-10 relative mx-auto w-2/3 shadow-md">
@csrf
<h3 class="text-2xl text-gray-900 font-semibold">{{__('frontend.user.' . $status)}}</h3>
<p class="text-gray-600 mb-5">{{__('frontend.user.tickets.form.open_ticket_details' )}}</p>
<hr>
<div class=" mt-5">
<label class="block uppercase tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-first-name">
{{__('frontend.user.tickets.form.title')}}
</label>
<input type="text" name="title"
placeholder="{{__('frontend.user.tickets.form.title_placeholder')}}"
class="border p-2 w-full shadow-md"
value="{{old('title')}}">
@error('title')
<p class="text-red-700 font-semibold mt-2">
{{$message}}
</p>
@enderror
</div>
<div class=" mt-5">
<label class="block uppercase tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-first-name">
{{__('frontend.user.tickets.form.priority')}}
</label>
<select class="flex-1 h-10 mt-2 form-select w-full shadow-md" name="priority">
<option value="normal">{{__('frontend.user.tickets.form.priorities.normal')}}</option>
<option value="meduim">{{__('frontend.user.tickets.form.priorities.meduim')}}</option>
<option value="low">{{__('frontend.user.tickets.form.priorities.high')}}</option>
</select>
@error('priority')
<p class="text-red-700 font-semibold mt-2">
{{$message}}
</p>
@enderror
</div>
<div class=" mt-5">
<label class="block uppercase tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-first-name">
{{__('frontend.user.tickets.form.department')}}
</label>
<select class="flex-1 h-10 mt-2 form-select w-full shadow-md" name="department_id">
@foreach($departments as $department)
<option value="{{$department->id}}">{{$department->name}}</option>
@endforeach
</select>
@error('department_id')
<p class="text-red-700 font-semibold mt-2">
{{$message}}
</p>
@enderror
</div>
<div class=" mt-5">
<label class="block uppercase tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-first-name">
{{__('frontend.user.tickets.form.description')}}
</label>
<textarea name="description" id=""
cols="10"
rows="6"
placeholder="{{__('frontend.user.tickets.form.description_details')}}"
class="border p-2 mt-3 w-full shadow-md rounded-sm"
value="{{old('description')}}"></textarea>
@error('description')
<p class="text-red-700 font-semibold mt-2">
{{$message}}
</p>
@enderror
</div>
<div class="mt-5 mx-auto text-center">
<button
type="submit"
class="text-sm focus:outline-none
justify-center px-4 py-2 rounded font-bold cursor-pointer
hover:bg-teal-700 hover:text-teal-100
bg-teal-100
text-teal-700
inline-block
w-full
border duration-200 ease-in-out
border-teal-600 transition">
{{__('frontend.user.tickets.form.submit')}}
</button>
</div>
</form>
any better solution @snapey i will be apreciated.
Does it matter which priority you choose when submitting?
By the way, you spelled medium incorrectly in both your form dropdown value and your validation rule
i do it just for security purposes
what does this mean?
@snapey just for increase the security , with specify the values in the priority input - another layer of validation (in my opinion of course).
Yes, you should do that. My question was about when you got the error. Did you get the error when selecting normal, medium or high
when selecting meduim
cause in migration i put:
$table->enum('priority', ['normal', 'medium', 'high'])->default('normal');
i refactoring my code like the following:
in Ticket Repository:
public function create($user, array $ticketData)
{
return $user->tickets()->create($ticketData);
}
store method:
public function store(User $user, TicketRequest $request)
{
$validatedData = $request->validated();
$validatedData = array_merge($validatedData, [
'slug' => make_slug($request->title) . '-' . random_int(0, 12300),
'ticket_unique_id' => Str::random(20),
]);
$ticketCreated = $this->ticket->create($user, $validatedData);
if (!$ticketCreated) {
return redirect(route('user.tickets.index', auth()->id()))->with('fails', '');
}
return redirect(route('user.tickets.index', auth()->id()))->with('success', '');
}
view:
.........
<div class=" mt-5">
<label class="block uppercase tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-first-name">
{{__('frontend.user.tickets.form.priority')}}
</label>
<select class="flex-1 h-10 mt-2 form-select w-full shadow-md" name="priority">
<option value="normal">{{__('frontend.user.tickets.form.priorities.normal')}}</option>
<option value="medium">{{__('frontend.user.tickets.form.priorities.medium')}}</option>
<option value="low">{{__('frontend.user.tickets.form.priorities.high')}}</option>
</select>
@error('priority')
<p class="text-red-700 font-semibold mt-2">
{{$message}}
</p>
@enderror
</div>
......
database.php
.....
'strict' => true,
.....
Please or to participate in this conversation.