Undefined variable categories_dropdown in laravel - laravel

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
]);

Related

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();

laravelcollective select option in laravel

How to show value select option when install laravelcollective /html in laravel?
blade
<div class="row">
<div class="col-md-6 form-group">
<label for="target">Categoory</label>
{!! Form::select('categories', $categories, null, [
'class' => 'form-control',
'id' => 'target',
]) !!}
</div>
</div>
controller
public function index()
{
$categories = Category::all();
return view('user::admin.users.index', compact('categories'));
}
demo
You should print your categories like this:
{!! Form::select('categories', $categories->pluck('name', 'id'), null, [
'class' => 'form-control',
'id' => 'target',
]) !!}

The image failed to upload.on a server laravel

The image failed to upload.
link
https://comedoruniversitariouncp.000webhostapp.com/products/create
The project works in local server,
the error appears when i upload to a server
create.blade.php
<form action="{{ route('products.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<label class="col-form-label col-sm-2">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name">
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Price</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="price" step="0.1">
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Amount</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="amount" >
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Image</label>
<div class="col-sm-10">
<input type="file" class="form-control-file" name="image">
</div>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
ProductController.php
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'price' => 'required',
'amount' => 'required',
'image' => 'required|image'
]);
$image = $request->file('image');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
Product::create([
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount,
'image' => $new_name
]);
return redirect()->route('products.index')->with('message', 'Product created successfully');
}
As you mention about works on local but not remote. I assumed that the upload_max_filesize is greater than the size your upload file, and both on local and remote are not the same.
You may use The Storage Facade as a convenient way to interact with your local filesystems.
use Illuminate\Support\Facades\Storage;
//...
$new_name = rand() . '.' . $image->getClientOriginalExtension();
Storage::disk('public')->putFileAs('images', request->file('image'), $new_name);
//...
Docs
You should try this
Try with adding mimetypes in validation of image.
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'price' => 'required',
'amount' => 'required',
'image' => 'required|mimes:jpeg,bmp,png'
]);
$image = $request->file('image');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
Product::create([
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount,
'image' => $new_name
]);
return redirect()->route('products.index')->with('message', 'Product created successfully');
}
You Should Try code this,
you may change a part code :
$image->move(public_path('images'), $new_name);
to be code :
$image->move(public_path('images'.$new_name));
this is code, 100% work to me.

Call to a member function getClientOriginalName() on null upload file PDF

i want to upload pdf FILE with laravel,,, in my controller :
public function store(Request $request)
{
$categories = Category::findOrFail($request->category_id);
$request->request->add(['code' => $categories->aliases]);
$request->validate([
'code' => 'string',
'pdf' => 'required',
'eur' => 'required|numeric',
'date' => 'required'
]);
`
enter code here`$rawInput = $request->except('image');
$priceInput = $request->only(['idr', 'usd', 'eur', 'date']);
$pdf = $request->file('pdf')->getClientOriginalName();
in my blade...
<div class="form-group row">
<label class="col-md-4 col-form-label text-md-right">PDF</label>
<div class="col-md-6">
<input type="file" name="pdf" class="form-control{{ $errors->has('pdf') ? ' is-invalid' : '' }}">
#if ($errors->has('pdf'))
<span class="invalid-feedback">
<strong>{{ $errors->first('pdf') }}</strong>
</span>
#endif
</div>
</div>
when i click upload,,, error like this
i am getting error undefined varibale $pdf.. $pdf variable i PUT and compact in
return view('inventory::raws.show', compact(['raw', 'pdf']));
..
whats wrong my code........
use
$pdf = $request->file('pdf')->getClientOriginalName();

form validation with array not working

On my form I have some check boxes. And they are in an array. When I submit my form for some reason it clears the Modify check boxes. And then when go back to look not checked.
I have know what's causing issue is $this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit')
It does not seem to like, permission[modify] or permission[modify][] on the set_rules
How am I able to solve this?
Controller Edit Function:
public function edit() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'User Group Name', 'required');
$this->form_validation->set_rules('user_group_id', 'User Group Id', 'required');
$this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit');
if ($this->form_validation->run($this) == FALSE) {
$this->getForm();
} else {
$this->load->model('admin/user/model_user_group');
$this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post());
$this->db->select('permission');
$this->db->from($this->db->dbprefix . 'user_group');
$this->db->where('user_group_id', $this->session->userdata('user_group_id'));
$user_group_query = $this->db->get();
$permissions = unserialize($user_group_query->row('permission'));
$this->session->set_userdata($permissions);
$this->session->set_flashdata('success', 'Congratulations you have successfully modified' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
redirect('admin/users_group');
}
}
public function modify_check_edit() {
if (!in_array('users_group', $this->session->userdata('modify'))) {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to edit' );
}
}
View Form:
<?php echo validation_errors('<div class="alert alert-warning text-center"><i class="fa fa-exclamation-triangle"></i>
', '</div>'); ?>
<?php if ($this->uri->segment(4) == FALSE) { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'users_group');?>
<?php echo form_open('admin/users_group/add', $data);?>
<?php } else { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'users_group');?>
<?php echo form_open('admin/users_group/edit' .'/'. $this->uri->segment(4), $data);?>
<?php } ?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('User Group Name', 'name', $data);?>
<div class="col-sm-10">
<?php
$data_user_name = array(
'id' => 'name',
'name' => 'name',
'class' => 'form-control',
'value' => $name
)
;?>
<?php echo form_input($data_user_name);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('User Group Id', 'user_group_id', $data);?>
<div class="col-sm-10">
<?php
$data_user_group_id = array(
'id' => 'user_group_id',
'name' => 'user_group_id',
'class' => 'form-control',
'value' => $user_group_id
)
;?>
<?php echo form_input($data_user_group_id);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Access Permission', 'permission_access', $data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $access)) { ?>
<?php
$data_checked = array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => TRUE,
);
echo form_checkbox($data_checked);
?>
<?php echo $permission; ?>
<?php } else { ?>
<?php
$data_not_checked = array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => FALSE,
);
echo form_checkbox($data_not_checked);
?>
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Modify Permission', 'permission_modify', $data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $modify)) { ?>
<?php
$data = array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => TRUE,
);
echo form_checkbox($data);
?>
<?php echo $permission; ?>
<?php } else { ?>
<?php
$data = array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => FALSE,
);
echo form_checkbox($data);
?>
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>
<?php echo form_close();?>
After a good half hour trying to figure it out it was mostly part of the call back not to working with th set_rules() for permission[modify]
Just fixed code here for call back and seems to be working now.
public function modify_check_edit() {
$this->load->library('form_validation');
if (is_array($this->session->userdata('modify'))) {
$permission = !in_array('users_group', $this->session->userdata('modify'));
if ($permission) {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
} else {
return TRUE;
}
return TRUE;
} else {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
}
}

Resources