How to get Multiple data in laravel - php

this is the controller im getting error Property [{$key}] does not exist on this collection instance.
public function showcustom($id)
{
$custompack = CustomPackages::find($id);
$venue = Venue::find($custompack->venue);
$food = Food::all();
$attire = Attire::all();
$hairmakeup = HairMakeup::all();
$invitation = Invitation::all();
$photosvideos = PhotosVideos::all();
$cakescupcakes = CakesCupcakes::all();
$lightsound = LightSound::all();
$programhost = ProgramHost::all();
$eventsingers = EventSingers::all();
//$custompack = CustomPackages::all();
return view('packages.customshow',compact('venue','custompack','packages','food','attire','hairmakeup','photosvideos','cakescupcakes','lightsound','programhost','eventsingers','invitation'));
}

Please check if the venue you are passing in Venue::find(integer_expected) is an integer..

Related

Shorter way in php laravel to load a model in another similar one?

I want to copy every property of the contract model into this another templatecontract model and so on. My code works, but I don't know much about laravel (or php in fact) and my intuition tells me that there must be a better way, or more elegant way.
fill() from Laravel does not get much better. Maybe with a constructor?
Equal tables:
Contract -> TemplateContract
Chapter -> TemplateChapter
Clause -> TemplateClause
public function storeTemplate(ContractCreateRequest $request)
{
DB::beginTransaction();
try
{
$contract = Contract::find($request->input()['type']);
$templatecontract = new TemplateContract();
$templatecontract->id = $contract->id;
$templatecontract->pid = $contract->pid;
$templatecontract->deleted = $contract->deleted;
$templatecontract->sorting = $contract->sorting;
$templatecontract->created_at = $contract->created_at;
$templatecontract->updated_at = $contract->updated_at;
$templatecontract->deleted_at = $contract->deleted_at;
$templatecontract->title = $contract->title;
$templatecontract->description = $contract->description;
$templatecontract->hidden = $contract->hidden;
$templatecontract->contract_type = $contract->contract_type;
$templatecontract->process_type = $contract->process_type;
$templatecontract->tstamp = $contract->tstamp;
$templatecontract->is_english = $contract->is_english;
$templatecontract->usecasetitle = $contract->usecasetitle;
if (Auth::user()) {
$templatecontract->user_id = Auth::user()->id;
}
if($templatecontract->save())
{
$chapter = DB::table('chapter')->where('contract', $templatecontract->id)->get();
if(isset($chapter))
{
foreach ($chapter as $key => $value) {
$templatechapter = new TemplateChapter();
$templatechapter->clause = $value->clause;
$templatechapter->contract = $value->contract;
$templatechapter->created_at = $value->created_at;
$templatechapter->deleted = $value->deleted;
$templatechapter->headlinetype = $value->headlinetype;
$templatechapter->hidden = $value->hidden;
$templatechapter->id = $value->id;
$templatechapter->must = $value->must;
$templatechapter->note = $value->note;
$templatechapter->sorting = $value->sorting;
$templatechapter->title = $value->title;
$templatechapter->tstamp = $value->tstamp;
$templatechapter->updated_at = $value->updated_at;
$templatechapters[] = $templatechapter;
if($templatechapter->save())
{
$clause = DB::table('clause')->where('chapter', $value->id)->get();
if(isset($clause))
{
foreach ($clause as $key => $value) {
$templateclause = new TemplateClause();
$templateclause->id = $value->id;
$templateclause->chapter = $value->chapter;
$templateclause->clausetext = $value->clausetext;
$templateclause->variable = $value->variable;
$templateclause->topic = $value->topic;
$templateclause->deleted = $value->deleted;
$templateclause->selectword = $value->selectword;
$templateclause->shortinfo = $value->shortinfo;
$templateclause->sorting = $value->sorting;
$templateclause->created_at = $value->created_at;
$templateclause->updated_at = $value->updated_at;
$templateclause->hidden = $value->hidden;
$templateclause->tstamp = $value->tstamp;
$templateclause->save();
$templateclauses[] = $templateclause;
}
}
}
}
}
}
//DB::commit();
return response()->success(__('success.showing', ['resource' => 'des Vertrags', 'resourceE' => 'contract']), $templatecontract, 200);
}
catch (Exception $e)
{
return response()->error(__('error.showing', ['resource' => 'des Vertrags', 'resourceE' => 'contract']), 400, $e);
}
}
So almost all of the props from the request matches with the column names. You just need to call the create method of Eloquent model to create a new record and pass key value pair but rather an object. Also, you need not to worry about the extra parameters $contract contains since Laravel will only extract and assign params defined in protected $fillable property of the class.
// cast object to array
$contract = (array) $contract;
$templateContract = TemplateContract::create($contract)

Laravel 5.4 - Update an existing record

I'm struggling to update an existing record through an Eloquent Model in Laravel 5.4
I have for creating a record that works perfectly fine, I took it and modified it to try and update the record:
public function commitEdit ($char_edit_id)
{
$edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->first();
$edited_character->campaign_id = 1;
$edited_character->character_name = request('characterName');
$edited_character->Race = request('race');
$edited_character->Sub_Race = request('subRaceField');
$edited_character->Class = request('class');
$edited_character->Level = request('level');
$edited_character->Strength = request('strength');
$edited_character->Dexterity = request('dexterity');
$edited_character->Constitution = request('constitution');
$edited_character->Intelligence = request('intelligence');
$edited_character->Wisdom = request('wisdom');
$edited_character->Charisma = request('charisma');
$levelVar = request('level');
if ($levelVar >= 4) {
$edited_character->Proficiency = 2;
} else if ($levelVar >= 8) {
$edited_character->Proficiency = 3;
}
$edited_character->Trained_Skills = request('skillsField');
$edited_character->Languages = request('languagesField');
$edited_character->Hit_Die = 1;
$edited_character->max_HP = request('max-hp');
$edited_character->Alignment = request('alignment');
$edited_character->Armor_Class = request('armor-class');
$edited_character->Initiative = request('initiative');
$edited_character->Speed = request('speed');
$edited_character->Background = request('background');
$edited_character->update();
return redirect('./characters');
That gives this error:
Call to undefined method stdClass::update()
I have tried using save() but I get the same error with save() instead of update()
Thanks in advance c:
Documentation
If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single StdClass object:
$edited_character is a stdClass, no Eloquent model.
You can try this code:
public function commitEdit ($char_edit_id)
{
$edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->update([
'campaign_id' => 1,
'character_name' => request('characterName'),
'Race' => request('race'),
//others property
]);
}
Or create Characters model which will be extends from Illuminate\Database\Eloquent\Model and use save method:
public function commitEdit ($char_edit_id)
{
$edited_character = Characters::where('char_id', $char_edit_id)-first();
//your code with properties
$edited_character->save();
}
You can try this way:
public function commitEdit ($char_edit_id)
{
$edited_character = Characters::find($char_edit_id);
$edited_character->character_name = request('characterName');
$edited_character->Race = request('race');
$edited_character->Sub_Race = request('subRaceField');
$edited_character->Class = request('class');
$edited_character->Level = request('level');
$edited_character->Strength = request('strength');
$edited_character->Dexterity = request('dexterity');
$edited_character->Constitution = request('constitution');
$edited_character->Intelligence = request('intelligence');
$edited_character->Wisdom = request('wisdom');
$edited_character->Charisma = request('charisma');
$levelVar = request('level');
if ($levelVar >= 4) {
$edited_character->Proficiency = 2;
} else if ($levelVar >= 8) {
$edited_character->Proficiency = 3;
}
$edited_character->Trained_Skills = request('skillsField');
$edited_character->Languages = request('languagesField');
$edited_character->Hit_Die = 1;
$edited_character->max_HP = request('max-hp');
$edited_character->Alignment = request('alignment');
$edited_character->Armor_Class = request('armor-class');
$edited_character->Initiative = request('initiative');
$edited_character->Speed = request('speed');
$edited_character->Background = request('background');
if($edited_character->save()){
return redirect('./characters');
}else{
// show error message
}
}

Difficulties in creating update with Laravel 5.1

I'm trying to create an update with Laravel 5.1 but it shows the error:
I have 2 updates in this method and I noticed that the error already happens in the first one
Type error: Argument 1 passed to
Illuminate\Database\Eloquent\Builder::update() must be of the type
array, null given
My Controller
public function update($id)
{
$dadosForm = $this->request->except('_token');
$dadosForm = $this->request->offsetUnset('fat_cnpj');
$dadosForm = $this->request->offsetUnset('vatendimento');
$dadosForm = $this->request->offsetUnset('integra');
$dadosForm = $this->request->offsetUnset('material');
$proposta = $this->proposta;
$proposta->cliente_id = $this->request->get('cliente_id');
$proposta->contato = $this->request->get('contato');
$proposta->email = $this->request->get('email');
$proposta->telefone = $this->request->get('telefone');
$proposta->fatcnpj = $this->request->get('fatcnpj');
$proposta->atendimento = $this->request->get('atendimento');
$proposta->dt_solicitacao = $this->request->get('dt_solicitacao');
$proposta->dt_vigencia = $this->request->get('dt_vigencia');
$proposta->vendedor = $this->request->get('vendedor');
$proposta->coleta = $this->request->get('coleta');
$proposta->dt_integracao = $this->request->get('dt_integracao');
$proposta->hr_integracao = $this->request->get('hr_integracao');
$proposta->frete_material = $this->request->get('frete_material');
$proposta->status_id = $this->request->get('status_id');
$this->proposta->where('id', $id)->update($dadosForm);
$proposta_id = $id;
$count = $this->ensaios->max('id');
for($i=1;$i<=$count;$i++){ //Save Ensaios
$proposta_ensaios = new PropostaEnsaios();
$proposta_ensaios->id_proposta = $proposta_id;
$proposta_ensaios->id_produto = $i;
$proposta_ensaios->quantidade = $dadosForm['quantidade_'.$i];
$proposta_ensaios->valor= $dadosForm['valor_'.$i];
$proposta_ensaios->total = $dadosForm['total_'.$i];
$proposta_ensaios->where('id', $id)->update($dadosForm);
}
$this->request->session()->flash('alert-success', 'Dados Alterados com Sucesso!');
return redirect()->route('manage-content');
}
You're setting the $dadosForm variable to NULL in the last four lines of:
$dadosForm = $this->request->except('_token');
$dadosForm = $this->request->offsetUnset('fat_cnpj');
$dadosForm = $this->request->offsetUnset('vatendimento');
$dadosForm = $this->request->offsetUnset('integra');
$dadosForm = $this->request->offsetUnset('material');
(For reference PHP will return a NULL value from functions which don't specify a return - as offsetUnset doesn't.) I believe you're intending to do something more like:
$this->request->offsetUnset('fat_cnpj');
$this->request->offsetUnset('vatendimento');
$this->request->offsetUnset('integra');
$this->request->offsetUnset('material');
$dadosForm = $this->request->except('_token');

Query array on model codeigninter

I am a student who learns CodeIgniter, I have a school assignment about the database, I create a project, and run query in my model just like this.
class Send_model extends CI_Model{
function hello(){ $table = $this->db->query("SELECT * FROM (`tbl1`) LEFT JOIN `tbl2` ON `tbl2`.`id` = `tbl1`.`child_id` LEFT JOIN `tbl3` ON `tbl3`.`id` = `tbl1`.`child_id` ");
foreach($table->result() as $row){
$modbus[]= $row->modbus;
$data []= $row->data;
$alert[]= $row->alert;
};
$param0 = '&'.$modbus[0].'='.$data[0].':'.$alert[0];
$param1 = '&'.$modbus[1].'='.$data[1].':'.$alert[1];
$param2 = '&'.$modbus[2].'='.$data[2].':'.$alert[2];
$param3 = '&'.$modbus[3].'='.$data[3].':'.$alert[3];
$param4 = '&'.$modbus[4].'='.$data[4].':'.$alert[4];
$param5 = '&'.$modbus[5].'='.$data[5].':'.$alert[5];
$param6 = '&'.$modbus[6].'='.$data[6].':'.$alert[6];
$param7 = '&'.$modbus[7].'='.$data[7].':'.$alert[7];
$param8 = '&'.$modbus[8].'='.$data[8].':'.$alert[8];
$param9 = '&'.$modbus[9].'='.$data[9].':'.$alert[9];
$param10 = '&'.$modbus[10].'='.$data[10].':'.$alert[10];
$param11= '&'.$modbus[11].'='.$data[11].':'.$alert[11];
$param12 = '&'.$modbus[12].'='.$data[12].':'.$alert[12];
$param13 = '&'.$modbus[13].'='.$data[13].':'.$alert[13];
$param14 = '&'.$modbus[14].'='.$data[14].':'.$alert[14];
$param15 = '&'.$modbus[15].'='.$data[15].':'.$alert[15];
$param16 = '&'.$modbus[16].'='.$data[16].':'.$alert[16];
$param17 = '&'.$modbus[17].'='.$data[17].':'.$alert[17];
$param18 = '&'.$modbus[18].'='.$data[18].':'.$alert[18];
$sent = $param0.$param1.$param3.$param4.$param5.$param6.$param7.$param8.$param9.$param10.$param11.$param12.$param13.$param13.$param14.$param15.$param16.$param17;
return $sent;
}
my controller just like this
class Send extend CI_Controller{
function data ()
{
$this->load->model('send_model');
$send = $this->model->send_model->hello();
echo $sent;
}
}
i have a problem,if tbl1 add data then i have to write adding code to my script
can anyone help me to simplify this code?
It is because you're hard-coding to send only 19 rows of data by creating 19 $param variables. In a dynamic condition, you may have an empty record-set or hundreds of records. Modify the structure of your foreach loop to the following:
$param = array();
foreach($table->result() as $row){
$param[] = '&'.$row->modbus.'='.$row->data.':'.$row->alert;
}
return $param;
Hope it answers your question.

PHP classes are keeping me from accessing the needed variable

ok so i have a php file that includes code for do a sql insert
myfile.php
include($somewhere.'/addleads.php');
addleads.php
require_once(MAIN_CLASS_PATH."common/class.Common.php");
require_once(MAIN_CLASS_PATH."modules/Leads/class.Leads.php");
$objcommon = new common();
$objLeads = new Leads();
$Errormsg = $objLeads->AddLBCleads($_REQUEST);
class.Leads.php
class Leads extends Common{
function Leads(){
$this->Common();
$this->Leadstype = "Leadstype";
$this->Leads = "Leads";
}
function AddLBCleads($objArray){
global $_REQUEST,$objSmarty,$global_config;
$objLeads = new Leads();
$objInsertArray['txtP_Ident'] = $objArray['selProperty'];
$objInsertArray['txtFirstName'] = $objArray['txtfirstname'];
$objInsertArray['txtLastName'] = $objArray['txtlastname'];
$objInsertArray['txtEmail'] = $objArray['txtEmail'];
$objInsertArray['txtPhone'] = $objArray['txtPhone'];
$objInsertArray['txtTypeId'] = $objArray['selleadtype'];
$objInsertArray['txtComments'] = $objArray['txtcomments'];
$StrEnterdate = date('Y-m-d H:i:s');
$objInsertArray['txtMoveDate'] = $StrMoveDate;
$objInsertArray['txtEntereddate'] = $StrEnterdate;
$current_id = $this->AddInfoToDB($objInsertArray,"txt",$this->LBCPrimary_leads);
How do i get $current_id from myfile.php, when i try to access it is unavailable
Just below this line:
class Leads extends Common{
Add:
public $current_id = null; // create a public accessible variable
And instead of:
$current_id = $this->AddInfoToDB($objInsertArray,"txt",$this->LBCPrimary_leads);
Use:
$this->current_id = $this->AddInfoToDB($objInsertArray,"txt",$this->LBCPrimary_leads);
Now you can get it like:
$objLeads = new Leads();
echo $objLeads->current_id;

Categories