0

I have a User model which is a parent and Project model which is a child. I created a one-to-many relationship between these two like below.

User Model:

    class User extends Authenticatable
      {
       use Notifiable;

       /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
       protected $fillable = [
         'username', 'email', 'password',
       ];
       /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
       protected $hidden = [
         'password', 'remember_token',
       ];

       /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
       protected $casts = [
         'email_verified_at' => 'datetime',
       ];

       public function projects(){
          return $this->hasMany('App\Project', 'user_id');
       }
    }

Project Model:

    class Project extends Model
    {
       // Table Name
       protected $table = 'projects';
       //Primary Key
       protected $primaryKey = 'project_id';
       // Timestamps
       public $timestamps = true;

       protected $guarded = [];

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

when applying where clause on user model and then getting its related projects:

    class HomeController extends Controller
    {
        public function createProject(Request $request){
           $client = User::where('email', $request->input('client'))->projects;
        }
    }

getting error

   Exception
   Property [projects] does not exist on the Eloquent builder instance.

but when doing

    $client = User::find(id)->projects;

above query is giving me results.

Result Expected: i want to get the User model data by WHERE() clause instead of Find() clause and then gets its related projects.

habib
  • 89
  • 1
  • 10

4 Answers4

0
class HomeController extends Controller
    {
        public function createProject(Request $request){


     $client = User::with('projects')->where('id');
        }
    }
iAmGroot
  • 950
  • 1
  • 6
  • 22
0

As the Error Says that you dont have property in the Builder

 $client = User::where('email', $request->input('client'))->projects;

try this

$client = User::with('projects')->where('email', $request->input('client'))->first()->projects;

here we are getting the user with the specific email and loading the realtion and here you get the relation as object

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
0

The source of your issue is that you have not yet retrieved any users. Before calling first() or get() on the query builder, you are limited to functions of the query builder.

Short version: call first() before accessing the projects

 $client = User::query()
     ->where('email', $request->input('client'))
     ->first()
     ->projects;

Optional: add with('projects') to eager load the projects. This doesn't add any performance bonus in your case though, as you are only loading a single model.

Namoshek
  • 6,394
  • 2
  • 19
  • 31
0

In HomeController this line will retrun collection of array.... In simple words it will return multiple records....
$client = User::where('email', $request->input('client'))->projects;

As you want single record use first (). To retrive single record... It will retrun first matching record...

$client = User::where('email', $request->input('client'))->first()->projects; 
Varsha Jadhav
  • 81
  • 1
  • 5