jeudi 1 octobre 2015

Laravel Model ORM with Join of 2 tables

I have 2 table like this-

QAS

enter image description here

Users

enter image description here

Model for them are-

QA.php

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class QA extends Model
    {
        protected $table = "qas";
        protected $fillable = ['webinar_id', 'subscriber_id', 'question', 'public'];
    }

User.php

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, EntrustUserTrait

    //Both Authorizable and EntrustUserTrait provides can
    //we will use for Authorizable's can in this case.
    {
        Authorizable::can insteadof EntrustUserTrait;
    }

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password', 'enabled'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }

    public function subscribers_lists() {
        return $this->hasMany('App\SubscribersList');
    }

    public function subscribers() {
        return $this->hasManyThrough('App\Subscriber', 'App\SubscribersList');
    }

    public function webinars() {
        return $this->hasMany('App\Webinar');
    }

    public function panelists() {
        return $this->hasMany('App\Panelist', 'customer_id');
    }

    public function panelist_profile() {
        return $this->hasOne('App\Panelist','user_id');    
    }
}

I have a query builder like this-

    $ownQas = DB::table('qas')
                ->join('users', 'users.id', '=', 'qas.subscriber_id')
                ->select(
                            'qas.question as question',
                            'qas.created_at as question_datetime',
                            'users.name as question_name',
                            DB::raw('NOW() - UNIX_TIMESTAMP(qas.created_at) as question_ask_before'),
                            'qas.answer as answer'
                        )
                ->where('qas.webinar_id', '=' , $webinar['id'])
                ->where('qas.subscriber_id', '=' , Auth::user()->id)
                ->where('qas.public', '=' , 0)
                ->get();

I want to convert it in Laravel Model ORM.

I don't have any idea how to o it.

Can anyone please help?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1N4bX2n
via IFTTT

Aucun commentaire:

Enregistrer un commentaire