How would i make a single request to insert in multiple tables using laravel Eloquent ORM relationship .ie
Table 1 : users
id
name
email
Table 2 : posts
id
user_id
content
Table 3 : Image
id
user_id
post_id
image_name
relationship
Table users id references user_id in other two tables .
Table posts has One To Many relation with users.
Table images has One To Many relation with users and post ie it can be shared with other users and on other posts.
So that when one makes a post insert ,it should insert the record in the tables with a single query.
This is one way of doing it:
$post = (new Post)->fill($request->all()->toArray())->user()->associate(Auth::user())->save();
As for the image, the Post model should have a model event such as static::created to handle the image upload and manipulation.
Or to make more sense, a model event in the Post model should trigger another model event from the Image model.
->toArray() may be optional, I can't test it here where I'm now.
Related
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')
Using Eloquent, I'm trying to make a polymorphic relationship between some models that isn't working out. Simplified, it works like this: there is a model 'groups', which obviously can contain some groups. Now each of these groups can either contain 'members' or 'guests', so I have a table and model for both of them. These are not similar to each other. My groups table contains a column in which model to use (members or guests). The structure is as follows:
groups (\App\Models\Group)
id
name
model (either \App\Models\Member or \App\Models\Guest)
members (\App\Models\Member)
id
group_id
first_name
last_name
address
email
.....
guests (\App\Models\Guest)
id
group_id
name
So the relationship 'people' in a group retrieves either the members or the guests. Using the default $this->morphTo() function using the group_id is able to retrieve the correct results, but due to its logic only returns one result, while I need all results. The rest of the app is indifferent to whether the people are members or guests, so I do not want to split those two up here.
Any ideas on how to accomplish this? I'm trying to eager load the relationship.
I have this table articles and users and I already made model on them and made some relationships with them thru MODEL and some foreign keys. How will I display it in a view like this article is created by this user. How will I query it ? like
$article->user() bla bla.
$query=article::where(['id'=>1])->with('user')->first();
$email=$query->user->email;
print_r($email);
this gives the email of the user with article id 1, if proper models and relationships.
I have following Database Tables
User
-idUser
Products
-idProducts
Product_Collector
-Product_CollectorID
-idProducts (foreign)
-idUser (foreign)
Ticket
-idTicket
-Product_CollectorID
Currently I'm modeling all these Database Tables in Laravel.
But I'm struggeling with the function to get the Products associated with the Tickets.
Do I have to create a model for the Product_Collector? Or is there any other good solution?
I have this schema
All the relations here must be one-to-zero/one.
A user can be either an employee or a customer. The user_type ENUM gives me the type so I know where to go from there.
Then an employee can be either basic or a manager. The employee_type discriminator let's me know that.
How am I supposed to build the Eloquent Model relations?
Let's say I have a user that is an employee. I need to get it's common fields from the users table but also need to get common fields from employees table. Do I need to hard code, and know that when user_type=emp I need to select from the employees table? What if I need to add another user type later?
UPDATE
Would it make sense to change my schema into something simpler?
My problem is that by using, as suggested, polymorphic relations I would end up to something like this:
$user = new User::userable()->employable()->...
Would a schema in which I drop the employees table and have employee_managers and employee_basics linked straight to the users table?
this is an polymorphic relationship. but if you want to be easy, you need to fix some things.
in the table employees
- user_id
- employable_id
- employable_type enum(Manager, Basic) # References to the target model
.... this last two are for the polymorphic relation, this is the nomenclature
in the basics and managers table you could delete the user_id field, but you need an id field as increments type
and now in the model Employee you need to make this function
public function employable(){
return $this->morphTo();
}
I hope this works :)