I was trying to use Str_slug but it is not working in laravel 5.8.
I am using PhpStorm latest version.
$product->slug = Str_slug('$request->title');
Check the image for more
public function ProductStore(StoreValidation $request){
$product = new product();
$product->category_id = 1;
$product->brand_id = 1;
$product->title = $request->title;
$product->desc = $request->desc;
$product->slug = Str_slug('$request->title');
$product->quantity = $request->quantity;
$product->price = $request->price;
$product->offer_price = $request->offer;
$product->status = 1;
$product->admin_id = 1;
// Saving Product information into product table
$product->save();
if ($request->hasFile('uploadFile')){
$image = $request->file('uploadFile');
$img = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('image/product/' .$img);
$img_ins = Image::make($image)->resize(220,294);
$img_ins->save($location);
$product_img = new product_image();
$product_img->product_id = $product->id;
$product_img->image_name = $img;
$product_img->save();
}
return redirect()->route('admin_panel.pages.admin-addProduct');
}
You should use Str::slug() (Illuminate\Support\Str) instead of deprecated str_slug().
https://laravel.com/docs/5.8/helpers#method-str-slug
Related
For some reason, I still get a warning message: Creating default object from empty value.
Product controller
public function update(Request $request, $id)
{
$products = new Product();
$products = Product::find($id);
if ($request->hasFile('image'))
{
$path = 'assets/uploads/products/'.$products->image;
if (File::exists($path))
{
File::delete($path);
}
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = time().'.'.$ext;
$file->move('assets/uploads/products/',$filename);
$products->image = $filename;
}
$products->name = $request->input('name');
$products->slug = $request->input('slug');
$products->small_description = $request->input('small_description');
$products->description = $request->input('description');
$products->origanl_price = $request->input('origanl_price');
$products->selling_price = $request->input('selling_price');
$products->qty = $request->input('qty');
$products->tax = $request->input('tax');
$products->status = $request->input('status') == TRUE? '1':'0';
$products->trending = $request->input('trending') == TRUE? '1':'0';
$products->meta_title = $request->input('meta_title');
$products->meta_keywords = $request->input('meta_keywords');
$products->meta_description = $request->input('meta_description');
$products->update();
return redirect('/products')
->with('success', 'Product updated Successfully');
}
When I update the product, the page returns an error.
Creating default object from empty value.
How can I properly initialize the new empty object?
Why is the foreach loop not working in the same method in Laravel if the condition has a file? But when I remove the if condition, then the foreach is working. Otherwise, only the file is saved.
public function store(Request $request)
{
$userBasic = new UserBasic();
$userBasic->userId = $request->userId;
$userBasic->phone = $request->phone;
$userBasic->fullName = $request->fullName;
$userBasic->dob = $request->dob;
$userBasic->country = $request->country;
if ($request->hasFile('profileImg')) {
$img = $request->file('profileImg');
$imgName = time() . rand() . '.' . $img->extension();
$img->move(public_path('assets/img'), $imgName);
$profileImg = 'assets/img/' . $imgName;
$userBasic->profileImg = $profileImg;
}
$userBasic->status = $request->has('status');
$userBasic->save();
foreach ($request->medias as $media ) {
$userSml = new UserSocialMediaLink();
$userSml->userId = $request->userId;
$userSml->mediaName = $media['name'];
$userSml->mediaLink = $media['link'];
$userSml->save();
}
}
On a controller store function, I have this code:
public function store(RegisterAccountsRequest $request){
if($request->installment > 1){
foreach(range(1, $request->installment) as $installment) {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name . " ( {$installment} / {$request->installment} )";
$data->value = ( $request->value / $request->installment );
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = $installment;
$data->due_date = $request->due_date;
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
} else {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name;
$data->value = $request->value;
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = 1;
$data->due_date = $request->due_date;
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
return redirect()->route('admin.financials.index')
->with('success', "Conta cadastrada com sucesso!", $data);
}
When $request->installment is bigger than 1, then runs the first part of the code e it should do some calculations which I'm not figuring out how to store each installment for each month, like:
due_date
________
30/10/2020
30/11/2020
30/12/2020
etc.
I hope I made myself clear enough.
Help, please!
I think you need something like this:
public function store(RegisterAccountsRequest $request){
if($request->installment > 1){
$dueDate = Carbon::createFromFormat('d/m/Y', $request->due_date);
foreach(range(1, $request->installment) as $installment) {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name . " ( {$installment} / {$request->installment} )";
$data->value = ( $request->value / $request->installment );
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = $installment;
$data->due_date = $dueDate->format('d/m/Y');
$dueDate->addMonth();
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
} else {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name;
$data->value = $request->value;
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = 1;
$data->due_date = $request->due_date;
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
return redirect()->route('admin.financials.index')
->with('success', "Conta cadastrada com sucesso!", $data);
}
In this case I make a Carbon(carbon docs) instance with date due_date.
After using the value like so $data->due_date = $dueDate->format('d/m/Y'); and then adding one month like so $dueDate->addMonth(); I have a new month available each iteration.
I deployed my Laravel-5.8 application on Digital Ocean which uses Ubuntu-18. When I wanted to save image into my Laravel App on Digital Ocean:
public function create()
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
$employees = DB::table('hr_employees')->select('id')->where('id', $userEmployee)->first();
// dd($employees);
$goaltypes = AppraisalGoalType::where('company_id', $userCompany)->get();
$categories = AppraisalGoalType::with('children')->where('company_id', $userCompany)->whereNull('parent_id')->get();
return view('appraisal.appraisal_goals.create')
->with('goaltypes', $goaltypes)
->with('categories', $categories)
->with('identities', $identities)
->with('employees', $employees)
;
}
public function store(StoreAppraisalGoalRequest $request)
{
$appraisalStartDate = Carbon::parse($request->appraisal_start_date);
$appraisalEndDate = Carbon::parse($request->appraisal_end_date);
$userCompany = Auth::user()->company_id;
$employeeId = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
$employees = DB::table('hr_employees')->select('id')->where('id', $employeeId)->first();
DB::beginTransaction();
try {
$goal = new AppraisalGoal();
$goal->goal_type_id = $request->goal_type_id;
$goal->appraisal_identity_id = $request->appraisal_identity_id;
$goal->employee_id = $request->employee_id; //$employeeId; //$request->employees_id
$goal->weighted_score = $request->weighted_score;
$goal->goal_title = $request->goal_title;
$goal->goal_description = $request->goal_description;
$goal->company_id = Auth::user()->company_id;
$goal->created_by = Auth::user()->id;
$goal->created_at = date("Y-m-d H:i:s");
$goal->is_active = 1;
if ($request->appraisal_doc != "") {
$appraisal_doc = $request->file('appraisal_doc');
$new_name = rand() . '.' . $appraisal_doc->getClientOriginalExtension();
$appraisal_doc->move(public_path('storage/documents/appraisal_goal'), $new_name);
$goal->appraisal_doc = $new_name;
}
$goal->save();
foreach ( $request->activity as $key => $activity){
$startDate = Carbon::parse($request->start_date[$key]);
$endDate = Carbon::parse($request->end_date[$key]);
$goaldetail = new AppraisalGoalDetail();
$goaldetail->kpi_description = $request->kpi_description[$key];
$goaldetail->appraisal_doc = $request->application_doc[$key];
$goaldetail->activity = $request->activity[$key];
$goaldetail->start_date = $startDate ->toDateTimeString();
$goaldetail->end_date = $endDate->toDateTimeString();
$goaldetail->appraisal_goal_id = $goal->id;
$goaldetail->appraisal_identity_id = $goal->appraisal_identity_id;
$goaldetail->employee_id = $goal->employee_id;
$goaldetail->company_id = Auth::user()->company_id;
$goaldetail->created_by = Auth::user()->id;
$goaldetail->created_at = date("Y-m-d H:i:s");
$goaldetail->is_active = 1;
$goaldetail->save();
}
$min_date = AppraisalGoalDetail::select('start_date')->where('appraisal_goal_id', $goal->id)->min('start_date');
$max_date = AppraisalGoalDetail::select('end_date')->where('appraisal_goal_id', $goal->id)->max('end_date');
$parentid = AppraisalGoalType::select('parent_id')->whereNotNull('parent_id')->where('company_id', $userCompany)->where('id', $goal->goal_type_id)->first();
$goal->update([
'appraisal_start_date' => $min_date,
'appraisal_end_date' => $max_date,
'parent_id' => $parentid->parent_id
]);
DB::commit();
Session::flash('success', 'Appraisal Goal is created successfully');
return redirect()->route('appraisal.appraisal_goals.index');
} catch (Exception $exception) {
dd($exception->getMessage());
DB::rollback();
Session::flash('error', 'Action failed! Please try again');
return redirect()->route('appraisal.appraisal_goals.index');
}
}
Everything works perfectly on the local server. But when I deployed it online to Digital Ocean. Then, when I click on submit, I got this error:
"Unable to write in the "/var/www/html/laraapp/public/storage/documents/appraisal_goal" directory"
How do I resolve it?
Thank you
You do not have necessary permission to write in storage folder.
Execute below command to provide necessary permission to write in storage folder
chmod 755 -R /var/www/html/laraapp/public/storage
chown www-data:www-data /var/www/html/laraapp/public/storage
I was try to upload multiple files using Storage so here's my code:
public function store(Request $request)
{
$gallery = new Gallery();
$gallery->name = $request->name;
$gallery->description = $request->description;
$gallery->save();
$path = 'public'.'/'.str_slug($request->name);
Storage::makeDirectory(str_slug($request->name));
$image = new Image();
if ( is_array($request->images)) {
foreach ($request->images as $file) {
$image->gallery_id = $gallery->id;
$image->name = $file->getClientOriginalName();
$image->extension = $file->extension();
$image->save();
Storage::putFileAs($path, $file);
}
} else {
$image->gallery_id = $gallery->id;
$image->name = $request->images->getClientOriginalName();
$image->save();
}
}
and I already setup my form like this:
<div class="form-group">
<label for="images" class="col-md-2 control-label">Images</label>
<div class="col-md-6">
<input type="file" class="form-control" name="images[]" id="images[]" multiple>
</div>
</div>
it give me an errors like this:
"Type error: Too few arguments to function Illuminate\Filesystem\FilesystemAdapter::putFileAs(), 2 passed in E:\laragon\www\blog\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemManager.php on line 343 and at least 3 expected ◀"
so the problem is it the way I check that it has multiple file using is_array is right? and the second what can cause this problems?
Pass the 3rd argument to the putFileAs method
if( is_array($request->images)){
foreach ($request->images as $file) {
$image = new Image();
$image->gallery_id = $gallery->id;
$image->name = $file->getClientOriginalName();
$image->extension = $file->extension();
$image->size = 555;
$image->save();
// Or any custom name as the third argument
Storage::putFileAs($path, $file, $file->getClientOriginalName());
}
} else {
$image = new Image();
$image->gallery_id = $gallery->id;
$image->name = $request->images->getClientOriginalName();;
$image->save();
// Or any custom name as the third argument
Storage::putFileAs($path, $request->images, $request->images->getClientOriginalName());
}
return back();
Documentation
You can use below example. I think this will help you to solve your problem
public function store(Request $request)
{
$gallery = new Gallery();
$gallery->name = $request->name;
$gallery->description = $request->description;
$gallery->save();
$path = '/images/'.str_slug($request->name);
if( is_array($request->images)){
foreach ($request->images as $file){
$image = new Image();
$image->gallery_id = $gallery->id;
$image->name = $file->getClientOriginalName();
$image->extension = $file->extension();
$image->size = 555;
$image->save();
$file->move(public_path($path, $image->name);
}
}else{
$image = new Image();
$image->gallery_id = $gallery->id;
$image->name = $request->images->getClientOriginalName();;
$image->save();
$file->move(public_path($slug, $image->name);
}
return back();
}
The problem is clear. You are trying to call the function "putFileAs" using 2 parameters, when she needs at least 3.
Just take a look in the documentation here
So