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();
Related
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
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
]);
I’m working on category and sub_catengory with laravel select menu related to shopping site using Mass Assignment, I’m having trouble to retrieve from the database from the select menu. I'm getting error said
Undefined variable: categories_dropdown (View:
add_product.blade.php file.
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label for="Category" class="col-sm-3 text-right control-label col-form-label font-
weight-bold">Category: <span class="red_star">∗</span></label>
<div class="col-sm-9">
<select name="category" id="category" class="custom-select">
<?php echo $categories_dropdown; ?>
</select>
</div>
</div>
</div>
ProductController.php
public function store(){
$data = request()->validate([
'sku' => 'required',
'product_name' => 'required',
'description' => 'required',
'brand' => 'required',
'category_id' => 'required',
'sub_categories' => 'required',
'size' => '',
'status' => '',
'product_code' => '',
'care' => '',
]);
Product::create($data);
$categories = Category::where(['parent_id'=>0])->get();
$categories_dropdown = "<option value='' selected disabled>Select</option>";
foreach($categories as $cat){
$categories_dropdown .= "<option value='".$cat->id."'>".$cat->name."</option>";
$sub_categories = Category::where(['parent_id'=>$cat->id])->get();
foreach ($sub_categories as $sub_cat) {
$categories_dropdown .= "<option value = '".$sub_cat->id."'> -- ".$sub_cat->name."</option>";
}
}
//Categories drop down end
return view('admin.products.add_product')->with(compact('categories_dropdown'));
}
product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $guarded = [];
}
what did I missed it!
many thanks.
You have to change this
return view('admin.products.add_product',compact('categories_dropdown'));
I am always using this when I pass data to view:
...
return view('admin.products.add_product')->with([
'categories_dropdown' => $categories_dropdown
]);
I think my question is wrong and I will clarify the whole topic here.
i got 2 buttons in 1 form and I've already searched about this topic.
So here it is:
I have 2 buttons the other button will go to this and the other will go to
public function store(Request $request)
{
request()->validate([
'name' => 'required',
'note' => 'required',
'ingredientid' => 'required',
]);
Recipe::create($request->all());
return redirect()->route('recipe.index')
->with('success','Recipe added to the list successfully.');
}
public function addviewing()
{
request()->validate([
'id' => 'required',
'name' => 'required',
]);
Viewingredient::create([
'id' => $request->id,
'name' => $request->name,
]);
return redirect()->route('recipe.form')
->with('success','Ingredient Added Successfully!');
}
And this is the two buttons:
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Add Ingredient</button>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
but when i click the add ingredient it does nothing.
I am trying to ask gender of the user in registration form through dropdown list but I am unable to get that value in controller.
Here is my code to register.blade.php
<!-- Gender -->
<div class="form-group">
<label for="gender" class="col-md-4 control-label">Gender</label>
<div class="col-md-6">
<select class="form-control" required="required">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
</div>
Code of register controller
protected function validator(array $data)
{
return Validator::make($data, [
'username' => 'required|string|max:255',
'fullname' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'dob' => 'required|date',
'gender' => 'required|string',
// 'gender' => 'in:male,female'
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
dd($data);
return User::create([
'username' => $data['username'],
'fullname' => $data['fullname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'dob' => $data['dob'],
'gender' => $data['gender'],
]);
}
user model contains
protected $fillable = [
'username', 'email', 'password','fullname','dob','gender'
];
and the output shown when line 'gender' => 'required|string', is commented in validator function of register controller. Nothing happens when this line is uncommented
Write attribute name="gender" in the selectelement.
Without name attribute the data is not passed via GET/POST request.
Name for field was missed.
<select class="form-control" name="gender" required="required">
<option value="male">Male</option>
<option value="female">Female</option>
</select>