Laravel Livewire: Validation in array of object - laravel

I am new in laravel livewire and having a hard time doing a validation array of object.
In the application, there is an item section and the user can add item/s.
Now all the fields in the item section are required. Once the user submitted form, it did not show the error message in the item section
View
#foreach($items as $i => $item)
<tr>
<td>
<!-- <select class="form-select #error('item_selected_service.{{ $i }}') is-invalid #enderror" type="text" id="service-id" placeholder="Enter member" wire:model.defer="member_id" autofocus> -->
<select class="form-select #error('selected_service_id') is-invalid #enderror" type="text" name="selected_service_id" id="service-id" placeholder="Select Services" wire:model.defer="selected_service_id" autofocus>
<option value="">Please Select</option>
#foreach ($fees as $fee)
<option value="{{ $fee->id }}">{{ $fee->code }} - {{ $fee->name }}</option>
#endforeach
</select>
#error('selected_service_id') <div class="invalid-feedback">{{ $message }}</div> #enderror
</td>
</tr>
#endforeach
Controller
public $members;
public $fees;
public $member_id;
public $date_delivered;
public $terms;
public $date_due;
public $items = [];
public function render()
{
return view('livewire.fees.billings.billing-create');
}
public function mount()
{
$this->members = Member::where('company_id', Auth::user()->company_id)->where('status_id',22)->get();
$this->fees = Fee::where('company_id', Auth::user()->company_id)->where('status_id',20)->get();
$items = new \stdClass();
$items->selected_service_id = '';
$items->quantity = 0;
$items->amount = 0;
array_push($this->items, $items);
}
public function store()
{
$this->validate([
'member_id' => 'required',
'date_delivered' => 'required',
'terms' => 'required|numeric',
'date_due' => 'required',
'items.selected_service_id' => 'required',
]);
}
Question: How to display error message in array of object?

add like this
$this->validate([
'member_id' => 'required',
'date_delivered' => 'required',
'terms' => 'required|numeric',
'date_due' => 'required',
'items.selected_service_id' => 'required',
'member_id.required' => 'Member id is required' //Your Error message
]);

Related

how to convert string data(from input form) to integer data(to db)? Laravel 8

I am a newbie in laravel. I have a little problem. I am confused how to convert form input data to db. I want when filling out the form input field it's in the form of a STRING, but I want the db to receive data as an integer.
What should I do?
In my blade :
<label class="form-label mb-3"><b>Category</b></label>
<input name="category_id" class="form-control #error('category_id') is-invalid #enderror" list="datalistOptions" id="category_id" placeholder="Ketik untuk mencari jenis kategori pertanyaan">
<datalist id="datalistOptions">
#foreach ($categories as $category)
<option value="{{ $category->id }}" {{ old('category_id') == $category->name ? 'selected' : null }}>{{ $category->name }}</option>
#endforeach
</datalist>
#error('category_id')
<div class="invalid-feedback">{{ $message }}</div>
#enderror
In my controller
$validated = $request->validate([
'title' => 'required|string',
'category_id' => 'required|nullable',
'question_detail' => 'required|string',
]);
Please help
form data is always passed as a string.
You need to convert the variable in the backend.
Description: https://www.tutorialkart.com/php/php-convert-string-to-int/
Have you tried using the validation numeric?
$validated = $request->validate([
'title' => 'required|string',
'category_id' => 'required|nullable',
'question_detail' => 'required|string',
'your_number_field' => 'required|numeric'
]);
https://laravel.com/docs/9.x/validation#rule-numeric
You may need to pair this with a numeric form field, on the input, like so: type="number"
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number

laravel 9 has defferent location then i set, and every file i put become .tmp

i dont understand why, i do it in laravel 8 and it work but not in laravel 9
Store function
public function store(Request $request)
{
$ValidatedData = $request->validate([
'title' => 'required|max:255',
'slug' => 'required|unique:menus',
'categories_id' => 'required',
'deskripsi' => 'required',
'is_order' => 'required',
'most' => 'required',
'price' => 'required',
'image' => 'image|file|max:1024'
]);
if($request->file('image')){
$validatedData['image'] = $request->image->store('menus');
}
$ValidatedData['users_id'] = Auth::user()->id;
$ValidatedData['excerpt'] = Str::limit(strip_tags($request->deskripsi), 100);
Menus::create($ValidatedData);
return Redirect('/dashboard/menus')->with('success', 'Tugas Baru Telah Ditambahkan!');
}
Form
<div class="mb-3">
<label for="image" class="form-label">Size Max. 1mb</label>
<input class="form-control #error('image') is-invalid #enderror" type="file"
name="image" id="image">
#error('image')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
.env
FILESYSTEM_DISK=public
After Create the storage location is change

Form didn't insert data to database properly [Laravel][Eloquent]

I have problem with my code somewhere that make my data $request just didn't passed to my database table (?), I'm not sure what the problem is but every time I try to submit it just redirect back to my create blade view.But when I debug it using dd($request->all()); it have everything it need.
My table have 5 columns, id, book_id, member_id, user_id, borrow_date, return_date
My Model
protected $table = "borrow";
protected $guarded = [];
public $timestamps = false;
// Relationship Book
public function book()
{
return $this->belongsTo('App\Book');
}
// Relationship Member
public function member()
{
return $this->belongsTo('App\Member');
}
My Create Controller
public function create()
{
$book= Book::all();
$member= Member::all();
return view('borrow.create', compact('book', 'member'));
}
public function store(Request $request)
{
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'user_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
'status' => 'required'
]);
Borrow::create([
'book_id' => $request->book_id,
'member_id' => $request->member_id,
'user_id' => Auth::user()->id,
'borrow_date' => $request->borrow_date,
'return_date' => $request->return_date,
'status' => 'borrowed',
]); return redirect('/borrow');
}
My Create View
<form action="/borrow" method="POST">
#csrf
<div class="form-group row">
<label class="col-sm-2 col-form-label">Book</label>
<div class="col-sm-10">
<select data-placeholder="Enter Book Data"
data-allow-clear="1" name="book_id" id="book_id">
<option></option>
#foreach($book as $value)
<option value="{{ $value->id }}">ISBN {{ $value->isbn }} -
{{ $value->title }} ({{ $value->year }})
</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Member</label>
<div class="col-sm-10">
<select data-placeholder="Enter Member Data"
data-allow-clear="1" name="member_id" id="member_id">
<option></option>
#foreach($member as $value)
<option value="{{ $value->id }}">{{ $value->name }}
#if ($value->gender == 'man')
(M) -
#else
(W) -
#endif
{{ $value->phone }}
</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Borrow Date</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="borrow_date"
id="borrow_date">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Return Date</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="return_date"
id="return_date">
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
dd($request->all());
array:5 [▼
"_token" => "pN3PPQGpT4jmLln59tY3HBiLj27fWgf65ioIYlv0"
"book_id" => "99"
"member_id" => "99"
"borrow_date" => "2021-09-01"
"return_date" => "2021-09-30"
]
Thanks! Sorry if my English and explanation is bad
You are trying to validate a user_id and a status presents in your $request but of course it doesn't.
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'user_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
'status' => 'required'
]);
You are using Auth::user()->id as user_id and it isn't in $request
So, just remove 'user_id' => 'required', from validation. You also don't have status in your $request so you need to remove it too. It should be like this;
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
]);
Use fillable in Peminjaman model
protected $fillable = [
'id', 'book_id', 'member_id', 'user_id', 'borrow_date', 'return_date'
];
try to remove user_id and status from the validation, the request doesn't have these parameters, and you are validating them as required values.
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
]);
When using the create() method, you are using what is called massive assignment. As per docs https://laravel.com/docs/8.x/eloquent#mass-assignment:
...before using the create method, you will need to specify either a
fillable or guarded property on your model class. These properties are
required because all Eloquent models are protected against mass
assignment vulnerabilities by default.
Saying that, you have 2 options:
1 - Keep using create() method but define fillable property in your model
protected $fillable = ['id', 'book_id', 'member_id', 'user_id', 'borrow_date', 'return_date'];
2 - Use the save() method with not need to define fillable property:
$borrow = new Borrow();
$borrow->book_id = $request->book_id;
$borrow->member_id = $request->member_id;
$borrow->user_id = Auth::user()->id;
$borrow->borrow_date = $request->borrow_date;
$borrow->return_date = $request->return_date;
$borrow->status = 'borrowed';
$borrow->save();

Livewire - I only can Add records to the pivot table - Through checkboxes

I am only able to add records to the pivot table. Really strugling on removing an entry from the pivot table
Livewire Component
<?php
namespace App\Http\Livewire;
use App\Models\User;
use App\Models\Company;
use Livewire\Component;
use Livewire\WithPagination;
class UserAssignCompanySection extends Component
{
use WithPagination;
public $sortBy = 'id';
public $sortAsc = false;
public $user;
public $companies;
public $search;
public $confirmingCompanyRemoval = false;
public $userCompanies = [];
protected $rules = [
'user.name' => 'required',
'user.email' => 'required',
'user.title' => 'required',
'user.first_name' => 'required',
'user.last_name' => 'nullable',
'user.mobile' => 'nullable',
'user.phone' => 'nullable',
'user.is_customer' => 'boolean',
'user.is_provider' => 'boolean',
'userCompanies' => 'required|array',
];
// Table Sort
public function sortBy($field)
{
if($field == $this->sortBy){
$this->sortAsc = !$this->sortAsc;
}
$this->sortBy = $field;
}
// public function updatedUserCompanies()
// {
// $var1 = array_diff(Company::pluck('id')->toArray(),$this->userCompanies);
// dd(Company::pluck('id')->toArray(), $this->userCompanies);
// dd($var1);
// $this->user->companies()->sync($this->userCompanies);
// }
public function updatedUserCompanies()
{
//dd($this->userCompanies);
$this->user->companies()->sync($this->userCompanies);
}
public function mount(User $user)
{
$this->user = $user;
$this->companies = Company::all();
$this->userCompanies = $user->companies()->pluck('company_id')->toArray();
//dd($this->userCompanies);
}
public function render()
{
//$this->user->companies()->sync($this->userCompanies);
return view('livewire.user-assign-company-section');
}
}
<div>
<div class="grid grid-cols-1 text-left xl:grid-cols-6">
<div class="col-span-3 p-4">
<p class="text-lg underline">Name</p>
<x-jet-input wire:model.defer="user.name" id="name" type="text" disabled class="block w-full mt-1 bg-gray-100" />
<x-jet-input-error for="user.name" class="mt-2" />
<br>
#json($userCompanies);
{{-- #foreach ($userCompanies as $item)
{{ $item }}
#endforeach --}}
#foreach($companies as $company)
<div class="mt-1">
<input wire:model="userCompanies" type="checkbox" value="{{ $company->id }}">
{{ $company->name }} <br>
</div>
#endforeach
<br>
{{-- <x-jet-button>Back</x-jet-button> --}}
<br>
</div>
</div>
</div>
When i click on the checbox with company_id = 3 a sting gets added to $this->userCompanies livewire property
something like this
[1,2,"3"];
When i uncheck ids such as 1 and 2 (which was retreived through the DB won't get deleted)
I think it tries to delete a string such as "1" from an array based on integers
If property is array then model have to be declared as array (with dot notation). Try some like this:
#foreach($companies as $i => $company)
<div class="mt-1">
<input wire:model="userCompanies.$i" type="checkbox" value="{{$company->id }}">
{{ $company->name }} <br>
</div>
#endforeach

Total, Quantity, Price in Laravel

I have a table named 'products' with 5 fields (id, title, price, quantity, total).
My goal is to calculate via the form products.create the total, price * quantity.
Database - products
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->integer('quantity');
$table->double('price');
$table->double('total')->nullable();
$table->timestamps();
});
}
Model - product
protected $fillable = ['title', 'quantity', 'price', 'total'];
public function setTotalAttribute()
{
$this->total = $this->quantity * $this->price;
}
public function getTotalAttribute($value)
{
return $value;
}
** Controller - ProductController**
public function index()
{
$products = Product::oldest()->paginate(5);
return view('admin.products.index', compact('products'))
->with('i', (request()->input('page', 1)-1)*5);
}
public function create()
{
$products = Product::all();
return view('admin.products.create', compact('products'));
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'quantity' => 'required',
'price' => 'required',
'total' => 'required'
]);
Product::create($request->all());
return redirect()->route('products.index')
->with('success', 'save');
}
My problem is in my view "products.create" I have 3 fields when I encode the 3 fields, nothing happens ???
Products.create
<form class="panel-body" action="{{route('products.store')}}" method="POST" novalidate>
#csrf
<fieldset class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
<label for="form-group-input-1">Title</label>
<input type="text" name="title" id="title" class="form-control" value="{{ old('title')}}"/>
{!! $errors->first('title', '<span class="help-block">:message</span>') !!}
</fieldset>
<fieldset class="form-group {{ $errors->has('quantity') ? 'has-error' : '' }}">
<label for="form-group-input-1">Quantity</label>
<input type="text" name="quantity" id="quantity" class="form-control" value="{{ old('quantity')}}"/>
{!! $errors->first('quantity', '<span class="help-block">:message</span>') !!}
</fieldset>
<fieldset class="form-group {{ $errors->has('price') ? 'has-error' : '' }}">
<label for="form-group-input-1">Price</label>
<input type="text" name="price" id="price" class="form-control" value="{{ old('price')}}"/>
{!! $errors->first('price', '<span class="help-block">:message</span>') !!}
</fieldset>
Back
<button type="submit" class="btn btn-sm btn-primary">Valider</button>
Thank you for help.
First of all you didn't send any total value...
$request->validate([
'title' => 'required',
'quantity' => 'required',
'price' => 'required',
]);
Just follow Eloquent ORM
$product = New Product();
$product-title = $request->title;
$product-quantity = $request->quantity;
$product-price = $request->price;
$product-total = $request->price * $request* quantity;
$product->save();
//redirect()
The mutator will receive the value that is being set on the attribute, so your mutator method should be like this on you Product model
public function setTotalAttribute()
{
$this->attributes['total'] = $this->quantity * $this->price;
}

Resources