#### 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 ..

[This is to populate data from two tables that one has two foreign keys from the same column as reference on the other table] https://i.stack.imgur.com/D8fiv.png [This is my schema for the table with the foreign key] https://i.stack.imgur.com/eYDL0.png This is written in laravel and it is working however i have an itchy feeling that this is ..

I need help in a very simple thing, I have 3 tables, users, brands, appliances, brands and products have many to many relations No While registering user, i want to give option to user to select their preferred brands and then save then, after suces, user is created, My confusion is in this case, Do ..

I’m trying to implement a polymorphic relation in my application while still taking advantage of foreign keys. I have 2 user types: Admin and User. Both of them can leave comment on a post. Here’s a brief description of my comment table: id – The id of the comment id_admin – FK of admin table, ..

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 make a one to many relationship in my project but when i want to fetch data it returns null response. i have a user Model and Intership Model. each user has one Intership and each Intership has many users. so , relationship is one to many . i create this relation but i cant .. 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 ..

I have two tables named users and user_data. The user table has a primary key named id and I have assigned this column as a foreign key to the other table named user_data. Now while adding values to the user_data table through a controller from a form, I want to set the value of the ..

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 ..

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’); } ..

I have an API with different parts that contains many foreign keys, like category_id for posts and categories , user_id for users and posts , parent_id for categories and subcategories and … so for example , i have a post details json response : "post": { "id": 1, "category_id": 2, "user_id": 1, "title": "Is there ..

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’, ]); ..

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’, ..

Image We have 2 tables first table name is attributes_products in which attritubes_id a foreign key of the second table is attributes so we can find the list of second table by using first table Sourc..

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 ..

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 ..

I have this very weird situation in my MySQL table. MySQL version 5.6. The framework I am using is Laravel 5.7 though I think it is irrelevant So I have two tables: users table for user information secretary_notes table, which has a user_id. There is a foreign key. CONSTRAINT secretary_notes_user_id_foreign FOREIGN KEY (user_id) REFERENCES users ..
I want to get all SubCourse parrents. In example: I have HTML Course in database, and HTML has 1 subcourse, HTML flex, and I want to get HTML flex parent, the HTML Course. I tried this but I get error. $subCourses = SubCourse::with(‘courses’)->get() Course.php class Course extends Model { use HasFactory; protected$fillable = [ ..