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);
Related
Using Laravel 5.6, let's say I have the following database tables:
networks users vlans
-------- ----- -----
id id id
network_id network_id
vlan_id
And these relations:
class Network extends Model {
public function users() { return $this->hasMany("User"); }
public function vlans() { return $this->hasMany("Vlan"); }
}
class User extends Model {
public function network() { return $this->belongsTo("Network"); }
public function vlan() { return $this->belongsTo("Vlan"); }
}
class Vlan extends Model {
public function network() { return $this->belongsTo("Network"); }
public function user() { return $this->hasOne("User"); }
}
My question is: given a Network ID, is there an easy way to determine which Vlan objects are not assigned to a User?
This is very early stages (and I'm quite new to Laravel) so if I've screwed up the relationships I have no qualms about starting over. I've considered changing things so that the vlans table has a user_id foreign key but that seems backwards. (And it would just leave me with the same question in reverse: how to find all the User objects that don't have a Vlan assignment!)
You can add a scope to your Vlan model:
public function scopeNotAssigned($query, $id)
{
return $query->whereNotIn(
'id',
User::where('network_id', $id)->select('vlan_id')->get()->pluck('vlan_id')->toArray()
);
}
Then suppose you have a network:
$network = \App\Network::first();
You can retrieve the non-assigned vlans in the network like this:
$vlans = \App\Vlan::notAssigned($network->id)->get();
Otherwise you can also add a relationship with the users in your Vlan model:
public function user()
{
return $this->hasOne('App\User');
}
Then you could use the whereDoesntHave function:
$vlans = Vlan::whereDoesntHave('users', function ($query) use ($network) {
return $query->where('network_id', $network->id);
})->get();
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.
Consider the following table structure:
user table
id
name
lang_region_id
lang_region table
id
lang_id
region_id
lang table
id
name
region table
id
name
Fairly new to the Laravel framework, but trying to setup Eloquent models and relationships to an existing database. I want to establish the relationship between my user model and the lang and region models. The lang_region table defines what language and region combinations are available and then we can link each user to a valid combination.
I have read through the Laravel documentation several times looking for the proper relationship type, but is seems that the Many to Many and Has Many Through relationships are close, but since our user.id isn't used in the intermediate table I may be out of luck.
Sorry for the amateur question, but just getting used to Laravel and ORMs in general.
I would use the lang_region table as both a pivot table and a regular table with its own model.
class LangRegion extends model
{
protected $table = 'lang_region';
public function language()
{
return $this->belongsTo(Language::class, 'lang_id');
}
public function region()
{
return $this->belongsTo(Region::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
class User extends model
{
protected $table = 'user';
public function langRegion()
{
return $this->belongsTo(LangRegion::class);
}
}
class Language extends model
{
protected $table = 'lang';
public function regions()
{
$this->belongsToMany(Region::class, 'lang_region', 'lang_id', 'region_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'lang_id', 'lang_region_id');
}
}
class Region extends model
{
protected $table = 'region';
public function languages()
{
$this->belongsToMany(Language::class, 'lang_region', 'region_id', 'lang_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'region_id', 'lang_region_id');
}
}
If I understand what you want correctly:
class User extends Model {
private function lang_region() {
return $this->hasOne(LangRegion::class)
}
public function lang() {
return $this->lang_region()->lang();
}
public function region() {
return $this->lang_region()->region();
}
}
class LangRegion extends Model {
public function lang() {
return $this->belongsTo(Lang::class);
}
public function region() {
return $this->belongsTo(Region::class);
}
}
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
I am trying to develop a blog using Laravel 5 in which i have to show comment along with user on post.
Here is my database table schema.
User
id
name
Posts
id
post_content
user_id
Comments
id
comment
user_id
post_id
Here is my User Model
public function posts()
{
return $this->hasMany('App\Models\Posts');
}
public function comments(){
return $this->hasManyThrough('App\Models\Comments','App\Models\Posts');
}
Here is my Posts Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function comments(){
return $this->hasMany('Ap\Models\Comments');
}
Here is my Comment Model
public function posts()
{
return $this->belongsTo('App\Models\Posts');
}
public function user(){
return $this->posts->name;
}
Here is my code how i am accessing user name
$comments = Comments::find(1);
$comment['comment'] = $comments->comment;
$comment['user_name'] = $comments->name;
$comment['post_id'] = $comments->posts->id;
may be i am not getting in right direction? if i am doing right then why it is not working.
In laravel 5 you do not call the model as you are doing. since the models are stored in the app folder just call like. Plus I think you need to define the relationship
class User extends Model {
public function phone()
{
return $this->hasOne('App\Phone');
}
}
class Phone extends Model {
public function user()
{
return $this->belongsTo('App\User');
}
}
$phone = Phone::find(1);
For the case of foreign keys and more regarding the Eloquent relationships in laravel 5 just follow the documentation on the laravel website. Make sure to look at dynamic properties of that are allowed by eloquent
http://laravel.com/docs/5.0/eloquent