Category : relationship

I have a page controller which is used to control what template/view is displayed based on the URI requested. If the URI matches the ‘slug’ field in the Database then the controller serves up the related template/view that is associated with that page. There are separate Pages Model and Templates Model that are related via ..

Read more

Assuming there is application where Users can be informed of their status of pending or complete actions, what would be the best way to solve this. For example, prompting the users to upload a profile picture if they have not done so and/or notifiying them the action is complete. One way would be to use ..

Read more

.Hi, i’m encountering a problem with a script. i’m working with Laravel 6.x (but there is no big difference). So, i have a model Order, whic has relationship with another model Group like this : Model Order : public function groups() { return $this->hasMany(‘AppModelsOrderGroup’, ‘order_id’)->orderBy(‘order_group’); } Model Group : public function order() { return $this->belongsTo(‘AppModelsOrderOrder’, ..

Read more

I’m trying to get a model an his relations "filtered" by id and I didn’t succeed to achieve that. My db looks like : operations id | name | desc ———————————— 1 | operation system | operate sys items id | name | desc ———————————– 4 | maintenance | serv maint processes id | cat ..

Read more

I have a product database. I created tables with similar products. A relationship should have a multi-product to multi-product ratio. I created the product__similar table: product_id (FK) similar_id (FK). How can I create a relationship so that links are formed in both directions? I created the relationship: public function productSimilar() { return $this->belongsToMany(Product::class, ‘product__product_similar’, ‘product_id’, ..

Read more

i have manytomany relation ship between categroy and product category model class Attribute extends Model implements Auditable { use HasFactory, AuditableTrait; protected $fillable = [‘category’,’sub_categ’]; public function products(): BelongsToMany { return $this->belongsToMany(Product::class); } } product model class Product extends Model implements Auditable { use HasFactory, AuditableTrait; protected $table = ‘products’; protected $fillable = [‘name’,’price’,’description’, ‘details’]; ..

Read more

I have pivot table vacancy_tag. I tried different options (value,select, lists and etc), but didn’t go further than that. My request: Vacancy::where(‘id’,1)->with([‘tags’ => function ($q){ $q->select(‘tags.id’)->pluck(‘id’); }])->get()->toArray(); return this: … "tags" => array:2 [▼ 0 => array:1 [▼ "id" => 1 ] 1 => array:1 [▼ "id" => 2 ] … but need this: "tags" ..

Read more

i need run ->unique(‘model_column’) from a eager loader collection. How can i run that without use a foreach? I need generate the final collection to use it in a json_encode axios call. If i call ->unique(‘model_column’) from the model relationship method it crash, if i run them from the controller, no crash but nothing happens, ..

Read more

So I’m trying to have plans associated to the current user. I have modal and migration called Plan. I have a method called user that I use the belongsTo() to get all the users that’s associated to that plan. I’m not sure why its not working Modal namespace AppModels; use AppModelsUser; use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentFactoriesHasFactory; ..

Read more

I my laravel (7.x) application. I have to models User and UserReferral with hasMany relationships. User.php class User extends Authenticate implements MustVerifyEmail { public function referrals() { return $this ->hasMany(UserReferral::class) ->orderBy(‘created_at’, ‘DESC’) ->skip(0) ->take(10); } } UserReferral.php class UserReferral extends Model { public function user() { return $this->belongsTo(User::class); } } The list of Referrals has ..

Read more

I use Laravel 8. I want to get the user proccess course, but I get all users proccess. In User.php public function courses() { return $this->belongsToMany(Course::class); } Course.php public function progresses() { return $this->hasMany(Progress::class); } Progress.php is empty. Course table Schema::create(‘courses’, function (Blueprint $table) { $table->id(); $table->string(‘title’); … $table->timestamps(); }); Progress table Schema::create(‘progress’, function (Blueprint ..

Read more

I would like to merge 2 relationships into one query from 2 different models class User extends Authenticatable { public function relazioneFollower() { return $this->belongsToMany(User::class, ‘user_follower’,’follower_id’,’following_id’) ->withTimestamps(); } } class bacheca extends Model { protected $primaryKey = ‘id_post’; public function commento() { return $this->hasMany(Comment::class,’id_post_commento’); } } this piece of code works fine, but I would ..

Read more