Category : foreign-keys

Assume I have a the tables users and persons. An entry in persons can have a user id or not. So a person can have a user, but this is optional. This means that the foreign key in persons needs to be nullable. This is my database migration: $table->bigInteger(‘user_id’)->nulleable()->unsigned(); $table->foreign(‘user_id’)->nulleable()->references(‘id’)->on(‘users’); Sadly, when I check the ..

Read more

I need help for following matter. I have 4 tables as follows categories : categories attributes : attributes attribute_options : attribute options according to specific attribute id attribute_category : pivot table for category and attributes models Model relationships are as follows. Category Model public function categories() { return $this->belongsToMany(Category::class); } Attribute Model public function attributes() ..

Read more

I want to search for usernames from the user’s table. I have used the Laravel relationship(eloquent). I don’t search for usernames from users’ tables. My code is Below and I Explain relationship with All Tablets I have a query write inside the group controller public function getGroupsData(Request $request){ $group = Group::with([‘memberscount’, ‘getusername’])->where(‘groupName’, "like", "%" . ..

Read more

I’m trying to run migrations and three of those have foreign keys. This is one of them: public function up() { Schema::create(‘roomclasses’, function (Blueprint $table) { $table->engine = ‘InnoDB’; $table->id(); $table->timestamps(); $table->string(‘code’, 3); $table->text(‘description’); $table->time(‘time’); $table->float(‘price’); }); Schema::table(‘roomclasses’, function (Blueprint $table) { $table->foreignId(‘days_combination_id’)->constrained(‘days_combinations’); $table->foreignId(‘room_id’)->constrained(‘rooms’); }); } SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint ..

Read more

I wanna set two primary key(id and Android_id) in my table (etat_terminals) so there is a problem. PS: Android_id is type string this is migration of etat_terminals: public function up() { Schema::create(‘etat_terminals’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->string(‘Android_id’); $table->string(‘NiveauDeBatterie’)->nullable(); $table->string(‘Memoire’)->nullable(); $table->string(‘Lattitude’)->nullable(); $table->string(‘Longitude’)->nullable(); $table->string(‘Fabriquant’)->nullable(); $table->string(‘Modele’)->nullable(); $table->string(‘VersionSE’)->nullable(); $table->timestamps(); $table->softDeletes(); }); } this is migration of terminals: public ..

Read more

I have the following structure: **users** id, company_id **companies** id, country_id **countries** id, name Now I’d like to get the user with the company and country like this: User::with(‘country’)->get(); So I have added the relationship to my user model: public function country() { return $this->hasOneThrough(Country::class, Company::class); } However, Eloquent is looking for a user_id column ..

Read more

I have four tables – roles , items, permissions, role_permissions. Their migrations are as follows: roles: Schema::create(‘roles’, function (Blueprint $table) { $table->id(); $table->string(‘name’); $table->string(‘slug’)->nullable(); $table->timestamps(); }); items: Schema::create(‘items’, function (Blueprint $table) { $table->id(); $table->string(‘name’); $table->timestamps(); }); permissions: Schema::create(‘permissions’, function (Blueprint $table) { $table->id(); $table->string(‘name’); $table->timestamps(); }); and the role_permissions : Schema::create(‘role_permissions’, function (Blueprint $table) { ..

Read more

I have two tables ‘teams’ and ‘users’. in users I have a foreign key("current_team_ID) for ‘ID’ field in ‘teams’ table. Now I want to see in our view the name of team of user. This is my controller code: public function index() { $user = User::orderby(‘id’, ‘desc’)->paginate(20); $current_team_id = Teams::pluck(‘title’, ‘id’); return view(‘admin.users.index’)->with(compact(‘user’, ‘current_team_id’)); } ..

Read more

I have this relational table on my db: id, is referenced to: "attivitaSost" (and attivitaSpostata). The relathionship is "optional" so the foreignkey is nullable. But since the problem is the same, I will try to solve the first relationship first. My model "cciActivities" have this 2 methods: public function attOrig() { return $this->hasOne(CcieActivity::class,’id’, ‘attivitaSost’); } ..

Read more

I’m having trouble inserting value in null field. this is the table public function up() { Schema::create(‘users’, function (Blueprint $table) { $table->id(); $table->string(‘name’); $table->string(‘username’)->unique()->nullable(); $table->string(‘mobile_no’)->nullable(); $table->string(‘address’)->nullable(); $table->string(’email’)->unique(); $table->boolean(‘role_name’)->default(0); $table->string(‘password’); $table->rememberToken(); $table->timestamps(); }); } this is the UserController public function update(Request $request, User $user) { $request->validate([ ‘name’ => ‘required|max:255’, ‘mobile_no’ => ‘required|max:11’, ‘address’ => ‘required|max:255’, ]); ..

Read more

I’m having trouble with the foreign key. I have two tables. users table and user_address table. I’ll show you the migration. users table: public function up() { Schema::create(‘users’, function (Blueprint $table) { $table->id(); $table->string(‘name’); $table->string(‘username’)->unique()->nullable(); $table->string(’email’)->unique(); $table->timestamp(’email_verified_at’)->nullable(); $table->string(‘password’); $table->rememberToken(); $table->timestamps(); }); } and this is the user_address table: public function up() { Schema::create(‘user_address’, function (Blueprint ..

Read more

I’ve got these two tables linked with a one to one relation. How should the seeders be if I want unique foreign keys in sponsored apartments table? Model table Apartments: public function sponsoredapartment() { return $this -> hasOne(SponsoredApartment::class); } Model table Sponsored Apartments public function apartment() { return $this -> belongsTo(Apartment::class); } Migrations Apartments Schema::create(‘apartments’, ..

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 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’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 try to get Address with Country’s name, which belongs to InvoiceData. Invoice Data belongs to Personal or Business Profile. Last condition was made with morphTo() Laravel’s feature. Simplified db structure: personal_profiles: id, name invoice_data: id, address_id, invoiceable_id, invoiceable_type addresses: id, country_id, postal_code, city countries id, name Then models: class UserProfile extends Model { public ..

Read more