RelationNotFoundException in RelationNotFoundException.php - laravel

each product hasMany property.
when I use with function:
dd(Product::with(ProductProperty::class)->get());
I got this error :
RelationNotFoundException in RelationNotFoundException.php
Call to undefined relationship [App\Models\ProductProperty] on model [App\Models\Product].
class Product extends Model
{
protected $table = 'products';
protected $fillable = [
'user_id' ,'brand_id' , 'title', 'price', 'current_buy','max_buy','min_buy_per_bill',
'max_buy_per_bill','count','off','seri','short_description','long_description',
];
public function ProductProperty()
{
return $this->hasMany('App\Models\ProductProperty');
}
}
class ProductProperty extends Model
{
protected $table = 'products_properties';
protected $fillable = [
'product_id' ,'parent_id' , 'title','value', 'price', 'current_buy','max_buy','min_buy_per_bill',
'max_buy_per_bill','count','off','seri','short_description','long_description',
];
public function Product()
{
return $this->belongsTo('App\Models\Product');
}
}

Looking at your code you can't use ::class with the with() function. The main reason is the ::class will return the full path with namespace.
Product::with(ProductProperty::class)->get(); is incorrect.
Replace it with Product::with('ProductProperty')->get();

Related

Laravel TypeError in many to many relationship

I have 3 models: Order, Products, OrderProducts:
class Order extends Model
{
use HasFactory;
protected $fillable = [
'name',
];
public function products(){
return $this->belongsToMany(OrderProduct::class);
}
}
class Products extends Model
{
use HasFactory;
protected $fillable = [
'name', 'link', 'buyprice', 'sellprice','weight',
];
public function orders(){
return $this->belongsToMany(OrderProduct::class);
}
}
class OrderProduct extends Model
{
use HasFactory;
protected $fillable = [
'order_id', 'product_id',
];
public function orders() {
return $this->belongsToMany(Order::class);
}
public function products() {
return $this->belongsToMany(Products::class);
}
}
When i view a specific Order i want to display all associated products, which i try to retrieve like this in my controller:
public function show($id)
{
//$products = Products::all();
$order = Order::where('id',$id)->first();
$orderList = $order->products();
return $orderList;
}
The error i get it:
TypeError
Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in D:\laravel\pricing\vendor\laravel\framework\src\Illuminate\Http\Response.php on line 72
How can i get collection of all products for a specific order?

Search in Laravel relation using json column

I have problem with search in json column.
I have 3 relations:
products
feature products
feature_values
My tables:
products: https://ibb.co/1dgjT6m
feature products: https://ibb.co/K9f74Wn
feature_values: https://ibb.co/rc3zG5W
My migrations:
Product:
class Product extends Model implements Presentable
{
....
public function featureProducts()
{
return $this->hasMany(FeatureProduct::class, 'product_id');
//return $this->belongsToMany(Feature::class, 'feature_product', 'id');
}
}
FeatureProduct
class FeatureProduct extends Model
{
protected $table = "feature_product";
protected $with = [
'values'
];
public function values()
{
return $this->belongsTo(FeatureValue::class, 'feature_values_ids', 'id');
}
}
FeatureValues:
class FeatureValue extends Model
{
use SoftDeletes,
Translatable;
protected $fillable = [
'feature_id',
'value'
];
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
protected $translatable = [
'value'
];
public function feature(): BelongsTo
{
return $this->belongsTo(Feature::class);
}
public function getAdminUrlAttribute(): AbstractAdminUrlPresenter
{
return new FeatureValueUrlPresenter($this);
}
}
I need to show products with features and assigned features_values
I heve problem with search in json column: feature_product. feature_values_ids
When I have INT in this column, then this is working fine:
public function values()
{
return $this->belongsTo(FeatureValue::class, 'feature_values_ids', 'id');
}
When I have:
["1", "2","3"]
I haven't any results :(
Haw can I repair it?

Trying to get property 'max_rating' of non-object in Laravel

I have these three models in Laravel-5.8
GoalType
class GoalType extends Model
{
protected $table = 'goal_types';
protected $fillable = [
'id',
'name',
];
public function goals()
{
return $this->hasMany('App\Models\Goal');
}
public function ratinglimit()
{
return $this->belongsTo(App\Models\RatingLimit);
}
}
RatingLimit
class RatingLimit extends Model
{
protected $table = 'rating_limits';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'max_rating',
];
public function goaltype()
{
return $this->hasOne('App\Models\GoalType','goal_type_id','id');
}
}
Goal
class Goal extends Model
{
protected $table = 'goals';
protected $fillable = [
'id',
'goal_type_id',
'goal_title',
];
public function goaltype()
{
return $this->belongsTo('App\Models\GoalType','goal_type_id');
}
}
Controller
$goals = Goal::where('is_approved', 3)->get();
A RatingLimit is associated with one GoalType (hasOne) and GoalType has many Goal
When I tried to render max_rating
index.blade
{{$goal->goaltype->ratinglimit->max_rating}}
I got this error:
Trying to get property 'max_rating' of non-object
When I changed it to:
{{ $goal->goaltype->ratinglimit ? $goal->goaltype->ratinglimit->max_rating : '-' }}
it deisplays '-', and the error is no more there. But max_rating in the database is not empty
How do I resolve this?
Thanks
->get() returns a collection! so you should loop it through foreach to have access to each item and then you can use your relation:
#foreach ($goals as $g)
{{$goal->goaltype->ratinglimit->max_rating}}
#endforeach

Accessing attribute from different model in Laravel

Please take a look at this screenshot
that is the result from this
salesItem product_id = {{ $salesItem->product_id }}
and this
wh2summary ={{$salesItem->wh2sum}}
How can I access the qty_in in the long result from wh2sum? I can see the qty_in but if i do this {{$salesItem->wh2sum->qty_in}}
i get this error
my Salesitems model
protected $table = 'sales_items';
protected $fillable = [
'product_id',
'sales_id',
'product_name',
'product_code',
'price',
'rate',
'quantity',
'total_price',
];
public function wh2sum()
{
return $this->hasMany('App\Warehouse2StockSummaries', 'product_id', 'product_id');
}
my wh2sum model
class Warehouse2StockSummaries extends Model
{
protected $table = 'warehouse2_summary';
protected $fillable = [
'product_id',
'qty_in',
'qty_out'
];
public function product()
{
return $this->hasOne('App\Products', 'id','product_id');
}
}
for my products model
class Products extends Model
{
// use SoftDeletes;
protected $fillable = [
'product_code',
'name',
'categories_id',
'wh1_limit_warning',
'wh2_limit_warning',
'price',
'selling_price',
'user_id'
];
public function warehouse2StockSummary()
{
return $this->hasMany('App\Warehouse2StockSummaries', 'id', 'product_id');
}
public function salesItem()
{
return $this->hasMany('App\Purchaseitems');
}
please help and thank you in advance!

laravel eloquent doesn't use protected table name

In my model i added protected $table, but when i'm going to use it laravel does't use it. This is my role models:
class Role extends Model
{
protected $table = 'role';
protected $primaryKey = 'ROLE_ID';
protected $casts = [
'ACTIVE' => 'boolean',
];
protected $fillable = [
'ROLE', 'ACTIVE', 'TYPE'
];
public $timestamps = false;
public function groups()
{
return $this->belongsToMany(Group::class, GroupRole::class, 'ROLE_ID', 'GROUP_ID');
}
}
And this is Group model:
class Group extends Model
{
protected $table = 'groups';
protected $primaryKey = 'GROUP_ID';
protected $fillable = [
'GROUP_ID', 'GROUP_NAME', 'PARENT_GROUP', 'ACTIVE'
];
protected $casts = [
'ACTIVE' => 'boolean',
];
public $timestamps = false;
public function type()
{
return $this->belongsTo(GroupType::class, 'TYPE', 'TYPE_ID');
}
public function roles()
{
return $this->belongsToMany(Role::class, GroupRole::class, 'GROUP_ID', 'ROLE_ID');
}
}
And this is group_role table model. It handles many to many relation between role and group:
class GroupRole extends Model
{
protected $table = 'group_role';
protected $primaryKey = 'GROUP_ROLE_ID';
protected $fillable = [
'COMMENT', 'ROLE_ID', 'GROUP_ID'
];
public $timestamps = false;
}
Problem begin when i want to use this models. For example:
$role = App\Role::first();
$groups = $role->groups;
Laravel returns this error messages:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'favian_mydb.App\GroupRole' doesn't exist (SQL: select groups.*, App\GroupRole.ROLE_ID as pivot_ROLE_ID, App\GroupRole.GROUP_ID as pivot_GROUP_ID from groups inner join App\GroupRole on groups.GROUP_ID = App\GroupRole.GROUP_ID where App\GroupRole.ROLE_ID = 1)
I tried to replace App\GroupRole with group_role and executing in mysql. It works fine. Am i missing something?
The Problem is in your roles relation:
public function roles()
{
return $this->belongsToMany(Role::class, GroupRole::class,'GROUP_ID','ROLE_ID');
}
The belongsToMany expects the intermediate table name as second argument, not the class name.
So you have to define it like this:
public function roles()
{
return $this->belongsToMany(Role::class, 'group_role','GROUP_ID','ROLE_ID');
}
I think the problem is in you relation functions. Try to use strings instead of Model::class.
Example:
return $this->return $this->belongsTo('App\GroupType', 'TYPE', 'TYPE_ID');
Hope this works.

Resources