【发布时间】:2018-10-17 12:54:09
【问题描述】:
我喜欢将数据保存到数据库。但它显示错误 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vojodb.users' doesn't exist (SQL: insert into users ** 我在 stackoverflow 中搜索了如何解决我的问题。我曾尝试在用户模型中添加 **public $table = "users"。但什么也没发生。请给我解决方案如何解决这个问题。
这是我的代码
//注册控制器
<?php
namespace App\Http\Controllers\Auth;
use App\Models\User;
use App\Models\Band;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Session;
use File;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data, Request $request)
{
}
protected function show(){
return view('auth.register');
}
protected function savesession(Request $request){
Session::put('SimpanNamaBand',$request->input('senderName'));
Session::put('SimpanEmailBand',$request->input('senderEmail'));
Session::put('SimpanUserBand',$request->input('senderUsername'));
Session::put('SimpanPasswordBand',$request->input('senderPassword'));
}
protected function savesessiondua(Request $request){
Session::put('SimpanKotaBand',$request->input('senderCity'));
Session::put('SimpanTanggalBand',$request->input('senderFormedDate'));
Session::put('SimpanGenreBand',$request->input('senderGenre'));
Session::put('SimpanWebsiteBand',$request->input('senderWebsite'));
Session::put('SimpanContactBand',$request->input('senderContact'));
}
protected function lastsession(Request $request)
{
Session::put('SimpanTypeBand', $request->input('soundsLike'));
}
protected function showform(){
return view('auth.registertwo');
//return view('auth.registerv');
}
protected function showformfinal(){
//$print = Session::get('SaveRegister');
return view('auth.final');
//return view('auth.registerv');
}
protected function showformverification(){
return view('auth.verify');
}
protected function test(){
if(Session::has('SimpanNamaBand')){
$value = Session::get('SimpanNamaBand');
var_dump($value);
}
}
protected function store(Request $request){
$user = new User;
$user->username = Session::get('SimpanUserBand');
$user->password = bcrypt(Session::get('SimpanPasswordBand'));
$user->email = Session::get('SimpanEmailBand');
$user->name = Session::get('SimpanNamaBand');
$user->user_type = 'band';
$user->save();
$band = new Band;
$band->city = Session::get('SimpanCityBand');
$band->formed_date = Session::get('SimpanTanggalBand');
$band->genre = Session::get('SimpanGenreBand');
$band->website = Session::get('SimpanWebsiteBand');
$band->contact = Session::get('SimpanContactBand');
$band->sounds_like = Session::get('SimpanTypeBand');
if(!$request->hasFile('verify_file') && !$request->file('verify_file')->isValid()) {
return abort(404, 'File not uploaded!');
}
$filename = $request->session()->get('SimpanNamaBand');
$request->image->move(base_path('public/' .$request->session()->get("SimpanNamaBan"). '/document'), $filename);
$band->verification_file = $filename;
$band->save();
return response()->json(['Data berhasil di simpan']);
}
}
//用户迁移
<?php
use Illuminate\Support\Facades\Schema;
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('username');
$table->string('password');
$table->string('email')->unique();
$table->string('name');
$table->enum('user_type', ['customer', 'band', 'admin'])->default('customer');
$table->string('profile_picture')->nullable();
$table->datetime('last_login')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
//用户模型
<?php
namespace App\Models;
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 = [
'name', 'email', 'password','username'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
protected $table = "users";
// Ambil data dari field yang bersangkutan
public function detail() {
return $this->hasOne('App\Models\\'.ucfirst($this->user_type));
}
}
//波段模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Band extends Model
{
protected $primaryKey = 'user_id';
public $incrementing = false;
public function user() {
return $this->belongsTo(User::class);
}
}
我使用 php artisan make:auth 但我根据需要修改了控制器。是否有可能导致错误?谢谢。
【问题讨论】:
-
先迁移建表,然后在控制器中使用
-
我已经通过迁移制作了表格。
-
你跑
php artisan migrate了吗? -
没有。我想保存数据。我应该回滚迁移吗?
标签: php mysql laravel authentication orm