How to fix Call to a member function where() on int - php

I have to update some records but it doesn't work properly.
Someone know what to do?
$dog = DB::table('dogs')
->update($data)
->where('id', $id);
return response()->json($dog);
})

Call where before update, e.g. :
$dog = DB::table('dogs')->where('id', $id)->update($data);

Related

Error Method Illuminate\\Database\\Eloquent\\Collection::save does not exist. in Laravel

I want to change the status at $ result, to get the data at $ result I use query builder, but there is an error like that
$results = ClientVendor::where('client_id','=', $request->client_id)
->where('vendor_id','=',$request->vendor_id)
->get();
$results->status = $request->status;
$results->save();
return response()->json($results);
You cant do this because you call whole collection where is many elements. Call just single record, then you can update it.
When you use get() you call collection
When you use first() or find($id) then you get single record that you can update.
Look at example:
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first(); // this point is the most important to change
$results->status = $request->status;
$results->save();
return response()->json($results);;
Good luck!
You can try this one too.
$results = ClientVendor::where('client_id','=', $request->client_id)
->where('vendor_id','=',$request->vendor_id)
->update([
'status' => $request->status
]);
Try this:
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$results->status = $request->status;
$results->save();
return response()->json($results);
It depends on your needs, if you want to :
Get and update one record, you should use first() or firstOrFail() instead of get(). Should be look like this :
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$results->status = $request->status;
$results->save();
return response()->json($results);
Get and update multiple records, yes you can use get(), but you should do foreach and then update the single record one by one. just like this :
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->get();
foreach($results as $result){
$result->status = $request->status;
$result->save();
}
return response()->json($results);
Many have suggested using first() instead of get(). I think fistOrFail() would be a better option to handle null results.
Otherwise, in cases where the result is null, you'd get Call to a member function save() on null error.
Eg:
ClientVendor does not have a record with either client_id or vendor_id matching $request->client_id or $request->client_id respectively.
$result = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$result->status = $request->status; //produces error Creating default object from empty value
$result->save(); //produces error "Call to a member function save() on null" because $result will be empty
The above code produces 2 exceptions: 1. "Creating default object from empty value" on lines $result->sataus = .. and 2."Call to a member function save() on null" on line $result->save because $result will be empty.
To fix that, you would call firstOrFail() like so.
$result = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->firstOrFail();
$result->status = $request->status;
$result->save();
This fix, firstOrFail, when there are no results would produce a 404, a handled error, and the subsequent lines would not be executed.
For situations where you query using find($id), it is better to use findOrFail($id)
If you need to update one item use find with it's primary key & then update.Or without primary key then use where & first.then update as like this Just Use
$results = ClientVendor::where('client_id', $request->client_id)
->where('vendor_id',$request->vendor_id)
->first();
$results->update([
'status'=>$request->status
]);
return response()->json($results);

Laravel hasMany to Belongs to returns undefined

I know this has been asked before but specific to my case I could't find an answer that worked.
Currently I have two models.
App\JobStatus
App\Customer
In model App\JobStatus I have this:
public function customer()
{
return $this->belongsTo('App\Customer');
}
In model App\Customer I have this:
public function jobs()
{
return $this->hasMany('App\JobStatus', 'customer_id');
}
'customer_id' is the foreign key. I then try to access customer from Jobstatus in my controller like so:
$testMePlease = JobStatus::first()->where('qb', '=', 1);
$testMePlease->customer;
I have attempted to dd this. To put it in foreach loop. I've also tried $testMePlease->customer->customer_name. Customer_name being a column in the table and I get back the same error: "Undefined property: Illuminate\Database\Eloquent\Builder::$customer"
Any ideas what I'm doing wrong?
Try to change
$testMePlease = JobStatus::first()->where('qb', '=', 1);
To
$testMePlease = JobStatus::where('qb', '=', 1)->first();

json response with 2 table in Laravel 5.6

why my code get error
public function AuditorBagian_Edit($nopek)
{
$user = User::where('nopek', '=', $nopek)->get();
$bagian_user = Bagian::all()->where('kode_bagian', '=', $user->bagian)->get();
return response()->json($bagian_user);
}
I want to show data from Bagian
You can pass collection or array into response()->json() function it will convert as JSON data
public function AuditorBagian_Edit($nopek)
{
$user = User::where('nopek', '=', $nopek)->get();
$bagian_user = Bagian::where('kode_bagian', '=', $user->bagian)->get();
// or $bagian_user = Bagian::where('kode_bagian', '=', $user->bagian)->get()->toArray();
return response()->json($bagian_user);
}
Error result of code
$bagian_user = Bagian::all()->where('kode_bagian', '=', $user->bagian)->get();
Bagian::all() return instance of Illuminate\Database\Eloquent\Collection and find all records in db, then you try to filter ->where('kode_bagian', '=', $user->bagian)->get() specific records but this code wrong because method where() of Illuminate\Database\Eloquent\Collection class return instance of Illuminate\Database\Eloquent\Collection and this class does not haveget() method.
User::where('nopek', '=', $nopek)->get() also return instance of Illuminate\Database\Eloquent\Collection. To get single record use first() method instead of get()
The correct way get result is
$user = User::where('nopek', '=', $nopek)->first();
if(!empthy($user)) {
$bagian_user = Bagian::where('kode_bagian', '=', $user->bagian)->get().
}
Edited, format php code
Just remove the ::all() will do, all() and get() is the same behaviour.
Take not that all(), get(), first() is the final step to get the model data, the condition and with() and ordering() and etc must happen before all the three above mentioned

Sorting users through relation in laravel

i want to sort the users through voornaam(firstname). but im getting the data via a relation.
How do i make my query so that, the relation users are sorted by firstname by alphabet
my function:
public function sortfirstname($id) {
$ingeschrevenspelers = UserToernooi::with('users')->where('toernooiid', '=', $id)->get()->all();
//This query ^^
$toernooi = Toernooi::findOrFail($id);
dd($ingeschrevenspelers);
return view('adminfeatures.generatespelerslijst', compact('ingeschrevenspelers', 'toernooi'));
}
What i want to sort
any help is appreciated
thanks in advance
Writing code in your own language doesn't make it very easy for other developers to understand your code.
That being said, you can try the orderBy() method on your relationship
In your model where you define the relationship:
public function relationship()
{
return $this->belongsTo(SomeClass::class)->orderBy('name', 'DESC');
}
Don't fire all() function at the end thus obtaining a Collection instance of result
//query without the all function
$ingeschrevenspelers = UserToernooi::with('users')->where('toernooiid', '=', $id)->get();
//
$ingeschrevenspelers = $ingeschrevenspelers->sortBy('users.firstname');
An alternative to Jordy Groote's answer if you do not want to modify the Model class itself, you can query it with a closure.
$ingeschrevenspelers = UserToernooi::with(['users' => function($q) {
$q->orderBy('voornaam', 'asc');
}])->where('toernooiid', '=', $id)->get()->all();
Reference: https://laravel.com/docs/5.3/eloquent-relationships#constraining-eager-loads
Sidenote: I don't think you need a ->all() when you already did a ->get()
$ingeschrevenspelers = UserToernooi::with(['users' => function($query){
$query->orderBy('voornaam', 'asc');
}])->where('toernooiid', '=', $id)->get()->all();

orderBy on whereHas query in Laravel 4.2

how can I sort the return data from a query using whereHas? In my query i have to get the users data which id exist on interest table but i need to sort it using the datetime column from interest. But, the return query do not sort the data at all.
Here's my code snippets in case it would help you understand my problem.
Model (name: User)
//***relationship***//
public function interest(){
return $this->belongsTo('Interest','id','interest_by');
}
Controller
$userInterested = User::whereHas('interest',function($q) use ($id) {
return $q->where('interest_on', $id)
->orderBy('datetime');
});
$userQuery = $userInterested->get();
return $userQuery;
remove return from where has and try this.like
$userInterested = User::whereHas('interest',function($q) use ($id) {
$q->where('interest_on', $id)
->orderBy('datetime');
})->get();
return $userInterested;
$userInterested = User::whereHas('interest',function($q) use ($id) {
return $q->where('interest_on', $id);
})->orderBy('datetime');
$userQuery = $userInterested->get();
return $userQuery;

Categories