Frage

Models

class WPModel extends Eloquent
{

    protected $table = 'work_processes';
    protected $guarded = array('id');
    protected $softDelete = true;

    // declaring one-to-many relationship
    public function relatedWPAQs()
    {
        return $this->hasMany('WPAQModel', 'wp_id');
    }

    public function relatedUsers()
    {
        return $this->belongsTo('UserModel', 'wp_owner_id');
    }

}

class UserModel extends Eloquent 
{
    protected $table = 'users';
    protected $softDelete = true;

    public function relatedWPs()
    {
        return $this->hasMany('WPModel', 'wp_owner_id');
    }
}

class WPAQModel extends Eloquent
{
    protected $table = 'wp_audit_questions';
    protected $fillable = array('wp_id', 'wp_audit_question');

    // declaring one-to-many relationship - the inverse way
    public function relatedWP()
    {
        return $this->belongsTo('WPModel', 'wp_id');
    }

    public function scopeWpParent($query, $id)
    {
        return $query->where('wp_id', '=' ,$id);
    }
}

Controller

class WPAQController extends BaseController
{
    public function showWPAQ($wpid)
    {
        $workprocess = WPModel::where('id', $wpid)
                                ->join('users', 'users.id', '=', 'work_processes.wp_owner_id')
                                ->select('users.user_name', 'work_processes.*')
                                ->with(array(
                                'relatedWPAQs' => function($query) use ($wpid)
                                    {
                                        $query->where('wp_id', '=',$wpid);
                                    }                                
                                ))
                                ->get();
        return View::make('wpaqshow')->with(compact('workprocess'));
    }
}

when I run this code, I get following error

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select users.user_name, work_processes.* from work_processes inner join users on users.id = work_processes.wp_owner_id where work_processes.deleted_at is null and id = ?) (Bindings: array ( 0 => '1', ))

War es hilfreich?

Lösung

Try the following:

$workprocess = WPModel::where('work_processes.id', $wpid)
                                ->join('users', 'users.id', '=', 'work_processes.wp_owner_id')
                                ->select('users.user_name', 'work_processes.*')
                                ->with(array(
                                'relatedWPAQs' => function($query) use ($wpid)
                                    {
                                        $query->where('wp_id', '=',$wpid);
                                    }                                
                                ))
                                ->get();

You have joined few tables. Now laravel has many Ids. You have to tell Laravel which id in where clause Laravel should use..

WPModel::where('id', $wpid)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top