Too few arguments to function Illuminate\Database\Eloquent\Builder - php

I have 2 model one is task and another is order. I have used here one to many relation. Here is my code
public function orders(){
return $this->hasMany(Order::where('service_id', null), 'offerid');
}
for task
public function task()
{
return $this->belongsTo(Task::class, 'id');
}
I have to fetch only those table who have service_id null. when i make like this then showing error Too few arguments to function Illuminate\Database\Eloquent\Builder.
$tasks = Task::where('sub_category', $id)->with('serimages', 'serSignleImg', 'user', 'orders')->get();
This is the query where i fetching all data. I have pass them through api

You have error in your relation method
public function orders(){
return $this->hasMany(Order::class, 'offerid')->whereNull('offerid');
}
or you can do like this
public function orders(){
return $this->hasMany(Order::class, 'offerid');
}
$tasks = Task::where(['sub_category', $id)->with('serimages', 'serSignleImg', 'user', 'orders'=>function($query){$query->whereNull('offerid')}])->get();

Related

Laravel many to many relation not working as expected

this might be a silly question, but I am trying to connect 1 table to 2 related tables.
Inside the values array component_id 1 shows up when requesting data for component with id 2.
The relation inside Component::class
public function fields()
{
return $this->hasManyThrough(Field::class, FieldValue::class, 'component_id', 'id', 'id', 'field_id');
}
Maybe you make mistake, try determine relationships like that:
public function fields()
{
return $this->hasManyThrough(Field::class, FieldValue::class);
}
OR
public function fields()
{
return $this->hasManyThrough(Field::class, FieldValue::class, 'component_id', 'field_id', 'id', 'id');
}
And of course, check correct your code reading official documentation how to use this relationship.
Component::
public function fields()
{
return $this->hasMany(Field::class);
}
public function values()
{
return $this->hasManyThrough(Field::class, FieldValue::class);
}
Field:: (table) id,component_id
public function component()
{
return $this->belongsTo(Component::class);
}
public function values()
{
return $this->hasMany(FieldValue::class);
}
FieldValue:: (table)id,field_id
public function field()
{
return $this->belongsTo(Field::class);
}

Laravel Eloquent. I try to intersect two intermediate table. Is there any query more efficient than this?

Laravel Eloquent. I try to intersect two intermediate table. Is there any query more efficient than this ?
Unit Model :
public function products()
{
return $this->belongsToMany('App\Product');
}
public function users()
{
return $this->belongsToMany('App\User');
}
Product Model :
public function units()
{
return $this->belongsToMany('App\Unit');
}
public function users()
{
return $this->belongsToMany('App\User');
}
User Model :
public function units()
{
return $this->belongsToMany('App\Unit');
}
public function products()
{
return $this->belongsToMany('App\Product');
}
Query Eloquent :
Product::get()
->filter(function ($product) {
return $product->units
->pluck('id')
->intersect(auth()->user()->units->pluck('id'))
->isNotEmpty();
})
I try to retrieve all product where product unit is equal user login unit
UPDATE
I think code below is more clean
Product::whereHas('units', function (Builder $query) {
$query->where([
'units.id' => auth()->user()->units->pluck('id'),
]);
})->get();
I think your solutions looks optimal! It looks like a way to compare attributes from two pivot tables in the way
Product::with('users')
->with('units')
->compareAttributesFromPivotTables('attribute_a','attribute_b')
->get();
isn't yet implemented.

Accessing a hasMany relation function via Collection

I am trying to get all of the users notifications, and depending on if the user is a buyer or seller (can be both). I have made two functions in my notifications table to filter each other out.
My goal is to ultimately run:
$notifications = Auth::user()->notifications()->getBuyerNotifications();
or
$notifications = Auth::user()->notifications()->getSellerNotifications();
I am running into an issue: Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany
User Model:
public function notifications() {
return $this->hasMany('App\Notification', 'user_id', 'id');
}
Notifications Model:
public function user() {
return $this->belongsTo('App\User', 'id', 'user_id');
}
public static function getBuyerNotifications() {
return self::whereNotNull('buyer_id')
->whereNull('deleted_at')
->get();
}
public static function getSellerNotifications() {
return $this->whereNotNull('seller_id')
->whereNull('deleted_at')
->get();
}
The command I want to run to get all of the users notifications if they're a buyer: $notifications = Auth::user()->notifications()->getBuyerNotifications();
Firstly, you don't need to use whereNull('deleted_at'), you can import the softDeletes Trait in your model:
use Illuminate\Database\Eloquent\SoftDeletes;
...
class Notification extends Model {
use SoftDeletes;
...
}
Laravel will automatically use whereNull('deleted_at') on Eloquent-Builder.
Secondly, you cannot use static method on Illuminate\Database\Eloquent\Relations\HasMany.
Use scope method instead:
public function scopeBuyerNotifications($query) {
return $query->whereNotNull('buyer_id');
}
public function scopeSellerNotifications($query) {
return $query->whereNotNull('seller_id');
}
So you can find the notification like this:
$notifications = Auth::user()->notifications()->sellerNotifications()->get();
$notifications = Auth::user()->notifications()->buyerNotifications()->get();
Auth::user() uses session data.
Try this:
optional(User::find(Auth::id())->notifications)->getBuyerNotifications;
or
$userId = 1; // Example id you can just pass the user Id.
User::find($userId)->notifications->getBuyerNotifications;
You can add two other methods in user model as follows
public function getBuyerNotifications() {
return $this->hasMany('App\Notification', 'buyer_id', 'id');
}
public function getSellerNotifications() {
return $this->hasMany('App\Notification', 'seller_id', 'id');
}
And you can call it directly from the user instance
$user->getBuyerNotifications();
$user->getSellerNotifications();

getRouteByKeyName another model laravel

I have two models:
Categories
CategoriesTranslations
In model categories I writed:
public function getRouteKeyName(){
return 'alias';
}
Column alias belogns model CategoriesTranslations.
How I can return alias from model CategoriesTranslations?
My code is not working.
I tryed:
Add getRouteKeyName on CategoriesTranslations:
public function getRouteKeyName(){
return 'alias';
}
And in Categories add:
public function categoryTranslations() {
return $this->hasOne(CategoriesTranslations::class);
}
public function getRouteKeyName() {
return $this->categoryTranslations->getRouteKeyName();
}
I get error Call to a member function getRouteKeyName() on null When want show category by getRouteKeyName. How fix it?
You must use relationships between Categories and CategoriesTranslations.
In your Categories model:
public function categoriesTranslations()
{
return $this->hasMany(CategoriesTranslations::class);
}
public function getRouteKeyName()
{
$this->categoryTranslations->where('locale', Auth::user()->locale)->first()->getRouteKeyName();
}
Now you can call getRouteKeyName() from any instance of the Categories model and it will pull it from the CategoriesTranslations table.
** The potential fix here was adding in the where() statement and the first() method. Change the Auth::user()->locale to wherever you are storing which locale to use.

Query with two laravel models and return result

I have two models, User and Post
User Model:
public function posts()
{
return $this->hasMany('App\Post');
}
Post Model:
public function user()
{
return $this->belongsTo('App\User');
}
In my controller I have a public function which has:
$users = User::orderBy('is_ban', 'desc')->paginate(10);
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
Which is working as expected.
I also have one column in users table `is_ban' It's of boolean type.
I am looking for a query which will return the following:
Only get post which has been made by the user which has is_ban=false
perhaps i haven't understood you, but i hope it will help. You can add it to your Post model
public function getBannedUsersPosts()
{
return self::whereIn('user_id', User::where('is_ban', 0)->pluck('id'))->get();
}

Categories