SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ 0”(laravel)

时间:2019-02-03 06:57:12

标签: laravel

模型

class Order extends Model {
    protected $attributes = [ 'status_fa' , 'type_fa' , 'delivery_type_fa' ];
.....
}

控制器

$request->validate( [
        'category_id' => 'required|numeric' ,
        'file_id'     => 'required|numeric' ,
        'file_id'     => 'required|numeric' ,
        'origin'      => 'required|numeric' ,
        'goal'        => 'required|numeric' ,
        'subject'     => 'required' ,
        'time'        => 'required'
    ] );

    $order = Order::create( $request->only( 'category_id' , 'file_id' , 'origin' , 'goal' , 'subject' , 'description' , 'time' ) + [
            'user_id' => Auth::id()
        ] );

    return response( [ 'code' => 1 , 'order' => $order ] );

SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'0'

3 个答案:

答案 0 :(得分:0)

$ attributes数组用于定义模型上属性的默认值(如果需要)。因此,应该更像是:

protected $attributes = [ 'status_fa' => 'Not Started', 'type_fa' => 'Large', 'delivery_type_fa' => 'Express' ];

但是,我认为您打算使用$ fillable数组。这定义了要批量分配的属性。

protected $fillable = [ 'status_fa' , 'type_fa' , 'delivery_type_fa' ];

如果属性未在$ fillable数组中列出,则它们不包含在保存到数据库的请求中。

答案 1 :(得分:0)

尝试一下。

class Order extends Model {
    protected $table= 'databse table name';
    protected $fillable = [ 'status_fa' , 'type_fa' , 'delivery_type_fa' ];
    protected $hidden = [id and those column that you dont wanna show in return request]
.....
}

答案 2 :(得分:0)

对不起,我写了一个完整的错误。我应该用这个

public class AppDataDBHelper extends SQLiteOpenHelper{
private static String TAG = "AppDataDBHelper"; // Tag just for the LogCat window
private static String DB_NAME = "appdata.db";// Database name
private static String DB_TABLE = "Bonus_Data";
private static int DB_VERSION = 1;

private SQLiteDatabase mDataBase;
private final Context mContext;

public AppDataDBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void createDataBase() throws IOException {
    //If the database does not exist, copy it from the assets.

    Log.e(TAG, "Entered createDatabase");
    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        this.close();
        try {
            //Copy the database from assets
            Log.e(TAG, "createDatabase passing to copyDatabase");
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } catch (IOException mIOException) {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase() {
    Log.e(TAG, "entered checkDatabase");
    File dbFile = new File(mContext.getDatabasePath(DB_NAME).getPath());
    if (dbFile.exists()) return true;
    File dbdir = dbFile.getParentFile();
    if (!dbdir.exists()) {
        dbdir.mkdirs();
    }
    return false;
}

//Copy the database from assets
private void copyDataBase() throws IOException {
    InputStream mInput = null;
    OutputStream mOutput = null;
    Log.e(TAG, "entered copyDatabase");
    Log.e(TAG, "copyDatabase close");
    String outFileName = mContext.getDatabasePath(DB_NAME).getPath();

    try {
        mInput = mContext.getAssets().open(DB_NAME);
        mOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = mInput.read(buffer)) > 0) {
            mOutput.write(buffer, 0, length);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    } catch (IOException ie) {
        throw new Error("Copydatabase() error ");
    }
}

public ArrayList<String> readNames() {
    boolean distinct = true;

    ArrayList<String> mname = new ArrayList<>();

    String[] columns = new String[]{"name"};
    //Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    //This is used to draw distinct data values from database table
    Cursor cursor = mDataBase.query(distinct, DB_TABLE, columns, null, null, null, null, null, null);
    int iName = cursor.getColumnIndex("name");

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        mname.add(cursor.getString(iName));
    }

    cursor.close();
    return mname;
}
//this openRead is needed everytime you access this helper class 
public void openRead() {
    mDataBase = getReadableDatabase(); //mDatabase should always be initialized or else app crashes while accessing TABLE
}
}