Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未知列';id';在';其中第'条;在Laravel中定制';id';属性_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 未知列';id';在';其中第'条;在Laravel中定制';id';属性

Php 未知列';id';在';其中第'条;在Laravel中定制';id';属性,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我在Laravel的一个用户模块工作 我已经用一个名为“id\u user”的增量字段创建了表“users” 我已经覆盖了模型的$primaryKey,但它继续使用“id”默认字段进行查询 我也尝试过$key,但都是一样的 我想在我的表“users”中用“id”更改我的“id\u user”字段是可行的,但我需要在我的表中使用自定义id 在这里我粘贴了我的相关文件: Controller:userscocontroller.php class Admin_UsersController exte

我在Laravel的一个用户模块工作

我已经用一个名为“id\u user”的增量字段创建了表“users”

我已经覆盖了模型的
$primaryKey
,但它继续使用“id”默认字段进行查询

我也尝试过
$key
,但都是一样的

我想在我的表“users”中用“id”更改我的“id\u user”字段是可行的,但我需要在我的表中使用自定义id

在这里我粘贴了我的相关文件:

Controller:userscocontroller.php

class Admin_UsersController extends \BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
//GET llama a index, POST llama a store
public function index()
{
    $users = User::paginate();  
    //dd($users); //dump de $users
    return View::make('admin/users/list')->with('users', $users);
   }


/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    $user = new User;
    return View::make('admin/users/form')->with('user', $user);        
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    $user = new User;
    $data = Input::all();
    if ($user->isValid($data)) {
        $user->fill($data);
        $user->save();
        return Redirect::route('admin.users.show', array($user->id));
    }else{
        return Redirect::route('admin.users.create')->withInput()->withErrors($user->errors);
    }
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $user =  User::find($id);
    if (is_null($user)) 
    {
        App::abort(404);
    }

    return View::make('admin/users/profile')->with('user', $user);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    $user = User::find($id);
    if (is_null ($user))
    {
    App::abort(404);
    }

    return View::make('admin/users/form')->with('user', $user);

}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{

    $user = User::find($id);

    if (is_null ($user))
    {
        App::abort(404);
    }

    $data = Input::all();


    if ($user->isValid($data))
    {
        $user->fill($data);
        $user->save();
        return Redirect::route('admin.users.show', array($user->id_user));
    }
    else
    {
        return Redirect::route('admin.users.edit', $user->id_user)->withInput()->withErrors($user->errors);
    }
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    //
}
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

/**
 * The database table used by the model.
 *
 * @var string
 */
public $primaryKey='id_user';
    public  $table = 'users';
public $errors;
protected $fillable = array('email', 'fullname', 'password','user', 'address', 'rank');
protected $perPage = 2; 


/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

public function isValid($data)
{
    $rules = array(
        'user'  => 'required|min:4|max:15|unique:users',
        'password'  => 'required|min:7|confirmed',
        'email'     => 'required|email|unique:users',
        'fullname' => 'required|min:4|max:40',
        'address'  => 'required|min:8',
        'rank'  => 'required'
    );

    // Si el usuario existe:
    if ($this->exists)
    {
        $rules['email'] .= ',email,' . $this->id_user;
        $rules['user'] .= ',user,' . $this->id_user;
    }
    else 
    {
        $rules['password'] .= '|required';
    }

    $validator = Validator::make($data, $rules);

    if ($validator->passes())
    {
        return true;
    }

    $this->errors = $validator->errors();

    return false;
}

}
return array(

/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'driver' => 'eloquent',

/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/

'model' => 'User',

/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/

'table' => 'users',

/*
|--------------------------------------------------------------------------
| Password Reminder Settings
|--------------------------------------------------------------------------
|
| Here you may set the settings for password reminders, including a view
| that should be used as your password reminder e-mail. You will also
| be able to set the name of the table that holds the reset tokens.
|
| The "expire" time is the number of minutes that the reminder should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/

'reminder' => array(

    'email' => 'emails.auth.reminder',

    'table' => 'password_reminders',

    'expire' => 60,

),

);
}

模型:User.php

class Admin_UsersController extends \BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
//GET llama a index, POST llama a store
public function index()
{
    $users = User::paginate();  
    //dd($users); //dump de $users
    return View::make('admin/users/list')->with('users', $users);
   }


/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    $user = new User;
    return View::make('admin/users/form')->with('user', $user);        
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    $user = new User;
    $data = Input::all();
    if ($user->isValid($data)) {
        $user->fill($data);
        $user->save();
        return Redirect::route('admin.users.show', array($user->id));
    }else{
        return Redirect::route('admin.users.create')->withInput()->withErrors($user->errors);
    }
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $user =  User::find($id);
    if (is_null($user)) 
    {
        App::abort(404);
    }

    return View::make('admin/users/profile')->with('user', $user);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    $user = User::find($id);
    if (is_null ($user))
    {
    App::abort(404);
    }

    return View::make('admin/users/form')->with('user', $user);

}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{

    $user = User::find($id);

    if (is_null ($user))
    {
        App::abort(404);
    }

    $data = Input::all();


    if ($user->isValid($data))
    {
        $user->fill($data);
        $user->save();
        return Redirect::route('admin.users.show', array($user->id_user));
    }
    else
    {
        return Redirect::route('admin.users.edit', $user->id_user)->withInput()->withErrors($user->errors);
    }
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    //
}
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

/**
 * The database table used by the model.
 *
 * @var string
 */
public $primaryKey='id_user';
    public  $table = 'users';
public $errors;
protected $fillable = array('email', 'fullname', 'password','user', 'address', 'rank');
protected $perPage = 2; 


/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

public function isValid($data)
{
    $rules = array(
        'user'  => 'required|min:4|max:15|unique:users',
        'password'  => 'required|min:7|confirmed',
        'email'     => 'required|email|unique:users',
        'fullname' => 'required|min:4|max:40',
        'address'  => 'required|min:8',
        'rank'  => 'required'
    );

    // Si el usuario existe:
    if ($this->exists)
    {
        $rules['email'] .= ',email,' . $this->id_user;
        $rules['user'] .= ',user,' . $this->id_user;
    }
    else 
    {
        $rules['password'] .= '|required';
    }

    $validator = Validator::make($data, $rules);

    if ($validator->passes())
    {
        return true;
    }

    $this->errors = $validator->errors();

    return false;
}

}
return array(

/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'driver' => 'eloquent',

/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/

'model' => 'User',

/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/

'table' => 'users',

/*
|--------------------------------------------------------------------------
| Password Reminder Settings
|--------------------------------------------------------------------------
|
| Here you may set the settings for password reminders, including a view
| that should be used as your password reminder e-mail. You will also
| be able to set the name of the table that holds the reset tokens.
|
| The "expire" time is the number of minutes that the reminder should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/

'reminder' => array(

    'email' => 'emails.auth.reminder',

    'table' => 'password_reminders',

    'expire' => 60,

),

);
迁移

public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id_user');
        $table->string('user');
        $table->string('password');
        $table->string('email');
        $table->string('fullname');
        $table->string('address');
        $table->string('rank');
        $table->string('avatar');
        $table->timestamps();
    });
}
Auth.php

class Admin_UsersController extends \BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
//GET llama a index, POST llama a store
public function index()
{
    $users = User::paginate();  
    //dd($users); //dump de $users
    return View::make('admin/users/list')->with('users', $users);
   }


/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    $user = new User;
    return View::make('admin/users/form')->with('user', $user);        
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    $user = new User;
    $data = Input::all();
    if ($user->isValid($data)) {
        $user->fill($data);
        $user->save();
        return Redirect::route('admin.users.show', array($user->id));
    }else{
        return Redirect::route('admin.users.create')->withInput()->withErrors($user->errors);
    }
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $user =  User::find($id);
    if (is_null($user)) 
    {
        App::abort(404);
    }

    return View::make('admin/users/profile')->with('user', $user);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    $user = User::find($id);
    if (is_null ($user))
    {
    App::abort(404);
    }

    return View::make('admin/users/form')->with('user', $user);

}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{

    $user = User::find($id);

    if (is_null ($user))
    {
        App::abort(404);
    }

    $data = Input::all();


    if ($user->isValid($data))
    {
        $user->fill($data);
        $user->save();
        return Redirect::route('admin.users.show', array($user->id_user));
    }
    else
    {
        return Redirect::route('admin.users.edit', $user->id_user)->withInput()->withErrors($user->errors);
    }
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    //
}
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

/**
 * The database table used by the model.
 *
 * @var string
 */
public $primaryKey='id_user';
    public  $table = 'users';
public $errors;
protected $fillable = array('email', 'fullname', 'password','user', 'address', 'rank');
protected $perPage = 2; 


/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

public function isValid($data)
{
    $rules = array(
        'user'  => 'required|min:4|max:15|unique:users',
        'password'  => 'required|min:7|confirmed',
        'email'     => 'required|email|unique:users',
        'fullname' => 'required|min:4|max:40',
        'address'  => 'required|min:8',
        'rank'  => 'required'
    );

    // Si el usuario existe:
    if ($this->exists)
    {
        $rules['email'] .= ',email,' . $this->id_user;
        $rules['user'] .= ',user,' . $this->id_user;
    }
    else 
    {
        $rules['password'] .= '|required';
    }

    $validator = Validator::make($data, $rules);

    if ($validator->passes())
    {
        return true;
    }

    $this->errors = $validator->errors();

    return false;
}

}
return array(

/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'driver' => 'eloquent',

/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/

'model' => 'User',

/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/

'table' => 'users',

/*
|--------------------------------------------------------------------------
| Password Reminder Settings
|--------------------------------------------------------------------------
|
| Here you may set the settings for password reminders, including a view
| that should be used as your password reminder e-mail. You will also
| be able to set the name of the table that holds the reset tokens.
|
| The "expire" time is the number of minutes that the reminder should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/

'reminder' => array(

    'email' => 'emails.auth.reminder',

    'table' => 'password_reminders',

    'expire' => 60,

),

);
知道我做错了什么吗?为什么它在默认情况下一直查询“id”而不是我的自定义“id\u用户”


谢谢

错误似乎出现在
存储
方法中

更改

return Redirect::route('admin.users.show', array($user->id));
return Redirect::route('admin.users.show', array($user->id_user));

return Redirect::route('admin.users.show', array($user->id));
return Redirect::route('admin.users.show', array($user->id_user));

我对验证程序类有问题。它没有使用我分配的$primaryKey,因为电子邮件的规则是错误的

这是正确的:

$rules['email'] .= ',' . $this->id_user . ',id_user'; 

其中第三个参数(id\u user)是进行查询时使用的主键。

可能重复:My bad,这是一个粘贴错误。我的代码中有$user->id\u user,但它仍然按“id”搜索,而不是按id\u user搜索:\