I have 2 tables: listings and listings_specifications
Listings table
id
type
status
location
specifications_id
Listings_specifications table
id
listing_id
swimming_pool
water_well
I need to select the specifications (checkboxes) on the same form with which I add a listing. I have created all the forms, views, models, controllers but I think I got some logic wrong.
Listing.php model
protected $table = 'listings';
public function contact()
{
return $this->BelongsTo('contacts');
}
public function specifications()
{
return $this->BelongsTo('listings_specifications');
}
Specification.php model
protected $table = 'listings_specifications';
public function listings()
{
return $this->BelongsTo('listings');
}
ListingsController.php (where the data gets saved in the database)
$listing = new Listing;
$contact = new Contact;
$listing->status = Input::get('status');
$listing->listingfor = Input::get('listingfor');
$listing->propertystatus = Input::get('propertystatus');
$listing->propertytype = Input::get('propertytype');
$listing->userid = Auth::user()->id;
$listing->location = Input::get('location');
$listing->contact_id = $contact_id;
$listing->save();
$specifications = Specification::find($id);
if( $listings->save() ) {
$specifications = new Specification;
$specifications->listing_id = $id;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$specifications->save();
}
I'm getting this error: Undefined variable: id
Where did I go wrong?
Thank you
It looks like you have some logic errors.
First of all, you are never setting $id anywhere, but that's okay because you really don't need it.
Remove the $specifications = Specification::find($id); line because that's not doing anything.
Then change your last section to something like this...
if( $listings->save() ) {
$specifications = new Specification;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$listing->specifications()->save($specifications);
}
$listing->specifications()->save($specifications); will automatically save the new specification with the correct listing_id for you.
Modify your Listing model's specifications relationship to...
public function specifications()
{
return $this->hasMany('Specification');
}
I'm assuming here one listing can have many specifications. If not, you can easily just change that to a hasOne.
You use $id in the line $specifications = Specification::find($id); but you don't define it before.
Related
I need to get the lecture id in my student table and from then only can i get the lecture's data using that id. The problem is, the $lectureID is null when i try to apply it on the lecture model, but when i check on the console, it does get the data. Thanks in advance.
Controller
public function getLecture($id)
{
$lectureID = student::select('lecture_id_FK')->where('student_id',$id)->first();
$lectureDATA = lecture::where('lecture_id',$lectureID)->first();
return $lectureDATA;
}
you can try two ways
public function getLecture($id)
{
## way 1
$lectureID = student::where('student_id',$id)->value('lecture_id_FK');
$lectureDATA = lecture::where('lecture_id',$lectureID)->first();
## way 2
$lectureID = student::where('student_id',$id)->first();
$lectureDATA = lecture::where('lecture_id',$lectureID->lecture_id_FK)->first();
return $lectureDATA;
}
before check what inside $lectureID:
public function getLecture($id)
{
$lectureID = student::select('lecture_id_FK')->where('student_id',$id)->first();
dd($lectureID);//I believe inside will be object with lecture_id_FK
//then just
$lectureDATA = lecture::where('lecture_id',$lectureID->lecture_id_FK)->first();
return $lectureDATA;
}
Recommended way to do it:
In Student model class ( recommend use capital S )
class student
{
public function lecture(){
return $this->hasOne('App\lecture','lecture_id_FK','lecture_id');
}
}
then just load studen with lecture like this
$student = studend::with('lecture')->find($id);
$lecture = $student->lecture;
I have a code on Save.php of Module Invoice:
public function saveRecord($request) {
...
$received = $request->get('received');
$balance = $request->get('balance');
$relProject = $request->get('relProject');
$project = Project_Record_Model::getInstanceById($relProject);
$fields = $project->getField('cf_938');
$fieldInstance = Vtiger_Field::getInstance($fields->id);
if ($fieldInstance) {
$fieldInstance->value = $received;
$fieldInstance->save();
}
}
Why save() is not work? When I save Invoice my field is not update on project.. I need fix it.
You should use the Record Model object to edit the record, not the Field Model. Try something like this:
public function saveRecord($request) {
...
$received = $request->get('received');
$balance = $request->get('balance');
$relProject = $request->get('relProject');
$project = Project_Record_Model::getInstanceById($relProject);
$project->set('mode', 'edit');
$project->set('cf_938', $received);
$project->save();
}
Inside the $get_user and $get_code queries they both have a group_id.
I have dd(); them Both and made 100% sure.
the $get_user query has multiple group_id's and the $get_code only has one group_id which is equal to one of the $get_user group_id's.
The goal at the moment is to create a group_id match query.
Get the code that has a group ID equal to one of the $get_user group_id's
public function getCodesViewQr($code_id)
{
$userid = Auth::id();
$get_user = GroupUser::all()->where('user_id',$userid);
$get_code = Code::all()->where('id',$code_id);
$group_match = GroupUser::where('group_id', $get_code->group_id);
$view['get_users'] = $get_user;
$view['get_codes'] = $get_code;
$view['group_matchs'] = $group_match;
return view('codes.view_qr_code', $view);
}
The group match query does not work. $get_code->group_id does not get the code group_id.
If there is a match then set $match equal to rue. else $match is False
$group_match = GroupUser::where('group_id', $get_code->group_id);
I'm using two Models Code and GroupUser
My Code table is like this :
-id
-group_id (This is the only on important right now)
-code_type
My GroupUser table is like this :
-id
-group_id (This is the only on important right now)
-user_id
-user_role
I have linked the Models
Inside my Code Model I have the relationship to GroupUser
public function group_user()
{
return $this->belongsto('App\GroupUser');
}
And Inside my GroupUser Model I have the relationship to Code
public function code()
{
return $this->belongsto('App\Code');
}
Inside My Code controller I have included my models.
use App\Code;
use App\GroupUser;
Hi guys so I had some help from a guy I work with and this is the solution he came up with. We made a few adjustments. all the Databases and results stayed the same. we just changed the method we used to get the results.
I really appreciate all the help from #linktoahref
public function view_code($random)
{
$code = Code::where('random', $random)->first();
$view['code'] = $code;
if ($code->code_type == 1)
{
// Its a coupon
if (!empty(Auth::user()))
{
// Someones is logged in
$user = Auth::user();
$view['user'] = $user;
$user_groups = GroupUser::where('user_id',$user->id)->pluck('group_id')->toArray();
if (in_array($code->group_id, $user_groups))
{
// The user is an admin of this code
return view('view_codes.coupon_admin', $view);
}else
{
// Save the code to that users account
return view('view_codes.generic_code', $view);
}
}else
{
// Anon
return view('view_codes.coupon_anon', $view);
}
}elseif ($code->code_type == 2)
{
// Voucher..
}else
{
// We don't know how to deal with that code type
}
}
$get_code = Code::find($code_id);
// Check if the code isn't null, else give a fallback to group_id
$group_id = 0;
if (! is_null($get_code)) {
$group_id = $get_code->group_id;
}
$group_match = GroupUser::where('group_id', $group_id)
->get();
$match = FALSE;
if ($group_match->count()) {
$match = TRUE;
}
I an developing a page to create, update, delete and view an event in which there is error while updating the event. There is a event table and a event_collection table. In that event_collection table there is event_id which is id of an event and a collection_id which is from other table collection.
When i create an event, all the data gets stored in event table except the collection one. in the collection table data gets stored in one by one manner like if I check 2 items in collection, it will generate 2 ids with same event_id and 2 collection_ids.
There is problem in update, when i try to update the code, it gives me error as
BadMethodCallException in Macroable.php line 81:
Method update does not exist.
Update method is:
public function update(EventRequest $request, $id)
{
$event = Event::findOrFail($id);
$input = $request->all();
$input['days_of_week'] = serialize(Input::get('days_of_week'));
$query = $event->update($input);
$checkbox_selection = Input::get('agree');
$choosen_checkbox = $id;
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
// return $collection_event;
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
// $collection_id = $id;
foreach($collection_event as $k){
// return $k;
if($k->event_id == $choosen_checkbox){
$data = $request->all();
$data['event_id']= $choosen_checkbox;
$data['collection_id'] = $collection;
$collection_event->update($data);
}
}
}
}
My store method is:
public function store(Request $request)
{
$checkbox = Input::get('days_of_week');
$checkbox_selection = Input::get('agree');
// return $checkbox_collection;
$input = $request->all();
$input['days_of_week'] = serialize($checkbox);
$query = Event::create($input);
$event_id = $query->id;
$pro_id = $query->provider_org_id;
/*For the checkbox selection, if they are multiple store each separately*/
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
$collection_id = $query->id;
if($collection_id){
$data = $request->all();
$data['event_id']= $collection_id;
$data['collection_id'] = $collection;
$create_collection = EventCollection::create($data);
}
}
}
return view('event.pic_upload',compact('event_id','pro_id'));
}
Store method works properly! Can someone please tell any solution? I am badly stucked in this.
I do not think the 'update' method works on collections.
This line will return a collection
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
You do not want a collection, rather a query. As given in the docs:
`App\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);`
Try removing the '->get()' from the statement.
I'm tryin to learn few things about Zend Framework and I got stucked on so simple operation like 'Edit' DB entry.
I've got list of contacts in MySQL db and my intention is to fill form with information from one row, edit it and save it back to db (update statement). I tried almost everthing that came into my mind, checked out google and book about ZF, but there is some problem all the time. At this moment, when I want to do update, zf and mysql will create new db entry with new id and edited information filled in, but that is not what i want to do obviously because instead of one updated entry in DB I've got two - old one and new one with updated information.
Here are the importat parts of my code...please have a look at it, I can't figure out what I'm missing here.
part of indexcontroller:
public function createcontactAction()
{
$createContactForm = $this->_helper->_formLoader('addContact');
$this->view->addContactForm = $createContactForm;
}
public function editcontactAction()
{
$id = $this->getRequest()->getParam('id');
$contactModel = new Application_Model_Contacts();
$contactRow = $contactModel->find($id)->current();
$addContactForm = $this->_helper->formLoader('addContact');
if ($this->getRequest()->isPost() && $this->getRequest()->getPost('send', false) !== false) {
if ($addContactForm->isValid($this->getRequest()->getPost())) {
$contactRow->setFromArray($addContactForm->getValues());
$contactRow->save();
$this->_redirect('/index/editcontact/id/' . $contactRow->id);
}
} else {
$addContactForm->populate($contactRow->toArray());
}
$this->view->addContactForm = $addContactForm;
}
public function savecontactAction()
{
$form = $this->_helper->formLoader('addContact');
if ($this->getRequest()->isPost() && $this->getRequest()->getPost('send', false) !== false) {
if ($form->isValid($this->getRequest()->getPost())) {
$contactModel = new Application_Model_Contacts();
$contactRow = $contactModel->createRow($form->getValues());
$contactRow->save();
$this->_redirect('/index/editcontact/id/' . $contactRow->id);
}
}
$this->view->form = $form;
}
form - parts that matters:
class Application_Form_AddContact extends Zend_Form
{
public function init()
{
$this->setAction('/index/savecontact');
$this->setMethod(Zend_Form::METHOD_POST);
$this->setAttrib('id', 'index_savecontact');
$contactFirstName = new Zend_Form_Element_Text('first_name', array('size'=>32, 'maxlength'=>64, 'label'=>'Křestní', 'required'=>false));
$contactLastName = new Zend_Form_Element_Text('last_name', array('size'=>32, 'maxlength'=>64, 'label'=>'Přímění', 'required'=>true));
.
.
.
$contactNotes = new Zend_Form_Element_Textarea('notes', array('cols'=>32, 'rows'=>1, 'label'=>'Poznámky', 'required'=>false));
$contactSend = new Zend_Form_Element_Submit('send', array('label'=>'Odeslat'));
$this->addElements(array ($contactFirstName, $contactLastName, $contactStreet, $contactHouseNumber, $contactCity, $contactZipCode, $contactCountry,
$contactPhoneNumber, $contactMobileNumber, $contactEmail, $contactWebPage, $contactCrn, $contactVat, $contactNotes, $contactSend));
Thank you very much!
(If theres anything more you could need to help me with this just ask for it)
EDIT:
heres model for contacts:
class Application_Model_Contacts extends Zend_Db_Table_Abstract
{
protected $_name = 'contacts';
protected $_primary = 'id';
}
I'm a bit rusty with regards to Zend_Db_Table and Zend_Db_Table_Row (I'm assuming that is what your model uses), but my bet would be that you are missing the Primary Key (PK) in your $contactRow - I'm guessing you probably don't supply it via the form as I see you get it through GET. So just set the id to $id in your $contactRow and you should be fine.
In editcontactAction(), before $contactRow->save();, add : $contactRow->id = $id. If your row doesn't have a id specified, save() can't update.
You're trying to get an update without providing the id of the row you want to update. The data used for the query is $form->getValues() but the form doesn't seem to contain the id of the contact. Add the id as a hidden field (with the id as the value) to your form or set it separately with $contactRow->id = $id and it should update instead of insert.