laravel - how to give alias name to eloquent model

eloquent, laravel

Solution

You can use the `Model::from` method for this

<?php

    class User extends Model
    {
      use Eloquence, Mappable;

      public $id;
      public $name
    }
    $data= User::from('User as u')
             ->select('distinct u.name')
             ->join('Member as m','u.id','m.userId')
             ->whereRaw('u.name is not null')
             ->get();

NOTE: I see a lot upvotes, but this approach isn't good practice. It's better to find a different approach. My advice stay on the common walked paths.

Problem

``` <?php namespace App\Http\Models; use Illuminate\Database\Eloquent\Model; use Sofa\Eloquence\Eloquence; use Sofa\Eloquence\Mappable; class User extends Model { use Eloquence, Mappable; public $id; public $name } $data= User::select('distinct User.name')->join('Member as m','User.id','m.userId')->whereRaw('User.name is not null')->get(); ``` I want to avoid Using table name all time instead i want to use alias name.

Original source

Related problems