Laravel - error en consultas

 
Vista:

error en consultas

Publicado por Jose (1 intervención) el 23/06/2016 02:59:29
Cordial Saludo,
soy nuevo en el mundo de laravel y de la programación en si, ando tratando de crear un pequeño programa que consta de recibir unas solicitudes y en este dar respuesta, tengo un problema en la realización de un procedimiento, trato de mostrar el nombre de la dependencia a la que un usuario pertenece y no el ID de la dependencia por el cual tengo la llave foránea, les comparto las migraciones
Dependencias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
 
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateDependenciasTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dependencias', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 60);
            $table->string('email');
            $table->timestamps();
 
        });
 
 
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('dependencias');
    }
}


Usuarios
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
 
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('apellido');
            $table->integer('identificacion')->unique();
            $table->string('email');
            $table->string('password', 60);
            $table->boolean('estado');
            $table->enum('type',['miembro', 'admin'])->default('miembro');
            $table->integer('dependencia_id')->unsigned()->nullable();
            $table->rememberToken();
            $table->timestamps();
 
            $table->foreign('dependencia_id')->references('id')->on('dependencias');
 
 
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

Modelo de Dependencias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
 
namespace App;
 
use Illuminate\Database\Eloquent\Model;
 
class Dependencia extends Model
{
    //
 
 
    protected $table = 'dependencias';
 
    protected $fillable = ['id','name', 'email'];
 
    public function User()
    {
        return $this->belongsTo('App\Dependencia');
    }
 
    public function tipoSolicitud()
    {
        return $this->belongsTo('App\tipoSolicitud');
 
    }
    public function Solicitud($value='')
    {
        return $this->belongsTo('App\Solicitud');
 
    }
}


Modelo de User
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
 
namespace App;
 
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
 
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;
 
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
 
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name','apellido', 'identificacion', 'email', 'password','type', 'dependencias_id'];
 
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

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

    public function Solicitud($value='')
    {
        return $this->belongsTo('App\Solicitud');      
    }

}


controller de user index
1
2
3
4
5
6
7
8
9
10
public function index()
    {
 
        $users = User::orderBy('id','ASC')->paginate(5);
               $users->each(function($users){
            $users->Dependencia;
        });
        return view('admin.users.index-user')
            ->with('users', $users);
    }



cuando intento imprimir en la vista me sale el siguiente error
1
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.user_id' in 'where clause' (SQL: select * from `users` where `users`.`user_id` = 1 and `users`.`user_id` is not null)



Imprimo la vista de esta manera
1
2
3
4
5
6
7
8
9
10
11
<tbody class="text-center">
			@foreach($users as $user)
				<tr>
					<td>{{ $user->identificacion}}</td>
					<td>
						{{ $user->Dependencia->name }}
					</td>
					<td>{{ $user->name }}</td>
					<td>{{ $user->email }}</td></td>
				</tr>
			@endforeach

Espero que alguien me pueda colaborar ya que no logro dar con la solución
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder