dimanche 29 novembre 2015

Laravel create simple RelationShip and get User data from other table

i'm created simple RelationShip from two table as User and Merchant :

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

    protected $table = 'users';

    protected $fillable = ['name', 'email', 'password'];

    protected $hidden = ['password', 'remember_token'];

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }
    public function merchant()
    {
        return $this->hasMany('App\Merchant');
    }
}

in App\Merchant is:

class Merchant
{
    protected $table = 'merchand_web_service';
    protected $fillable = ['agent_unique_id', 'agent_company_name', 'agent_company_logo'];

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

and my Migration file is:

class MerchandWebService extends Migration
{
    public function up()
    {
        Schema::create('merchand_web_service',function(Blueprint $table){
            $table->increments('id');
            $table->string('customer_key','50');
            $table->string('company_name','50');
            $table->string('company_logo','100');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('merchand_web_service');
    }
}

now i want to access User data on Merchant table on database by this code on Controller :

$all_records = Merchant::with('user')->orderBy('id', 'DESC')->paginate(50);

Unfortunately i get this error:

Call to undefined method app\Merchant::with()



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire