I need to make users to follow (just like in twitter) other users in my app.
So by this user(1) that follows user(2) can read view his posts- but not vice versa.
what's the relationship for it? is it many to many ?
Thanks.
You should use the many-to-many relation with pivot table I guess.
Seems to be a many-to-many: many users can follow many other users. So your schema would look something like this:
$table->integer('follower_id')->unsigned();
$table->integer('followee_id')->unsigned();
$table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('followee_id')->references('id')->on('users')->onDelete('cascade');
Related
i am new to Laravel i need help in building a relation with models, i have coach model which has many clients and client belong to Coach and i can perform operation on clients like this Auth()->user()->clients()->create($request->all()); now i want to add courses in way that i could use something like Auth()->user()->clients()->courses()->create($request->all()); how should i make a relation for this. what will be the best approach.
Actually you can't do ->clients()->courses() because the ->clients() will returns you a lot clients of user so ->courses() doesnt know the course of which client do you want to get.
You can create a relationship between the Couch and Course(add couche_id to courses table) model so then you can do
Auth()->user()->courses()->create($request->all());
Reading Laravel 6 eloquent-relationships docs at https://laravel.com/docs/6.x/eloquent-relationships#one-to-one
I did not catch what is “pivot table” and which is practical use of it working with db in laravel ?
Thanks!
Think you have two tables like users, and roles if you want to make a relationship between them then you need to declare Many To Many relationships because the single role have many users and single users have many roles. for defining Many To Many relationships between them you need to create role_user table, here role_user table means pivot table.
for more information please read this Many To Many relationships from laravel documentation
I have many to many relationships. Imagine I have 3 tables. Something like this :
users.
roles.
role_user.
(This example is also provided in laravel's docs).
Now I'm doing this : $user->roles() which returns roles with Pivot attributes . but what I actually want to do is move forward and also get the appropriate data from the 4th table. something like this $user->roles()->types(); and the difficult thing is that this types() belongs to pivot table.
Do you know how to do this kind of thing ? where Do I write types() function?
Assuming your "Roles" model has the relationship set you may try
$user->roles()->with("types")
Source docs: https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
I'm using laravel 5.6 and i have two tables categories and posts
I have created category_id in posts table which looks like
{id, category_id, title, description, created_at, updated_at}
I created a drop-down on the post create and edit form to select the category which works fine.
Now I am looking for something more advanced where a post can have multiple categories. I have changed belongTo to HasMany categories in post model.
I feel I am doing it the wrong way. Do i need to create another table i.e.,
post_categories
{id, category_id, post_id}
The reason i want to do this is because i have multiple posts which belong to multiple categories and my route is like this
site.com/categoryname/post-slug
So few posts appear in multiple categories.
You probably will need to use the pivot table. That way you can data mine. Even if you don't have a multi-select, you'll easily be able to collect posts linked to categories and vice versa. laraveldaily-good example. They use the sync method, one of my favs. when you save the form data you can just do something like App\Post::find($id)->categories()->sync(request()->input(categories')) and laravel will handle the rest for you.
Your relationships look like they are thought out. to me, it looks like your on the right track.
Just use the belongsToMany relationship instead of HasMany
Laravel has great documentation on this: laravel many to many
If I have tables in doctrine for user_1, user_2, etc. is there a way to dynamically set the table name in Doctrine for a single User model?
It's weird, I know. I'm trying to create an interface to a WordPress database (because WP has little to no API for directly accessing posts), and WP creates duplicate tables for each site, so there's a wp_posts, wp_comments, wp_2_posts, wp_2_comments, etc.
Here's what I ended up doing:
$post = new WordPressPost();
$post->setTableName('wp_'.$user_id.'_posts');
If it could, you would have to run migrations for each added/deleted user.
I am curious; why would you EVER need something like that?
I don't know how WP works, but here is the thing; each site should use it's OWN database, not to share it with others.