How to save ID dropdown's attributes dynamic in laravel livewire component - php

How can I save the ID (primary key) of the dynamic dropdowns in a combined mysql field (1,3,2,1 etc... )
I'm sorry, my english is bad
see image form in blade
field mysql save: combinated varchar(255)
see table price mysql with field for save
View Blade:
<form class="form-horizontal" wire:submit.prevent="storePrecio">
#csrf
#foreach ($product->attributeValues->unique('product_attribute_id') as $av)
<div class="form-group">
<label class="col-md-4 control-label">{{ $av->productAttribute->name }} :</label>
<div class="col-md-4">
<select class="form-control" wire:model="attribute_values.{{ $av->productAttribute->id }}">
#foreach ($av->productAttribute->attributeValues->where('product_id', $product->id) as $pav)
<option value="{{ $pav->id }}">{{ $pav->value }}</option>
#endforeach
</select>
</div>
Component Livewire:
class AdminAddPrecioComponent extends Component
{
// tabla precios
public $product_id;
public $category_id;
public $combination;
public $size;
public $quantity;
public $importe;
//atributos de producto
public $attribute_values = [];
public function storePrecio()
{
$this->validate([
'size' => 'required',
'quantity' => 'required',
'importe' => 'required'
]);
//grabar
$product = Product::where('slug', $this->slug)->first();
$product_id = $product->id;
$category_id = $product->category_id;
//
$precio = new Price();
$precio->category_id = $category_id;
$precio->product_id = $product_id;
$precio->size = $this->size;
$precio->quantity = $this->quantity;
$precio->importe = $this->importe;
//saving ID dropdowns dynamic array
foreach ($this->attribute_values as $key => $attribute_value[]) {
$avalues = explode(",", $attribute_value[]);
foreach ($avalues as $avalue) {
$precio->combination = $avalue[$key]; //NOT WORKING => EMPTY
}
}
$precio->save();
session()->flash('message', 'Precio ha sido creado con exito');
}
public function render()
{
$tamano = Tamano::all();
$cantidad = Cantidad::all();
$product_array = Product::all();
$categoria = Category::all();
$product = Product::where('slug', $this->slug)->first();
return view('livewire.admin.admin-add-precio-component', ['cantidad' => $cantidad, 'tamano' => $tamano, 'product_array' => $product_array, 'product' => $product, 'categoria' => $categoria])->layout('layouts.frontend.base');
}
my table attributes fill dropdowns
see table here

Related

Laravel 6 - `Undefined variable: value` only error in the first time

In the first time I want to save data to database from table this error appear.
ErrorException
Undefined variable: value
so I have to manually input the data from tinker or mysql
My Controller
public function store(Request $request)
{
$validateData = $request->validate([
'name_device_type' => 'required|max:255',
'signature' => 'Nullable'
]);
$id = DeviceType::getidDeviceTypes();
foreach ($id as $value); // Error happend in this line.
$lastdevicetypeId = $value->id;
$newdevicetypeId = $lastdevicetypeId + 1;
$GetnewdevicetypeId = sprintf('DT%04d', $newdevicetypeId);
$devicetypes = new DeviceType();
$devicetypes->idDeviceType = $GetnewdevicetypeId;
$devicetypes->name_device_type = $request->input('name_device_type');
$devicetypes->signature = $request->input('signature');
$devicetypes->save();
return redirect('/devicetypes')->with('success', 'New Device Type is added');
}
My Migration table
public function up()
{
Schema::create('device_types', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('idDeviceType');
$table->string('name_device_type');
$table->mediumText('signature');
$table->timestamps();
});
}
My create.blade.php
{!! Form::open(['action' => 'DeviceTypesController#store', 'method' => 'POST']) !!}
<div class="form-group">
{!! Form::label('name_device_type', 'Type Device'); !!}
{!! Form::text('name_device_type', '', ['class' => 'form-control', 'placeholder' => 'Type Device']); !!}
</div>
<div class="form-group">
{!! Form::label('signature', 'Signature (Optional)'); !!}
{!! Form::textarea('signature', '', ['id' => 'classic-ckeditor5', 'class' => 'form-control', 'placeholder' => 'Signature']); !!}
</div>
{{ Form::button('<i class="far fa-save"></i> Submit', ['type' => 'submit', 'class' => 'btn btn-info'] ) }}
{!! Form::close() !!}
the Model
class DeviceType extends Model
{
// Table Name
protected $table = 'device_types';
// Primary Key
protected $primaryKey = 'idDeviceTypes';
// Timestamps
public $timestamps = true;
public $incrementing = false;
public static function getidDeviceType(){
return $getidDeviceType = DB::table('device_types')->orderBy('id','desc')->take(1)->get();
}
}
But if the table has data this error disappear, also this error will appear again if I remove every data and made the table empty.
You have a Semicolon right after the foreach loop definition: foreach ($id as $value);
and you are using the $value in the next line $lastdevicetypeId = $value->id; which is outside the scope of your loop.
You should remove the ; following the the foreach loop and change it to : and add a endforeach; where you want to end the loop.
Example:
foreach ($id as $value):
$lastdevicetypeId = $value->id;
$newdevicetypeId = $lastdevicetypeId + 1;
$GetnewdevicetypeId = sprintf('DT%04d', $newdevicetypeId);
$devicetypes = new DeviceType();
$devicetypes->idDeviceType = $GetnewdevicetypeId;
$devicetypes->name_device_type = $request->input('name_device_type');
$devicetypes->signature = $request->input('signature');
$devicetypes->save();
endforeach;
Or you can write this code as:
foreach ($id as $value){
$lastdevicetypeId = $value->id;
$newdevicetypeId = $lastdevicetypeId + 1;
$GetnewdevicetypeId = sprintf('DT%04d', $newdevicetypeId);
$devicetypes = new DeviceType();
$devicetypes->idDeviceType = $GetnewdevicetypeId;
$devicetypes->name_device_type = $request->input('name_device_type');
$devicetypes->signature = $request->input('signature');
$devicetypes->save();
}
You could grab the max 'id' of the table and add 1 to it without having to grab a whole record:
...
// validation
$newdevicetypeId = DeviceType::max('id') + 1;
$GetnewdevicetypeId = sprintf('DT%04d', $newdevicetypeId);
// new DeviceType
...
There is the option of having a model event that can set this particular field after the record has been created so it has its own id to use.

How to pass $id of a form to another form

I have multiple forms where I need to pass through the id. In the example bellow I have 2 controllers one is for Courses and one is for the Exams. I'm trying to create a course and then pass through the course id to the exam form.
Here is what I've tried but the value is not passing through.
Course Controller:
public function store(StoreCoursesRequest $request)
{
if (! Gate::allows('course_create')) {
return abort(401);
}
$request = $this->saveFiles($request);
$course = Course::create($request->all()
// $status = array('assigned' => 'assigned', 'canceled'=>'canceled');
+ ['position' => Course::where('curriculum_id', $request->curriculum_id)->max('position') + 1]);
$trainers = \Auth::user()->isAdmin() ? array_filter((array)$request->input('trainers')) : [\Auth::user()->id];
$course->trainers()->sync($trainers);
$course->roles()->sync(array_filter((array)$request->input('roles')));
$course->assigned_user()->sync(array_filter((array)$request->input('assigned_user')));
$curriculum = Curriculum::get(array('id' => 'id'));
$exam = Exam::get(array('id' => 'id'));
foreach ($request->input('course_materials_id', []) as $index => $id) {
$model = config('medialibrary.media_model');
$file = $model::find($id);
$file->model_id = $course->id;
$file->save();
}
session('id', 'id');
return redirect()->route('admin.exams.create');
}
Here is the exams controller
public function create()
{
if (! Gate::allows('exam_create')) {
return abort(401);
}
$exam_assigneds = \App\Exam::get()->pluck('title', 'id')->prepend(trans('global.app_please_select'), '');
$questions = \App\ExamQuestion::get()->pluck('question', 'id');
$in_classes = \App\InClassCourse::get()->pluck('title', 'id')->prepend(trans('global.app_please_select'), '');
$reoccurance_type = \App\ReoccuranceType::get()->pluck('type', 'id')->prepend(trans('global.app_please_select'), '');
$courses = session('id');
return view('admin.exams.create', compact('courses', 'exam_assigneds', 'questions', 'in_classes', 'reoccurance_type'));
}
Here is the view
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('course_id', trans('global.exam.fields.course').'', ['class' => 'control-label']) !!}
{!! Form::text('id', $courses, old('id'), ['class' => 'form-control', 'placeholder' => '']) !!}
<p class="help-block"></p>
#if($errors->has('course_id'))
<p class="help-block">
{{ $errors->first('course_id') }}
</p>
#endif
</div>
</div>
All I'm getting is just text value of id. It doesn't pull the actual id.
In Course Controller modify
session('id', 'id');
to
session('id', $course->id);

Laravel Backpack. 1-1 (one-to-one) relation

I have 2 Eloquent models:
/**
* Entities/Products.php
*/
use CrudTrait;
protected $fillable = [
'name', 'macronutrients_id',
];
public function macronutrients()
{
return $this->hasOne(Macronutrients::class);
}
/**
* Entities/Macronutrients.php
*/
use CrudTrait;
protected $fillable = [
'proteins', 'fats', 'carbons', 'calories', 'product_id'
];
public function product()
{
return $this->belongsTo(Product::class);
}
I don't know how I can show table (or something like list of options) with all macronutrients on product's edit page via Laravel Backpack CRUD?
In other words, I want to make something like this:
on page http://example.com/admin/product/2/edit:
* [text] Name
* Macronutrients:
[number] proteins
[number] fats
[number] carbons
[number] calories
where [text], [number] is input fields.
I resolved this with some custom logic. As a result:
Screenshot of my /admin/product/1/edit
First of all, I created custom field:
<!-- /resources/views/vendor/backpack/crud/fields/product_macronutrients.blade.php -->
<!-- product_macronutrients -->
#php($macronutrients = isset($entry) ? $entry->macronutrients : false)
<div #include('crud::inc.field_wrapper_attributes') >
#include('crud::inc.field_translatable_icon')
<div class="array-container form-group">
<table class="table table-bordered table-striped m-b-0">
<thead>
<tr>
<th class="text-center">{{-- <i class="fa fa-trash"></i>--}} </th>
#foreach( $field['columns'] as $column )
<th style="font-weight: 300!important;">
// l10n strings (productscatalog::labels.proteins, productscatalog::labels.fats and so on)
#lang("productscatalog::labels.$column")
</th>
#endforeach
</tr>
</thead>
<tbody ui-sortable="sortableOptions" class="table-striped">
<tr class="array-row">
<td>
<p><b>#lang("productscatalog::labels.macrontr")</b></p>
</td>
#foreach( $field['columns'] as $column)
<td>
<input
class="form-control input-sm"
type="text"
name="{{ $column }}"
value="{{ old($column) ? old($column) : $macronutrients ? $macronutrients->$column : '' }}"
#include('crud::inc.field_attributes')
/>
</td>
#endforeach
</tr>
</tbody>
</table>
</div>
</div>
And ProductCrudController:
public function setup()
{
// other stuff...
$this->crud->addField([
'label' => 'Macronutrients',
'type' => 'product_macronutrients',
'name' => '',
'columns' => [
'proteins',
'fats',
'carbons',
'calories',
],
]);
}
public function store(StoreRequest $request)
{
$redirect_location = parent::storeCrud($request);
$this->storeOrUpdateMacronutrients($request, $this->crud->entry);
return $redirect_location;
}
public function update(UpdateRequest $request)
{
$redirect_location = parent::updateCrud($request);
$this->storeOrUpdateMacronutrients($request, $this->crud->entry);
return $redirect_location;
}
public function destroy($id)
{
$this->destroyMacronutrients($id);
$return = parent::destroy($id);
return $return;
}
protected function storeOrUpdateMacronutrients(Request $request, Product $product)
{
$macronutrients = Macronutrients::firstOrNew(['id' => $product->id]);
$macronutrients->proteins = $request->input('proteins');
$macronutrients->fats = $request->input('fats');
$macronutrients->carbons = $request->input('carbons');
$macronutrients->calories = $request->input('calories');
$macronutrients->save();
}
protected function destroyMacronutrients($productId)
{
$macronutrients = Macronutrients::findOrFail($productId);
$macronutrients->delete();
}
Hope it helps.
$this->crud->addColumn([
// 1-n relationship
'label' => "Country name", // Table column heading
'type' => "select",
'name' => 'country_name', // the column that contains the ID of that connected entity;
'entity' => 'country', // the method that defines the relationship in your Model
'attribute' => "country_name", // foreign key attribute that is shown to user
'model' => "App\Models\Country",
]);
this is an example for 1-n relationship in laravel backpack

Laravel store value from for-each loop and passed it as array

I have two for-each loop inside my create view [Subject_id, Lead_id], I want to store the for-each value into my database using array approach, and I couldn't make it work can anyone amend my codes or point me to a proper way thanks.
Controller:
public function store(Request $request)
{
//
$input = $request->all();
$items = array(['Subject_id','Lead_id']);
foreach($input as $inputs) {
$items[] = $inputs;
}
$scores = new Score();
$scores->Subject_id=$items['Subject_id'];
$scores->Lead_id=$items['Lead_id'];
$scores->save();
dd($request->all());
return redirect()->route('scores.create')->with('notif', 'Success.');
}
this is the message:
create view
#foreach ($leads as $lead)
<tr>
<td>{{ $lead->student_name }}</td>
<td><input type="checkbox" class="checkbox" name="Lead_id[]" value="{{ $lead->id }}"></td>
</tr>
#endforeach
#foreach($subjects as $subject)
<label >
<input type="checkbox" name="Subject_id[]" value="{{ $subject->id }}">
{{ $subject->subject_name }}
</label>
#endforeach
DD Result:
Try this code in your controller
public function store(Request $request)
{
$data = $request->all();
$leads = $data['Lead_id'];
$subject_ids = $data['Subject_id'];
//insert using foreach loop
foreach($leads as $key => $input) {
$scores = new Score();
$scores->Subject_id = isset($leads[$key]) ? $leads[$key] : ''; //add a default value here
$scores->Lead_id = isset($subject_ids[$key]) ? $subject_ids[$key] : ''; //add a default value here
$scores->save();
}
//insert using array at once
$rows = [];
foreach($leads as $key => $input) {
array_push($rows, [
'Subject_id' => isset($leads[$key]) ? $leads[$key] : '', //add a default value here
'Lead_id' => isset($subject_ids[$key]) ? $subject_ids[$key] : '' //add a default value here
]);
}
Score::insert($rows);
return redirect()->route('scores.create')->with('notif', 'Success.');
}
Every time creating an instance of model in foreach loop in not an efficient way. You can do something like this
foreach($input as $inputs) {
$dataArray[] = [
'Subject_id' => $inputs['Subject_id'],
'Lead_id' => $inputs['Lead_id'],
];
}
DB::table('Score')->insert($dataArray);
You can manipulate the data as you want.
with this code i have this
Update your blade
#foreach ($leads as $lead)
{{ $lead->student_name }}
#foreach($subjects as $subject)
<input type="checkbox" name="Subject_id[$lead->id][]" value="{{ $subject->id }}">
{{ $subject->subject_name }}
#endforeach
#endforeach
This as your controller
public function store(Request $request)
{
//
$subjects = $request->get('Subject_id');
foreach($subjects as $key=>$oneLeadOptions) {
foreach($oneLeadOptions as $option){
$scores = new Score();
$scores->Subject_id = $option;
$scores->Lead_id = $key;
$scores->save();
}
}
//To to other redirects logic here
}
try this

Laravel update makes new table rule instead of updating

I have a company table and an attributes table with all sorts of value in it.
One company hasMany attributes and an attribute belongsTo a company.
Now I have a value inside the attributes table with a 'account_nr_start' (for example, when a new user is added to a company its account_id starts counting up from 1000).
Controller:
public function __construct(Company $company, User $user)
{
if(Auth::user()->usertype_id == 7)
{
$this->company = $company;
}
else
{
$this->company_id = Auth::user()->company_id;
$this->company = $company->Where(function($query)
{
$query->where('id', '=', $this->company_id )
->orWhere('parent_id','=', $this->company_id);
}) ;
}
$this->user = $user;
$this->middleware('auth');
}
public function edit(Company $company, CompaniesController $companies)
{
$companies = $companies->getCompaniesName(Auth::user()->company_id);
$attributes = $company->attributes('company')
->where('attribute', '=', 'account_nr_start')
->get();
foreach ($attributes as $k => $v) {
$nr_start[] = $v->value;
}
return view('company.edit', ['company' => $company, 'id' => 'edit', 'companies' => $companies, 'nr_start' => $nr_start]);
}
public function update(UpdateCompanyRequest $request, $company, Attribute $attributes)
{
$company->fill($request->input())->save();
$attributes->fill($request->only('company_id', 'attribute_nr', 'value'))->save();
return redirect('company');
}
HTML/Blade:
<div class="form-group {{ $errors->has('_nr_') ? 'has-error' : '' }}">
{!! HTML::decode (Form::label('account_nr_start', trans('common.account_nr_start').'<span class="asterisk"> *</span>', ['class' => 'form-label col-sm-3 control-label text-capitalize'])) !!}
<div class="col-sm-6">
{!! Form::text('value', $nr_start[0], ["class"=>"form-control text-uppercase"]) !!}
{!! $errors->first('account_nr_start', '<span class="help-block">:message</span>') !!}
</div>
</div>
When I update a company now, it will upload like the last input here: :
So it makes a new rule, while it needs to edit the current attribute rule instead of making a new rule with an empty company_id/attribute.
If I understand what you are trying to do, I think this will fix your problem. The issue you have is the Attribute model is a new instance of the model rather than retrieving the model you need.
before running fill() from the attributes method try this
$new_attribute = $attributes->where('company_id', '=', $company->id)->where('attribute', '=', 'account_nr_start')->first();
Then run the fill()
$new_attribute->fill($request->only('company_id', 'attribute_nr', 'value'))->save();

Categories