Laravel 5 using pivot table with hasMany relationship - php

I'm making a poll system and I have two tables :
polls table : Has those fields (id,question,created_at,updated_at).
choices table : Has those fields (id,poll_id,choice).
And a pivot table named choice_poll : Has those fields (id,choice_id,poll_id,ip,name,phone, comment ,created_at,updated_at)
Poll Model :
class Poll extends Model
{
protected $table = 'polls';
protected $fillable = ['question'];
public $timestamps = true;
public function choices()
{
return $this->BelongsToMany('App\Choice')->withPivot('ip','name','phone','comment');
}
}
Choice Model :
class Choice extends Model
{
protected $table = 'choices';
protected $fillable = ['poll_id','choice'];
public $timestamps = false;
public function poll()
{
return $this->belongsTo('App\Poll')->withPivot('ip','name','phone','comment');
}
}
Now when I try to build this query it doesn't return the choices :
$poll->first()->choices()->get()
PS: There is many choices in the choices table associated with the first poll.

In this case you have Many To Many relationship so try to change belongsTo in :
public function poll()
{
return $this->belongsTo('App\Poll')->withPivot('ip','name','phone','comment');
}
To belongsToMany, and it will be :
public function poll()
{
return $this->belongsToMany('App\Poll')->withPivot('ip','name','phone','comment');
}
NOTE 1: You have to change BelongsToMany to belongsToMany note the B should be in lower case.
NOTE 2: You want the pivot table with timestapms create_at,updated_at as you mentioned in th OP so you have to use withTimestamps(); :
return $this->belongsToMany('App\Poll')
->withPivot('ip','name','phone','comment')
->withTimestamps();
//AND in the other side also
return $this->belongsToMany('App\Choice')
->withPivot('ip','name','phone','comment')
->withTimestamps();
Hope this helps.

This:
public function poll()
{
return $this->belongsTo('App\Poll')->withPivot('ip','name','phone','comment');
}
and
public function choices()
{
return $this->BelongsToMany('App\Choice')->withPivot('ip','name','phone','comment');
}
should be:
public function poll()
{
return $this->belongsToMany('App\Poll')->withPivot('ip','name','phone','comment');
}
and
public function choices()
{
return $this->belongsToMany('App\Choice')->withPivot('ip','name','phone','comment');
}

Related

associative table for OneToMany relation

I'm working on a subtitles bot where:
A Movie has multiple subtitles (depending on quality and language)
A Series has multiple seasons with multiple episodes that have multiple subtitles (same as a movie)
These are my tables (Thanks to DaveRandom):
The problem is, as far as I know associative tables are for many-to-many relationships (correct me if I'm wrong), I'm kinda stuck here, especially with Eloquent's belongsToMany method:
class Subtitle extends Model {
public $guarded = [];
public $timestamps = false;
public function SeriesEpisodes()
{
return $this->belongsToMany('SeriesEpisode', null, null, 'series_episodes_subtitle');
}
public function Movie()
{
return $this->belongsToMany('Movie');
}
}
But the problem is, A Subtitle belongsToOne Episode, whereas an Episode might have many subtitles.
I was wondering how one could use associative tables for such structure.
Or should I change the structure?
So what I was trying to achieve is possible with polymorphic relationships,
2 associative tables are removed and instead 2 fields are added to subtitles table:
ex: parent_type, parent_id
then I can use:
subtitles:
class Subtitle extends Model {
public $guarded = [];
public $timestamps = false;
public function Parent()
{
return $this->morphTo();
}
}
series_episodes:
class SeriesEpisode extends Model {
public $timestamps = false;
public $guarded = [];
public function Subtitles()
{
return $this->morphMany('Subtitle', 'Owner');
}
}
movies:
class Movie extends Model {
public $timestamps = false;
public $guarded = [];
public function Subtitles()
{
return $this->morphMany('Subtitle', 'Owner');
}
}
Hope it helps others.

Array returning null value, many to many. laravel

I'm trying to retrieve data from many to many relationship.I have two tables :
companies: [cid,name,origin]
vehicle_types: [id, type]
their pivot table: companies_vehicle_types: companies_id,vehicle_types_id Relationship defined: In Companies:
class companies extends Model
{
//
protected $fillable = ['name','origin'];
protected $primaryKey = 'cid';
public function vehicles(){
return $this->hasOne('App\vehicles');
}
public function vehicle_types(){
return $this->belongsToMany('App\vehicle_types', 'companies_vehicle_types', 'companies_id', 'vehicle_types_id');
}
}
In vehicle_types
class vehicle_types extends Model
{
//
protected $fillable = ['type'];
public function vehicles(){
return $this->belongsTo('App\vehicles');
}
public function companies(){
return $this->belongsToMany('App\companies','companies_vehicle_types','vehicle_types_id','companies_id')->withTimestamps();
}
}
I want to retrieve companies where vehicle_types = specific type. How can i do that? I tried doing following in my controller:
$vehicle_types=vehicle_types::where('type','Bike')->get();
foreach ($vehicle_types as $vehicle_type) {
# code...
foreach ($vehicle_type->companies as $company) {
$brand[]=$company->pivot->name;
}
}
return $brand;
But it doesn't seem to be working. $vehicle_types is working fine and returning value. $brand is not returning any value.

Eloquent model relationship for intermediate table

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);
}
}

Can same-table eager loads be combined in Laravel 4?

I have a model where I'm eager-loading two references to another table (in this case posts.created_by_id == users.id and posts.updated_by_id == users.id).
class Post {
protected $table = 'posts';
public function scopeLatest() {
return $query->with(['created_by', 'updated_by'])
->orderBy('created_at', 'desc');
}
public function createdBy() {
return $this->belongsTo('User');
}
public function updatedBy() {
return $this->belongsTo('User');
}
}
class User {
protected $table = 'users';
public function posts() {
return $this->hasMany('Post', 'created_by');
}
}
This results in something like the following queries:
SELECT * FROM tbl ORDER BY created_at DESC;
SELECT * FROM users WHERE id IN (?); (created_at)
SELECT * FROM users WHERE id IN (?); (updated_at)
This makes sense - we're loading all of the referenced records in created_by, and then updated_by - however we could optimise this to combine the ids make a single query to users.
My question is: is this something Eloquent currently supports?
Didn't work for me. It seems that the eager load works for only one instance of the table. For me only one of the relationships has been filled. Eloquent probably uses the the table name as a pointer for the eager load. It generates all the eager load queries but only one will be filled.
I had to separate the data into different tables just because of this problem (and haven't had the time to dig that deep into Eloquent code.)
I think this is what you might be looking for:
class Post {
protected $table = 'posts';
public function scopeLatest($query) {
return $query->with('createdBy', 'updatedBy')
->orderBy('created_at', 'desc');
}
public function createdBy() {
return $this->belongsTo('User','created_by_id');
}
public function updatedBy() {
return $this->belongsTo('User','updated_by_id');
}
}
class User {
protected $table = 'users';
public function postsCreated() {
return $this->hasMany('Post', 'created_by_id');
}
public function postsUpdated() {
return $this->hasMany('Post', 'updated_by_id');
}
}

Eloquent model relationship returning as null

I have a laravel model
class Project extends Eloquent {
public static $timestamps = true;
public $includes = array('members','members.memberdata');
public function tasks() {
return $this->has_many('Usertask','project_id');
}
public function members() {
return $this->has_many('Projectmember','project_id');
}
}
and a related models
class Projectmember extends Eloquent {
public static $table = "project_members";
public static $timestamps = true;
public function project() {
return $this->belongs_to('Project');
}
public function memberdata() {
return $this->has_one('Usermetadata','user_id');
}
}
class Usermetadata extends Eloquent {
public static $table = "users_metadata";
public function user() {
return $this->belongs_to('User');
}
public function member() {
return $this->belongs_to('Projectmember','user_id');
}
}
When i attempt to retrieve a single project model like so
$project = Project::find($id);
return Response::eloquent($project);
my json output looks like this
{"id":1,"user_id":1,"name":"UberWork","description":"Web based project management \/ task management app","target_date":"2013-11-15 00:00:00","budget":null,"status":0,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","members":[{"id":1,"project_id":1,"user_id":1,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","memberdata":{"user_id":1,"first_name":"Tamarakuro","last_name":"Foh","photo":"","company_name":null,"phone":null,"package":"free","subscription_start":"0000-00-00 00:00:00","subscription_end":"0000-00-00 00:00:00","api_key":"12b14a7d3ca48c53bb5b1a88fa3eca3b"}},{"id":3,"project_id":1,"user_id":3,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","memberdata":{"user_id":3,"first_name":"Ebere","last_name":"Uche","photo":"","company_name":"Chronotech Labs","phone":null,"package":"free","subscription_start":"0000-00-00 00:00:00","subscription_end":"0000-00-00 00:00:00","api_key":"ab446bd9ffbb898e818a892c7401e0f6"}},{"id":4,"project_id":1,"user_id":2,"created_at":"2013-04-17 08:13:00","updated_at":"2013-04-17 08:13:00","memberdata":null}]}
My database look like this;
Users
id
email
password
ip_address
active
...
users_metadata
id
user_id
first_name
last_name
profile_photo
...
Projects
id
user_id
name
description
status
...
project_members
id
project_id
user_id
My question is why is the last member of the project having its memberdata as "null", while the others are not null. Am i doing something wrong?
Relationships in Eloquent always link a primary key (pk) with a foreign key (fk). However, you're trying to base a relationship on two foreign keys, skipping out a relationship. The only Eloquent solution is to include the extra relationship step. Here are some models (I've ommited the relationships we don't need for this example)...
class Project extends Eloquent {
public static $timestamps = true;
public $includes = array('members','members.user', 'members.user.metadata');
public function members()
{
return $this->has_many('Projectmember','project_id');
}
}
class Projectmember extends Eloquent {
public static $table = "project_members";
public static $timestamps = true;
public function user()
{
return $this->belongs_to('User');
}
}
class User extends Eloquent {
public static $timestamps = true;
public $hidden = array('password', 'ip_address');
public function metadata()
{
return $this->has_one('Usermetadata');
}
}
class Usermetadata extends Eloquent {
public static $table = "users_metadata";
public function user() {
return $this->belongs_to('User');
}
}
I can see why you'd want to skip the relationship, but sadly it's not possible with relationships.

Categories