Category : relational-database

I have a simple questions but I cant find any answer in internet. class Product extends Model { use HasFactory; public function variants() { return $this->hasMany(Variant::class); } } >>> $p->variants(); => IlluminateDatabaseEloquentRelationsHasMany {#4331} >>> $p->variants; => IlluminateDatabaseEloquentCollection {#4310 all: [ AppModelsVariant {#4319 id: "1", name: "VariantOne", created_at: null, updated_at: null, quantity: "2", product_id: "1", }, ..

Read more

I’m facing against this problem since yesterday. I have a db table called resources have a foreign key linked to another table called category. I’m trying to retrieve the description field in my blade view, but I get this error: Trying to get property ‘description’ of non-object. My blade view: @extends(‘templates.header’) @section(‘section’) <div class="p-10 grid ..

Read more

I’m using Laravel 8 to develop my project and in this project, I have applied OneToMany relationship between User Model and Role Model like this: User.php: public function role() { return $this->belongsTo(User::class); } Role.php: public function users() { return $this->hasMany(Role::class); } Now I want to edit users, so at edit.blade.php, I added this line for ..

Read more

i have column in database named orders, products, and order_product with relationship i want collect data in colums order by search invoice number(not PK id), the problem is how can i get data in products column ? this Order model : use HasFactory; protected $table = ‘orders’; protected $fillable = [ ‘no’, ‘spg’, ‘nama’, ‘hp’, ..

Read more

Looked everywhere but couldn’t find an answer I have a similar query to this: $users = User::with(‘contact’)->get(); This would return something like: { "id":1, "name":"Foo", "contact":[ { "number":"123" }, { "number":"456" }, { "number":"789" } ] } I want to create another query with another relation that would return entries where the number is in ..

Read more

maybe you could help me about my current issue. I have Product that got some has many relations /** Inherits from : category, subcategory and model */ Product->hasMany(CategoryAttributes, $this->category) Product->hasMany(SubCategoryAttributes, $this->subCategory) Product->hasMany(ModelAttributes, $this->model) /** */ Product->hasMany(HideForSubcategoryAttributes, $this->subCategory) Product->hasMany(IgnoreForModelAttributes, $this->model) Now I need to implement something that gives me all attributes for the Product. collect the ..

Read more

I’d like to create a relation for a Group Model, which can have members of different morph types through a pivot table ‘group_members’. And this is what my structure currently looks like: Classes: Class Group { public function Members() { return $this->hasMany(GroupMember::class, ‘group_id’); } // Now here is where i want to return all the ..

Read more

I have two tables as events and organisations. In an organization may have multiple events .In the past i was not familier with laravel so i created Events and Organizations Model. I added foreign key in the migration of events table as $table->bigInteger(‘organization_id’)->unsigned()->default(null)->nullable(); Now i can’t change organization_id to organizations_id because there are lots of ..

Read more

I define the relation in Company table: public function country() { return $this->belongsTo(Country::class, "country_id")->withDefault([‘country’ => ‘unknown’]); } When I use the following code in the controller show function it works: // get name of country $company->country = $company->country()->pluck(‘country’); But if I use the same code in the index function in a loop, I get an ..

Read more