I have 3 tabels: users, teachers And posts.
users:
id - integer
name - string
teachers:
id - integer
teacher_id - integer
user_id - integer
name - string
posts:
id - integer
user_id - integer
title - string
User Model:
class User extends Model
{
public function teachers()
{
return $this->hasMany('App\Teacher');
}
}
Teacher Model:
class Teacher extends Model
{
public function posts()
{
return $this->hasMany('App\Post', 'user_id','teacher_id');
}
}
? Question is How can I use sth like this:
$user = User::find(1);
$teacher_posts = $user->teachers()->posts
I'm sort of new to laravel but here is my take.
What I have realized is that typically your many to many relationships will require a pivot table.
users:(id, name)
teachers:(id, teacher_id, name)
teacher_users:(id, user_id, teacher_id)
posts:(id, user_id, title)
And your Eloquent models will look like this.
class User extends Model
{
public function teachers()
{
return $this->belongsToMany('App\Teacher');
}
public function posts()
{
return $this->hasMany('App\Post');
}
}
class Teacher extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
}
$user = User::find(1);
$user_posts = $user->posts();
Check out the documentation here for more details
Related
I have 3 tables (Models)
Advertise :
id
title
...
Value:
id
title
amount
....
Field:
id
name
title
...
and my pivot tables is like:
advertise_id
value_id
field_id
how can I set relations in my models and do a crud(please give me an example)?
all the relations are many to many
3 model classes accordingly:
class Advertise extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class);
}
public function values()
{
return $this->belongsToMany(Value::class);
}
}
class Field extends Model
{
public function advertises()
{
return $this->belongsToMany(Advertise::class);
}
public function values()
{
return $this->belongsToMany(Value::class);
}
}
class Value extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class);
}
public function advertises()
{
return $this->belongsToMany(Advertise::class);
}
}
I have four models: Animal(which can only be "dog" or "cat"), Category, Breed and Post.
Table data:
Animal:
- id
- name
Category:
- id
- name
- animal_id
Breed:
- id
- name
- category_id
Post:
- id
- breed_id
What I need is to get all Posts that are about dogs. Have any ideas?
Update:
My models:
class Animal extends Model
{
public function categories()
{
return $this->hasMany(Category::class)->orderBy('name', 'ASC');
}
}
class Category extends Model
{
public function animal()
{
return $this->belongsTo(Animal::class);
}
public function breeds()
{
$this->hasMany(Breed::class)->orderBy('name', 'ASC');
}
}
class Breed extends Model
{
public function category()
{
return $this->belongsTo(Category::class);
}
public function posts()
{
return $this->hasMany(Posts::class);
}
}
class Posts extends Model
{
public function breed()
{
return $this->belongsTo(Breed::class);
}
}
First of all you need to properly set up the relations in the eloquent models,
here is an example of code,
For Post Eloquent Model
And post must have the column animal_id
public function animal(){
return $this->belongsTo(Animal::class);
}
Then you have to query like
$posts = Post::whereHas('animal',function($q){
return $q->where('name','dog');});
After that you will get the posts which belongs to animal data where animal name is dog. And if you want to query with your current database structure that is not recommended because of complex and long query.
I'd like to establish a many to many polymorphic relation in Laravel. (I'm new to it)
A user can have many profile types
Profile types are like Admin, Webmaster, ProjectManager.
I created a polymorphic relation and a pivot table for the profiles.
class User {
public function profiles(){
return Profile::where('user_id', $this->id);
}
}
class Webmaster { // and class Admin, Projectmanager
public function profiled(){
return $this->morphToMany(Profile::class, 'profileable');
}
public function saveToUser($user)
{
$profile = new Profile;
$profile->user_id = $user->id;
return $this->profiled()->save($profile);
}
}
Now I can save the models to the corresponding User.
$projectManager->saveToUser($user);
$webmaster->saveToUser($user);
It gets all saved to the pivot table as expected and the relations are valid.
profiles table looks like this:
id
user_id
profilable_id
profilable_type
Now the problem is retrieving a model collection of my profiles. I get the Profile types, but I dont get the Webmaster and ProjectManager.
So the question is: how do I get this model collection in this example?
Your model structure is going to be like:
class Webmaster extends Model
{
public function users()
{
return $this->morphToMany('App\Profile', 'userable');
}
}
class Admin extends Model
{
public function users()
{
return $this->morphToMany('App\Profile', 'userable');
}
}
// and ProjectManager, ....
User Model:
class User extends Model
{
public function webmasters()
{
return $this->morphedByMany('App\Webmaster', 'userable');
}
public function admins()
{
return $this->morphedByMany('App\Admin', 'userable');
}
}
Database schema:
webmasters
id - integer
...
admins
id - integer
...
users
id - integer
...
userables
user_id - integer
userable_id - integer
userable_type - string
Now, you can retrieve the relations:
$webmaster = App\Webmaster::find(1);
// retrieve users of a profile
foreach ($webmaster->users as $user) {
//
}
$user = App\User::find(1);
// retrieve webmaster profiles of a user
foreach ($user->webmasters as $webmasters) {
//
}
Actually, your profiles (webmaster, admin, projectmanager) are userable.
I'm using Paris for my new project...
Let's say I have 3 tables: users, books and borrows:
users: id/name
books: id/title
borrows: users_id/books_id/borrow_date/return_date
In Books class:
function users()
{
return $this->has_many_through('Users', 'Borrows');
}
In Users class:
function books()
{
return $this->has_many_through('User', 'Borrows');
}
Everything is fine, I can access to borrowed books by each user and list of users who borrowed a single book before, but I'm wondering that how can I access to borrow_date and return_date column/property of Borrows table/class?
You can do it using Idiorm (the Paris brother).
User model:
<?php
class User extends Model {
public static $_table = 'Users';
public static $_id_column = 'id';
function books()
{
return $this->has_many_through('User', 'Borrow');
}
public static function findBooksAndBorrows($id) {
return ORM::for_table('Users')
->join('Borrows', array('Users.id', '=', 'Borrows.users_id'))
->join('Books', array('Borrows.books_id', '=', 'Books.id'));
}
}
In your code:
$booksAndBorrows = User::findBooksAndBorrows(1)->find_array();
echo json_encode($booksAndBorrows);
I have tables: users, resources and pivot table user_resources.
users
id
username
etc.
resources
id
resource_name
etc.
user_resources
id
user_id
resource_id
User model:
class Resources extends Eloquent {
public function users() {
return $this->belongsToMany('User', 'user_resources');
}
}
and resource model:
class User extends Eloquent {
public function resources() {
return $this->belongsToMany('Resource', 'user_resources');
}
}
How can I get all of the users and all of the resources that belongs to the users.
Probably you just have to:
$everything = User::with('resources')->get();