I think you want this
$ideas = IdeaUser::find(10)->with('ideas');
Or this
$ideas = Ideas::find(10)->with('ideauser');
I tried both and no records return at all which is strange.
The way the tables are structure are like this
user_id | first_name | last_name
idea_id | fk_user_id | idea
Try:
public function ideas()
{
return $this->hasMany('Ideas','idea_id');
}
public function ideauser()
{
return $this->belongsTo('IdeaUser','user_id')->select(array('first_name', 'last_name'));
}
Also, check the spelling on $foreignKey:
protected $foreginKey = 'fk_user_id';
Rename fk_user_id to just user_id and I believe Eloquent should be smart enough to pick up the relationship without even defining the FK.
I would like to keep the same naming conventions. I tried executing this
$ideas = IdeaUser::find(10)->with('ideas');
and
$ideas = Ideas::find(10)->with('ideauser');
But both return 0 records when there is data in the database. I must be missing something so not sure what.
<?php class Ideas extends \Eloquent {
protected $table = 'ideas';
protected $primaryKey = 'idea_id';
protected $foreginKey = 'fk_user_id';
public function ideauser()
{
return $this->belongsTo('IdeaUser','user_id')->select(array('first_name', 'last_name'));
}
}
In my IdeaUser.php I have the following
<?php
class IdeaUser extends \Eloquent {
protected $table = 'idea_users';
protected $primaryKey = 'user_id';
public function ideas()
{
return $this->hasMany('Ideas','idea_id');
}
}
I think it is because you put "select" after the relationship, you need to put select(array())->get() or get(array('colums')).
try
$ideas = Ideas::find(10)->ideauser()->get();
if you append more things after relationship, you need to follow with a get(), paginate(),lists() ... I mean:
$something = Somethin::find(1);
$relatedItems = $something->relatedItems; //works
$relatedItems1 = $something->relatedItems->orderBy('name'); //DOES NOT WORK
$relatedItems2= $something->relatedItems->orderBy('name')->get(); WORKS!!!
So the same, with select() in you method . I suggest not to close relation to the columns. Just define the relationship. Then do another method or scope query to work with that.
//if you want you can also pass $fields public function getListed($query) { return $query->relation()->lists('name','id'); }
Or something like that. In my opinion you best and cleanes way is to work with Repositories...
I tried this and I am getting the following error
$ideas = Ideas::find(10)->ideauser()->get();
Call to a member function ideauser() on a non-object
I also tried this and now I am getting this error
$ideas = Ideas::find(10)->with('ideauser');
Illuminate\Database\Eloquent\Builder Object ( [query:protected] => Illuminate\Database\Query\Builder Object ( [connection:protected] => Illuminate\Database\MySqlConnection Object ( [pdo:protected] => PDO Object ( ) [readPdo:protected] => [reconnector:protected] => Closure Object ( [this] => Illuminate\Database\DatabaseManager Object ( [app:protected] => Illuminate\Foundation\Application Object ( [booted:protected] => 1 [bootingCallbacks:protected] => Array ( [0] => Closure Object ( [static] => Array ( [instance] => Illuminate\Translation\TranslationServiceProvider Object ( [defer:protected] => 1 [app:protected] => Illuminate\Foundation\Application Object RECURSION ) ) [this] => Illuminate\Foundation\Application Object RECURSION ) )
It's basic, but may be... Are you sure you have Idea with idea_id 10 ?
I know very basic but not sure why it isn't working I cant figure out what i am doing wrong. When I do this it works
$ideas=IdeaUser::join('ideas as idea', 'idea.fk_user_id', '=', 'idea_users.user_id')
->get(['idea.idea','idea_users.first_name','idea_users.last_name']);
Returns exactly what i want.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community