I get this error msg Call to a member function addEagerConstraints() on a non-object when I am trying to call a function from my model.
Here is the function:
public static function checkClaimById($claim_id) {
$result = Claim::with('orders')
->find($claim_id)
->where('orders.user_id', Auth::user()->id)
->whereNull('deleted_at')
->count();
if($result >= 1) {
return true;
} else {
return false;
}
}
I have the Model "claim" with has a field order_id and it belongs to an order.
And I have the model "order" and one Order has many claims.
Can someone help me?
thanks
Regards
I have modified my function like this:
public static function checkClaimById($claim_id) {
$result = Claim::with(array('orders' => function($query)
{
$query->where('user_id', Auth::user()->id);
}))
->where('id', $claim_id)
->whereNull('deleted_at')
->count();
if($result >= 1) {
return true;
} else {
return false;
}
}
Found the solution here: http://laravel.com/docs/eloquent#eager-loading
Related
I couldn't do what I wanted to do with Laravel, actually what I want to do is as follows,
I send getTruck id and list truckHistory with witdh, and I want to pull trukInvoice information with truckHistory invoice_id. But i am getting error where am i doing wrong?
Mysql Connection is as follows.
http://localhost:3000/truck/1
"trucks" table column "plate"
"invoice_details" column "plate_no"
"invoice_details" column "invoice_id"
"invoice" column "id"
Truck Controller
public function getTruck($id)
{
$truck = Truck::find($id)->with([
'truckHistory' => function($query){
// return $query->with(['trukInvoice']);
}
])->first();
return $truck;
}
Truck Model
public function companys()
{
return $this->belongsTo(Contact::class, 'company_id', 'id');
}
public function getCompanyNameAttribute()
{
return $this->companys()->first()->name;
}
public function truckHistory(){
return $this->hasMany(InvoiceDetail::class,'plate_no','plate');
}
public function trukInvoice(){
return $this->belongsTo(Invoice::class,'invoice_id','id');
}
Try this:
$truck = Truck::leftJoin('invoices', 'trucks.invoice_id', 'invoices.id')
->leftJoin('invoice_details', 'trucks.plate_no', 'invoice_details.plate')
->select('*')
->first();
or
$truck = Truck::with(['invoices' => function ($query) {
$query->whereHas('invoice_details', function ($q){
$q->whereNotNull('invoice_details.plate');
});
}])->first();
Check the documentation on restraining eager loads.
I'm struggling with Laravel since I'm still a beginner.
In my web.php I have the following route
Route::get('/customize/{id}', function ($id) {
if (User::where('id','=','4')->has('profile'->with('id',$id))->exists() && Profile::where('user_id', $id)->exists()) {
return true;
}
return false;
});
I know something isn't right.
Basically what I want to do is, return true ONLY IF The user with id 4 has a profile with the same id as the one in the url.
In my user modal I have the following relationship :
public function profile()
{
return $this->hasMany(Profile::class);
}
Example of the profiles table:
You've got a typo.
if (User::where('id','=','4')->has('profile'->with('id',$id))->exists() && Profile::where('user_id', $id)->exists()) {
Should be
if (User::where('id','=','4')->has('profile')->with('id',$id)->exists() && Profile::where('user_id', $id)->exists()) {
By doing 'profile'->with(...) you were calling a member function (with(...)) on a string ('profile').
To return true only if the User with id 4 has a profile with id = $id, you want to use whereHas.
if (
User::where('id', 4)->whereHas('profile', function ($query) use ($id) {
return $query->where('id', $id);
})
->exists()
) { ... }
Using PHP ^7.4, this can be written a bit more compactly with shorthand Closures.
if ( User::where('id', 4)->whereHas('profile', fn($query) => $query->where('id', $id))->exists() ) { ... }
By the way, when using where()/orWhere(), if you don't use an operator, it's implied to be '=' so where('id', '=', 4) can be written as just where('id', 4).
I'm refactoring a personal Laravel project and I got blocked in the way of improving it.
The function worked this way:
1. Get a collection from the model Thread
2. A foreach loop checks if a User voted that very thread, adding a custom key->value
3. Return the collection
What I had working up until now, was this:
ThreadController:
$threads = Thread::orderBy('created_at', 'desc')
->with('communities')
->with('author')
->withCount('replies')
->withCount('upvotes')
->withCount('downvotes')
->paginate(4);
foreach ($threads as $thread) {
if (Auth::user()) {
if (Vote::where('user_id', '=', Auth::user()->id)->where('thread_id', '=', $thread->id)->where('vote_type', '=', 1)->exists()) {
$thread->user_has_voted = 'true';
$thread->user_vote_type = 1;
} elseif (Vote::where('user_id', '=', Auth::user()->id)->where('thread_id', '=', $thread->id)->where('vote_type', '=', 0)->exists()) {
$thread->user_has_voted = 'true';
$thread->user_vote_type = 0;
} else {
$thread->user_has_voted = 'false';
}
}
}
return $threads;
What I would like to do is something like this:
Thread Model:
public function userVoteThread() {
if (Vote::where('user_id', '=', Auth::user()->id)
->where('thread_id', '=', $this->id)
->where('vote_type', '=', 1)
->exists()) {
return $this->user_vote_type = 1;
} elseif (Vote::where('user_id', '=', Auth::user()->id)
->where('thread_id', '=', $this->id)
->where('vote_type', '=', 0)
->exists()) {
return $this->user_vote_type = 0;
}
}
ThreadController:
$threads = Thread::orderBy('created_at', 'desc')
->with('communities')
->with('author')
->with('userVoteThread') <----- ADDING NEW MODEL FUNCTION
->withCount('replies')
->withCount('upvotes')
->withCount('downvotes')
->paginate(4);
After all, the closest I got was this error Call to a member function addEagerConstraints() on null, and I'm stuck trying to improve the code.
Is there a way to make that Thread model function work and use it through the collection?
Thank you very much!
PS: I hope I made myself understood, otherwise, ask me about it. Thank you :D
Firstly add relationship to the Thread model.
class Thread {
public function votes() {
return $this->hasMany(Thread::class);
}
}
Add your Eloquent Accessor to the Thread.
class Thread {
public function getUserVoteTypeAttribute() {
$this->votes
->where('user_id', Auth::user()->id ?? -1)
->first()->user_vote_type ?? null;
}
public function getUserHasVotedAttribute() {
return $this->user_vote_type !== null;
}
}
Now you can access these attributes on your model.
$thread = Thread::with('votes')->find(1);
$thread->user_vote_type;
$thread->user_has_voted;
Is that any method to filter the employees and the manpower table record does not return also?
$result = Pwra::with('purchaseOrder', 'manpower')
->where('pwra_dt', $date)
->where('time_session', $session)
->whereHas('manpower.employees', function ($q) {
$q->where('status', 1);
})
->get();
Pwra Class
public function manpower()
{
return $this->hasMany('App\Models\Manpower', 'pwra_uuid', 'pwra_uuid');
}
Manpower Class
public function employee()
{
return $this->hasOne('App\Models\Employees', 'employees_uuid', 'employees_uuid')->where('status', 1);
}
What I expected is: When the employees Status = 0, it will not return any record even manpower.
you can try 2 level whereHas()
$result = Pwra::with('purchaseOrder', 'manpower')
->where('pwra_dt', $date)
->where('time_session', $session)
->whereHas('manpower', function ($q) {
$q->whereHas('employees',function($subQ){
$subQ->where('status', 1);
});
})
->get();
whereHas('manpower.employees' it will not work
I wanna get data which is pwra_dt = $month && $year. What can I do? For now, it shows all records!!
Query:
$purchaseOrder = PurchaseOrder::whereHas('pwra', function (Builder $query) use ($year, $month) {
$query->whereYear('pwra_dt', $year)
->whereMonth('pwra_dt', $month);
})
->get();
PurchaseOrder(Model)
public function pwra()
{
return $this->hasMany('App\Models\Pwra', 'purchase_order_uuid', 'purchase_order_uuid');
}
PWRA (Model)
public function purchaseOrder()
{
return $this->belongsTo('App\Models\PurchaseOrder', 'purchase_order_uuid', 'purchase_order_uuid');
}