Property does not exist on the Eloquent builder instance - php

I have an events table and a tickets table. I am trying to decrement the available tickets in regular_atendies by the ordered tickets and same for vip
This is my store method:
// Create Ticket
$ticket = new Ticket();
$ticket->userName = $request->input('userName');
$ticket->userEmail = $request->input('userEmail');
$ticket->phoneNumber = $request->input('phoneNumber');
$ticket->regular_quantity = $request->input('regular_quantity');
$ticket->vip_quantity = $request->input('vip_quantity');
$ticket->event_id = $request->route('id');
$ticket->total = $request->input('regular_quantity') + $request->input('vip_quantity');
$event = Event::find(1);
$event = Event::where('id',$ticket->event_id);
if ($ticket->regular_quantity<$event->regular_attendies) {
DB::table('events')->decrement('regular_attendies', $ticket->regular_quantity);
} elseif ($ticket->vip_quantity<$event->$vip_attendies) {
DB::table('events')->decrement('vip_attendies', $ticket->vip_quantity);
} else {
echo "error";
}
$ticket->save();
return redirect('/');

You never execute your query. This line:
$event = Event::where('id',$ticket->event_id);
Needs a closure:
$event = Event::where('id',$ticket->event_id)->first();
// or ->firstOrFail();, etc
Otherwise, it's a Builder instance, and you can't access columns from a Builder. Using ->first() will convert it to an Event instance, or null.
Also, having this:
$event = Event::find(1);
// ^ I assume this is used for testing, probably should comment it out.
$event = Event::where('id',$ticket->event_id)->first();
Is redundant. Use one or the other.

Related

Update multiple ids against single id (Laravel)

I want to update multiple Departments against one unit. I tried this method, but it's not correct.
How can I update multiple departments ids?
Form:
Request:
Controller Function:
$pre_data = UnitDepartment::where('unit_id', $request->id)->get();
if ($pre_data) {
foreach ($pre_data as $value) {
$value->delete();
}
$department = $request->department_id;
foreach ($department as $value) {
$unitDepart = new UnitDepartment();
$unitDepart->unit_id = $request->id;
$unitDepart->department_id = $value;
$unitDepart->save();
}
}
table:
I found that is the table related to departments and units.
So you can build the relationship many-to-many between them,
Create the relationship in your models,
In Unit model:
public function departments()
{
return $this->belongsToMany('App\Unit','unit_department','unit_id','department_id');
}
In Department Model:
public function units()
{
return $this->belongsToMany('App\Department','unit_department','department_id','unit_id');
}
Attach the new relationship, simply use:
Unit::find($request->unit_id)->departments()
->sync($request->department_id);
Unfortunately, you cannot use softDelete on sync().
And I don't think you need to soft delete with unit_departments. As a pivot then it should be irrelevant if it is deleted or not.
And if user update the relationship on the frequent, this table will grow fast.
If you really need to soft-delete, you can write it like this:
$department_ids = $request->department_id;
$unit_id = $request->unit_id
// soft delete the unit_departments not in request:
UnitDepartment::where('unit_id', $unit_id)->whereNotIn('department_id', $department_ids)->delete();
// insert the new department_id+unit_id relationship
$exist_department_ids = UnitDepartment::where('unit_id', $unit_id)->whereIn('department_id', $department_ids)->pluck('department_ids')->all();
$dept_ids = array_diff($exist_department_ids, $department_ids);
$depts = collect($dept_ids)->map(function($dept_id) use ($unit_id) {
return ['department_id' => $dept_id, 'unit_id' => $unit_id];
});
UnitDepartment::insert($depts);
the problem is you're sending unit_id in the request, however using $request->id in the query which is wrong.
Change every occurance of $request->id with $request->unit_id in the controller.
to select pre data correctly
use
$pre_data = UnitDepartment::where('unit_id', $request->id)->first();
i tried this
$unit = UnitDepartment::where('unit_id', $request->unit_id)->get();
foreach ($unit as $item) {
$existDepartment[] = $item->department_id;
}
$newDepartment = $request->department_id;
$result = array_diff($newDepartment, $existDepartment);
if ($result) {
foreach ($result as $item) {
$data = new UnitDepartment();
$data->unit_id = $request->unit_id;
$data->department_id = $item;
$data->save();
}
}

Updating a row in Laravel 5.7 doesn't work, how do I fix it?

I'm trying to update a row in mysql table. However, when I click the register button, it doesn't do anything. I'm using Laravel.
Here's my update function:
public function update(Request $request)
{
$idpawn = $request['idprestamo'];
$paynumber = $request['numeropago'];
$payqty = $request['payqty'];
$statuspawns = statuspawns::find($idpawn,$paynumber);
$updateqty = $statuspawns->totalpayment - $payqty;
if($updateqty == "0"){
$status = "Pay";
}
else{
$status = "Partial Payment";
}
$statuspawns->total = $updateqty;
$statuspawns->status = $status;
$statuspawns->save();
return redirect()->back();
}
Thanks for your help.
I think the problem might be that you need square brackets instead of parenthesis since it's an array. $request['idprestamo']
$request['numeropago']
$request['payqty']
I'd like to also add that you can do it this way also with a magic method...
$request->numeropago
You have to access the Request this way $request->input_name not this way $request('input_name') because it is a Laravel Data Collection
In the end of the day, your function will look like this:
public function update(Request $request)
{
$idpawn = $request->idprestamo;
$paynumber = $request->numeropago;
$payqty = $request->payqty;
$statuspawns = statuspawns::find($idpawn,$paynumber);
$updateqty = $statuspawns->totalpayment - $payqty;
if($updateqty == "0"){
$status = "Pay";
}
else{
$status = "Partial Payment";
}
$statuspawns->total = $updateqty;
$statuspawns->status = $status;
$statuspawns->save();
return redirect()->back();
}
UPDATE
In Laravel 5.7 you have to use the update() method instead of the save() method to update a row.
You are not retrieving the record correctly. you have to pass only id to find(). As you have passed two parameters.
change this statement to..
$statuspawns = statuspawns::find($idpawn,$paynumber);
To
$statuspawns = statuspawns::find($idpawn);
Now you can update the columns which you want to update.

BadMethodCallException in Macroable.php line 81:Method update does not exist

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.

laravel: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

I have 3 DB tables and I did not put any relationship on any of them. then I wrote the following code:
public function store($key)
{
$user_key = md5(Input::get('email') . "-" . strtotime('now'));
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->user_key = $user_key;
$user->password = Hash::make(Input::get('password'));
$user->apipass = md5(Input::get('password'));
$user->save();
$newUid = $user->id;
//check key for share
$invited = DB::table('invites')->where('invite_key', Input::get('invite_key'))->where('status', '0')->count();
if($invited == 1) {
$inviter_id = DB::table('invites')->where('invite_key', Input::get('invite_key'))->where('status', '0')->pluck('user_id');
$read_only = DB::table('invites')->where('invite_key', Input::get('invite_key'))->where('status', '0')->pluck('read_only');
$share = new RecordShare;
$share->user_id = $inviter_id;
$share->shared_with_id = $newUid;
$share->read_only = $read_only;
$share->save;
}
return Redirect::to('login');
}
its supposed create a new user, then see who invited him, and then create a share record with the inviter.
but when I test it, I got the error:
LogicException
Relationship method must return an object of type
Illuminate\Database\Eloquent\Relations\Relation
open: /home/oneinfin/public_html/dialysis/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
*/
protected function getRelationshipFromMethod($key, $camelKey)
{
$relations = $this->$camelKey();
if ( ! $relations instanceof Relation)
{
throw new LogicException('Relationship method must return an object of type '
. 'Illuminate\Database\Eloquent\Relations\Relation');
}
I thought there was something wrong with my data and so tried to create an empty record
$share = new RecordShare;
$share->save;
but this also failed with the same error. The function only gets pass if I remove this part totally.
what could be wrong? I tried to clear the cache, but still don't work.
I think change
$share->save;
to
$share->save();

How to submit data to multiple tables in the same form

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.

Categories