Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

I think you want this

$ideas = IdeaUser::find(10)->with('ideas');

Or this

$ideas = Ideas::find(10)->with('ideauser');
Last updated 2 years ago.
0

I tried both and no records return at all which is strange.

The way the tables are structure are like this

ideauser

user_id | first_name | last_name

ideas

idea_id | fk_user_id | idea

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

In my ideas.php file I have this

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


 }
Last updated 2 years ago.
0

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...

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

It's basic, but may be... Are you sure you have Idea with idea_id 10 ?

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Any ideas as to what I am doing wrong.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

kyoukhana kyoukhana Joined 28 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.