Invalid argument supplied for foreach() Laravel 5.3 - php

I have 2 table(interview and interviewtrack) in 1 form.
here's my savehandler code so far :
public function saveHandler(Request $request, $obj)
{
try {
DB::beginTransaction();
$obj->fill($request->all());
if (!$obj->save()) {
throw new ValidationException($obj->errors());
}
foreach($request->interviewTracks as $interviewTrack) {
\Log::info($interviewTrack);
if (empty($interviewTrack['id'])) {
$interviewTrack = new InterviewTrack();
}
else {
$interviewTrack = InterviewTrack::find($interviewTrack['id']);
}
$interviewTrack->interview()->associate($obj);
$interviewTrack['interview_id'] = isset($interviewTrack['interview_id']);
$interviewTrack->status = $interviewTrack['status'];
$interviewTrack->track_date = isset($interviewTrack['track_date']) ? $interviewTrack['start_time'] : 'YYYY-MM-DD';
$interviewTrack->outcome = $interviewTrack['outcome'];
$interviewTrack->remarks = $interviewTrack['remarks'];
$interviewTrack->save();
};
if (!$interviewTrack->save()) {
throw new ValidationException($interviewTrack->errors());
}
DB::commit();
return $this->sendSuccessResponse($request);
} catch (ValidationException $e) {
DB::rollback();
\Log::error($e->errors);
return $this->sendErrorResponse($request, $e->errors);
} catch (Exception $e) {
DB::rollback();
\Log::error($e->getMessage());
return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator');
}
}
here's my view form code :
<?php $index = 0; ?>
<?php echo Form::hidden('interviewTracks['.$index.'][id]', null); ?>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Track Date</label>
<?php echo Form::text('interviewTracks['.$index.'][track_date]', null, ['id' => 'track_date','class' => 'form-control']); ?>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Status</label>
<?php echo Form::select('interviewTracks['.$index.'][status]', $interviewTrackList, null, ['id' => 'status','class' => 'form-control']); ?>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Outcome</label>
<?php echo Form::select('interviewTracks['.$index.'][outcome]', $outcomeList, null, ['id' => 'outcome','class' => 'form-control']); ?>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Remark</label>
<?php echo Form::text('interviewTracks['.$index.'][remark]', null, ['id' => 'remarks','class' => 'form-control']); ?>
</div>
</div>
</div>
<?php $index++; ?>
it throws error Invalid argument supplied for foreach() when I try to save.
any idea ??

Change:
foreach($request->interviewTracks as $interviewTrack)
to:
foreach($request->input('interviewTracks') as $interviewTrack)

Related

BadMethodCallException Method [header] does not exist on view. in View.php

I am trying to display messages in the error section from sender and receiver, both returning view mes_res from MessageController, but it is displaying an error message.
#controller method:
public function respone_msg($myid){
$dd = DB::table('messeges')->where(['is_read'=>0,'sender_id' =>$myid , 'receiver_id' => Session::get('id')])->get();
$myCount = count($dd);
var_dump($myCount);
$sendmsesgesreal = DB::table('messeges')->where(['sender_id' => Session::get('id') , 'receiver_id' => $myid])->orWhere(['receiver_id' => Session::get('id') ])->get();
//return view('users.mes_res')->with(['msg_data' => $sendmsesgesreal, 'user_id' => $myid, 'conv_id' => $sendmsesgesreal[0]->conversation_id]);
return view('users.mes_res')->with(['msg_data' => $sendmsesgesreal, 'user_id' => $myid, 'conv_id' => $sendmsesgesreal[0]->conversation_id])->header('Cache-Control', 'no-cache, no-store, must-revalidate');
}
view mes_reg:
<div class="messages-headline">
<i class="icon-feather-trash-2"></i> Delete Conversation
</div>
<!-- Message Content Inner -->
<div class="message-content-inner">
<?php
foreach ($msg_data as $msg) {
//
if($msg->sender_id==Session::get('id') && $msg->receiver_id==$user_id) //conv_id
{
$getuserdat1 = DB::table('profile_users')->where('user_id',$msg->sender_id)->get();
// print_r($msg);
if (count($getuserdat1)>0)
{
$fullname=$getuserdat1[0]->fname.' '.$getuserdat1[0]->lname;
if ($getuserdat1[0]->profile_images)
{
$img11 = url('')."/public/uploads/".$getuserdat1[0]->profile_images;
}
else
{
$img11 = url('')."/public/users/images/user-avatar-placeholder.png";
}
}
else
{
$img11 = url('')."/public/users/images/user-avatar-placeholder.png";
}
echo '<input type="hidden" name="receiver_id" id="receiver_id" value="'.$msg->sender_id.'">';
//print_r($img11);
?>
<div class="message-time-sign">
<span>{{ \Carbon\Carbon::parse($msg->CreatedBydate)->diffForhumans() }}</span>
</div>
<div class="message-bubble me">
<div class="message-bubble-inner">
<div class="message-avatar"><img src="{{$img11}}" alt="" /></div>
<div class="message-text"><p>{{$msg->messeges}}</p></div>
</div>
<div class="clearfix"></div>
</div>
<?php }
elseif($msg->sender_id==$user_id)
//else
{
//print_r($msg);
// $receiverdata = DB::table('messeges')
// ->where('sender_id',$msg->sender_id)->get();
// print_r($receiverdata);
//print_r($msg);
$getuserdat11 = DB::table('profile_users')->where('user_id',$msg->sender_id)->get();
if(count($getuserdat11)>0)
{
if ($getuserdat11[0]->profile_images)
{
$img111 = url('')."/public/uploads/".$getuserdat11[0]->profile_images;
}
else
{
$img111 = url('')."/public/users/images/user-avatar-placeholder.png";
}
}
else
{
$img111 = url('')."/public/users/images/user-avatar-placeholder.png";
}
echo '<input type="hidden" name="receiver_id" id="receiver_id" value="'.$msg->sender_id.'">';
//print_r($img111);
?>
<div class="message-time-sign">
<span>{{ \Carbon\Carbon::parse($msg->CreatedBydate)->diffForhumans() }}</span>
</div>
<div class="message-bubble">
<div class="message-bubble-inner">
<div class="message-avatar"><img src="{{$img111}}" alt="" /></div>
<div class="message-text"><p>{{$msg->messeges}}</p></div>
</div>
<div class="clearfix"></div>
</div>
<?php }
?>
</div>

Updating existing sql data in Cake PHP

Currently trying to figure out how to update existing SQL data in cake php. I have my input fields being automatically filled with preexisting data but whenever I hit save to change existing data it does nothing. NO error or warning message.
Here is my Controller:
public function settings()
{
$user = $this->UserAuth->getUser();
$userid = $this->UserAuth->getUserId();
//$userEntity = $this->Push->find()->where(['user_id = ' => $userid])->order('time')->toArray($this->request->data, ['validate'=>false]);
//$query = $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();
$userEntity = $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();
if($this->request->is('patch', 'post', 'put')) {
$errors = $userEntity->errors();
if($this->request->is('ajax')) {
if(empty($errors)) {
$response = ['error'=>0, 'message'=>'success'];
} else {
$response = ['error'=>1, 'message'=>'failure'];
$response['data']['push'] = $errors;
}
echo json_encode($response);exit;
} else {
if(empty($errors)) {
if(!empty($this->request->data($userEntity['iosapikey']))) {
$userEntity['iosapikey'] = $this->request->data['Push']['iosapikey'];
}
if(!empty($this->request->data['Push']['androidapikey'])) {
$userEntity['androidapikey'] = $this->request->data['Push']['androidapikey'];
}
if(!empty($this->request->data['Push']['restapikey'])) {
$userEntity['restapikey'] = $this->request->data['Push']['restapikey'];
}
if(!empty($this->request->data['Push']['segments'])) {
$userEntity['iosapikey'] = $this->request->data['Push']['segments'];
}
if(!empty($this->request->data['Push']['tags'])) {
$userEntity['androidapikey'] = $this->request->data['Push']['tags'];
}
if(!empty($this->request->data['Push']['deeplinks'])) {
$userEntity['restapikey'] = $this->request->data['Push']['deeplinks'];
}
if($this->Push->save($userEntity, ['validate'=>false])) {
$this->Flash->success(__('Push settings have been updated successfully'));
//$this->redirect(['action'=>'index', 'page'=>$page]);
} else {
$this->Flash->error(__('Unable to update push settings, please try again'));
}
}
}
} else {
if(!empty($userEntity['iosapikey'])) {
$userEntity['iosapikey'] = $userEntity['iosapikey'];
}
if(!empty($userEntity['androidapikey'])) {
$userEntity['androidapikey'] = $userEntity['androidapikey'];
}
if(!empty($userEntity['restapikey'])) {
$userEntity['restapikey'] = $userEntity['restapikey'];
}
if(!empty($userEntity['segments'])) {
$userEntity['segments'] = $userEntity['segments'];
}
if(!empty($userEntity['tags'])) {
$userEntity['tags'] = $userEntity['tags'];
}
if(!empty($userEntity['deeplinks'])) {
$userEntity['deeplinks'] = $userEntity['deeplinks'];
}
}
$this->set(compact('userEntity'));
$this->set('_serialize', ['push']);
}
This is my template:
<div class="panel-body">
<?php echo $this->element('Usermgmt.ajax_validation', ['formId'=>'editPushForm', 'submitButtonId'=>'editPushSubmitBtn']); ?>
<?php echo $this->Form->create($userEntity, ['id'=>'editPushForm', 'class'=>'form-horizontal', 'type'=>'text']);?>
<div class="form-group col-md-9 col-sm-10">
<label>iOS API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.iosapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Android API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.androidapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>REST API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.restapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Segments:</label>
<div class="">
<?php echo $this->Form->input('Push.segments',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Tags:</label>
<div class="">
<?php echo $this->Form->input('Push.tags',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Deeplinks:</label>
<div class="">
<?php echo $this->Form->input('Push.deeplinks',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-sm-offset-2 col-sm-3">
<?php echo $this->Form->Submit(__('Save'), ['div'=>false, 'class'=>'btn btn-primary pushbutton', 'id'=>'editPushSubmitBtn']); ?>
</div>
</div>
<?php echo $this->Form->end();?>
I'm sure I have a small error somewhere I just haven't spotted yet, but having put many hours into this with no positive result I thought it was time to find some help.
Cake PHP find function returns an object so i would request you to use a statement like $userEntity->iosapikey = $this->request->data['Push']['iosapikey']; to assign values and also enable debuging in cakephp https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#debugging-queries-and-resultsets

how to pass Array of object to view ? MVC ..in Codeigniter

I'm not understand how to pass array of object which contain data of table and this is store in one variable when model query return..
so help me...
more time im spend on this error so if any one have idea on this error
Error :
Undefined variable: data Filename: employee/view_Admin_Profile.php
Line Number: 164
at line is <?php echo ($data->sub_dept_name);?>
contoller :
public function update_Admin_Profile($emp_id)
{
$data = $this->Loginm->getEmployeeProfile($emp_id);
$this->load->view('admin/header',$data);
$this->load->view('admin/sidebar',$data);
$this->load->view('employee/update_Admin_Profile',$data);
$this->load->view('admin/footer');
}
view:
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Gendar:</label>
<div class="col-lg-8">
<?php echo ($data->gender);?>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Department Name:</label>
<div class="col-lg-8">
<?php echo ($data->sub_dept_name);?>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Role:</label>
<div class="col-lg-8">
<?php echo ($getEmpProfile->role_name);?>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Nationality:</label>
<div class="col-lg-8">
<?php echo ($data->nationality);?>
</div>
</div>
</div>
Model :
public function getEmployeeProfile($emp_id)
{
$this->db->select('*');
$this->db->from('employee e');
$this->db->join('sub_department sd','sd.sub_dept_id = e.sub_dept_id','left');
$this->db->join('department d','d.dept_id = sd.dept_id','left');
$this->db->join('user_role ur','ur.role_id = e.role_id','left');
$this->db->where('e.emp_id',$emp_id);
$query=$this->db->get();
if($query->num_rows() > 0)
{
return $query->row();
}
}
public function update_Admin_Profile($emp_id)
{
//$data = $this->Loginm->getEmployeeProfile($emp_id); Your Code
$data['data'] = $this->Loginm->getEmployeeProfile($emp_id); //Change to this line
$this->load->view('admin/header',$data);
$this->load->view('admin/sidebar',$data);
$this->load->view('employee/update_Admin_Profile',$data);
$this->load->view('admin/footer');
}
and in view you can fetch like the following code:
<?php echo ($data[0]->sub_dept_name);?>
Let's take the example:
You are getting the data from model like,
$data = array('gender' => 'male', 'age' => 22)
Then to access in codeigniter view you need to echo array's key as a variable
example:
<?php echo $gender; ?>

Add cart without reloading

I tried adding a cart in my application, but how you can while in order to add a new cart page does not reload, but the cart increases.
my controllers
function add_cart($code) {
$produk = $this->Model->get_id($code);
$data = array(
'id' => $produk->code
'qty' => 1,
'name' => $produk->name );
$this->cart->insert($data);
}
Model
function get_id($code) {
$query = $this->db->select('*')
->from('mytable')
->where('code', $code)
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
View
<?php foreach($product as $row) ?>
<div class="col-sm-4">
<div class="col-item">
<div class="info">
<div class="row">
<div class="price col-md-12">
<h5> <?php echo $row->name_product ;?></h5>
</div>
</div>
<div class="separator clear-left">
<p class="btn-add">
class="hidden-sm" href="<?php echo site_url('home/add_cart/'.$row->code);?>">Add</a></p>
</div>
</div>
</div>
</div>
<?php endforeach ?>
Use ajax to replace the href to request the add_chart operation, and get the response to refresh the page content without reload the page.

Simple File Upload for FuelPHP

Does anyone have experience of uploading a series of files to a web server with FuelPHP?
My current setup adds content to a database from a Form, but I'd like to process images at this point too - so basically move them to my web server when submitting a form.
Is this simple to do?
I have my 'action_add()' method in my controller, but not sure how to update it to loop through all my file fields and move files.
public function action_add()
{
$val = Model_Article::validate('add_article');
if ($val->run())
{
$status = (Input::post('save_draft') ? 0 : 1);
if ( ! $val->input('category_id'))
{
$category_id = null;
}
else
{
$category_id = $val->validated('category_id');
}
$article = new Model_Article(array(
'user_id' => $this->user_id,
'category_id' => $category_id,
'title' => $val->validated('title'),
'body' => $val->validated('body'),
'published' => $status,
));
if ($article->save())
{
Session::set_flash('success', 'Article successfully added.');
}
else
{
Session::set_flash('error', 'Something went wrong, '.
'please try again!');
}
Response::redirect('articles/add');
}
$this->template->title = 'Add Article';
$this->template->content = View::forge('articles/add')
->set('categories', Model_Category::find('all'), false)
->set('val', Validation::instance('add_article'), false);
}
My Form:
<h2>Add an Article</h2>
<p>Publish a new article by filling the form below.</p>
<div class="options">
<div class="option">
<?php echo Html::anchor('articles', 'View Articles'); ?>
</div>
<div class="option">
<?php echo Html::anchor('categories/add', 'Add a Category'); ?>
</div>
</div>
<?php echo $val->show_errors(); ?>
<?php echo Form::open(array('enctype' => 'multipart/form-data')); ?>
<?php $select_categories = array(null => 'Uncategorized'); ?>
<?php foreach ($categories as $category): ?>
<?php $select_categories[$category->id] = $category->name; ?>
<?php endforeach; ?>
<div class="input select">
<?php echo Form::label('Category', 'category_id'); ?>
<?php echo Form::select('category_id', e($val->input('category_id')),
$select_categories); ?>
</div>
<div class="input text required">
<?php echo Form::label('Title', 'title'); ?>
<?php echo Form::input('title', e($val->input('title')),
array('size' => '30')); ?>
</div>
<div class="input textarea required">
<?php echo Form::label('Body', 'body'); ?>
<?php echo Form::textarea('body', e($val->input('body')),
array('rows' => 4, 'cols' => 40)); ?>
</div>
<div class="input textarea required">
<?php echo FORM::file('filename'); ?>
</div>
<div class="input submit">
<?php echo Form::submit('add_article', 'Publish'); ?>
<?php echo Form::submit('save_draft', 'Save Draft'); ?>
</div>
<?php echo Form::close(); ?>
Many thanks for any pointers.
Okay I can give you some instruction.
First fuelphp upload documentation
Hope it helps sorry if there are typos in it
public function action_add()
{
$val = Model_Article::validate('add_article'); //<-- maybe its just me but I never saw any similar to this in fuelphp sorry about this if I'm wrong
// if your form validation is okay than continue with everyhing else
if ($val->run())
{
$article = Model_Article::forge();
// Custom configuration for this upload
$config = array(
'path' => DOCROOT.DS.'foldername/tomove/your/images',
'randomize' => true,
'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
);
Upload::process($config);
// if a valid file is passed than the function will save, or if its not empty
if (Upload::is_valid())
{
// save them according to the config
Upload::save();
//if you want to save to tha database lets grab the file name
$value = Upload::get_files();
$article->your_file_input_name = $value[0]['saved_as'];
}
$status = (Input::post('save_draft') ? 0 : 1);
if ( ! $val->input('category_id'))
{
$category_id = null;
}
else
{
$category_id = $val->validated('category_id');
}
$article->user_id = $this->user_id;
$article->category_i = $category_id;
$article->title = $val->validated('title');
$article->body = $val->validated('body');
$article->published = $status;
if ($article->save())
{
Session::set_flash('success', 'Article successfully added.');
}
else
{
Session::set_flash('error', 'Something went wrong, '.
'please try again!');
}
Response::redirect('articles/add');
}
$this->template->title = 'Add Article';
$this->template->content = View::forge('articles/add')
->set('categories', Model_Category::find('all'), false)
->set('val', Validation::instance('add_article'), false);
}

Categories