Laravel 6 database error : Integrity constraint violation - laravel

I am trying to set up the relations between my migrations in Laralvel 6, but I still have this error message and I do not understand why ....
When I want to publish an article, it takes the query but returns empty tables.

From the very limited information given, just by looking at the query it seems to be an issue with the user_id. In the insert stament there is no user_id inserted, which might be the problem? In your Article model, is user_id fillable?
If user_id isn't required, has it been made nullable in the migration for articles?
Please provide some examples of the migration, model and the method in the controller used for the create of the Article.

Related

Trying to use Same column as primary key in multiple tables, ''invalid field definition of index or relationship fix" error appears

I am new to MS Access. I have created 5 tables and this is my relationship view:
relationship
But when I am trying to establish a relationship between tblCard.CardID and tblFacilities.CardID, I get an error
invalid field definition of index or relationship fix
All the CardID columns are autonumber.
Can anyone please tell me why this problem is appearing?
Thanks in advance
I checked the indexing of all the CardID columns. I don't understand why this is happening.

Laravel get relation by custom value

Model - User
id [999]
name [foo]
Model - Post (without User Foreign Key)
id [1]
unique_key [USR_00000999]
data [bar]
I would like to get all user with related posts (one to many) by using "custom key" value, is this possible with build in eloquent?
I only manage to looping using foreach one by one with
Post::query()
->where('unique_key', sprintf('USR_%08d', $user_id))
->count();
That is not built-into Laravel by default... if you want to know why it's because it's not a common thing that everyone does...
BUT, you can use scope query so you don't need to do the sprintf everytime...
Laravel Query Scopes Documentation
But I want to ask, why wouldn't you just add user_id on your post table and just have an Accessors on your post model for generating the unique_key? That would be much easier on my perspective...
Laravel Accessor Documentation
UPDATE: (See Comment)
Is it possible to have an empty user_id on Posts table?
then populate it when the user is created?
say you have a posts with key of USR_00000999... and you have a user with an id of 999... when you create that user you'll just have to update all posts with key of USR_00000999 to have a user_id of 999...

Insert id from table to another table in laravel +vue.js

first image
second image
I want to get Id from students table where i want insert this id in differs table . I don't really know how to explain it but maybe if you see my codes, you would understand. I have already finish making the relationship between this 2 tables already
my student controller
this my differ controller but I do not know any idea about if this code true or no
first: 'app\Differ'? but Differ is in App\Models
you can use hasOne(Differ::class); when Student model and differ in the same folder
the second thing you should know, when using functions of relationship in laravel such as hasOne, a foreign key should be named by student_id in Differ or
you show it in function like hasOne(Differ::class,'id_student','id')

How to add in a model collection an associated row of related a model in laravel

I don't know if my title is right so feel free to edit this post and remove this first paragraph of my post.
I am trying to create a simple API in Laravel and I'm having a problem when i get a paginated shoutouts records to include the users who create it. I have 1 is to 1 relationship between User and Shoutout model and in the tables the shoutouts have the foreign key of user_id since the user is who created the shoutout and not the shoutout created the user.
So I tried to get a collection of shoutouts and include the user associated with it with this method:
$shoutouts = Shoutout::with('user')->paginate(15);
but I Having this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.shoutout_id' in 'where clause' (SQL: select * from `users` where `users`.`shoutout_id` in (1))
and it looks like the query trying to find shoutout_id to users table and since the foreign key is in the shoutouts table it gets an error in query. If I use this method:
$user = User::with('shoutout')->paginate(15);
there is no problem in querying, but the problem is its now a collection of users not a collection of shoutouts and the shoutout is nested in it, not the user is nested in shoutout collection. Is there a method(function) to use to get what i wanted to happen or a way to do that if if there is no method. I cant find any related question or maybe just my keyword for searching is wrong, feel free to suggest a link here if there is a related question to this question.
edited:
I think i will use query builder instead, but if there is method to achieve this in Eloquent I will upvote for that

Laravel5: How are Eloquent model relationships expressed in the database?

There's a missing link I fail to understand.
I use migrations to create database tables and I define the relationships there. meaning.. if I have a person table and a job table and I need a one to many relationship between the person and jobs, I'd have the job table contain a "person_id".
When I seed data or add it in my app, I do all the work of adding the records setting the *_id = values etc.
but somehow I feel Laravel has a better way of doing this.
if I define that one to many relationship with the oneToMany Laravel Eloquent suports:
in my Person model.....
public function jobs()
{
return $this->hasMany('Jobs);
}
what's done on the database level? how do I create the migration for such table? Is Laravel automagically doing the "expected" thing here? like looking for a Jobs table, and having a "person_id" there?
Yep, Laravel is doing what you guess in your last paragraph.
From the Laravel documentation for Eloquent Relationships (with the relevant paragraph in bold):
For example, a User model might have one Phone. We can define this
relation in Eloquent:
class User extends Model {
public function phone()
{
return $this->hasOne('App\Phone');
}
}
The first argument passed to the hasOne method is the name of the
related model. Once the relationship is defined, we may retrieve it
using Eloquent's dynamic properties:
$phone = User::find(1)->phone;
The SQL performed by this statement
will be as follows:
select * from users where id = 1
select * from phones where user_id = 1
Take note that Eloquent assumes the foreign key of the relationship based on the model name. In this case, Phone model is assumed to use a user_id foreign key.
Also note that you don't actually have to explicitly set the foreign key indexes in your database (just having those "foreign key" columns with the same data type as the parent key columns is enough for Laravel to accept the relationship), although you should probably have those indexes for the sake of database integrity.
There is indeed support to create foreign key relationships inside migration blueprints and it's very simple too.
Here is a simple example migration where we define a jobs table that has a user_id column that references the id column on users table.
Schema::create('jobs', function($table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
You can also use some other methods that laravel provides such as onDelete() or onUpdate
Of course to understand better the options that are available to you please read the documentation here.
Edit:
Keep in mind that Eloquent is just using fluent SQL wrapper and behind the scenes there are just raw sql queries, nothing magical is happening, fluent just makes your life a lot easier and helpers you write maintainable code.
Take a look here about the Query Builder and how it works and also, as #Martin Charchar stated , here about Eloquent and relationships.

Resources