jeudi 17 mai 2018

error Undefined property: Illuminate\Database\Eloquent\Collection

Laravel 5.3 I am trying to get the fields from tables users and mobiles

users table has a primary key id and my mobiles tables has a field users_id

so I want to get all the numbers of all users_id

select u.*,m.mobile from users u 
left join mobiles m on m.users_id = u.id
where m.mobiletypes_id = 1 and m.deleted_by is null

I am trying the above query in eloquent but I am facing an error undefined property illuminate database eloquent collection

my user model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

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

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


    public function mobile()
    {
        return $this->hasMany('App\mobile','users_id');
    }

    public function userdetails()
    {
        return $this->hasOne('App\Userdetails');
    }

}

my mobile model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class mobile extends Model
{
    protected $primaryKey = 'mobiles_id';
    protected $fillable = ['mobile', 'mobiletypes_id', 'users_id', 'created_by', 'updated_by', 'deleted_by', 'created_at', 'updated_at'];




}

my controller

public function employeeview(){
   // $user = User::all(); // get all user
    //$userdetails = User::with('userdetails','mobile')->get(); // get all userdetails 
    $mobile = User::with('mobile')->get();

    $userdetails = User::all();
    //return $user;
    //return view('employeeview',compact('userdetails'));
    return view('employeeview')->with('userdetails', $userdetails);


}

my view employeeview.blade.php

 @foreach($userdetails as $u)
                                   
@endforeach



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire