Retrieve the attributes passed to a factory within the definition method - Laravel 9 - laravel-9

I've been trying to find a way to retrieve the attributes passed to a factory within the definition method but have no luck, I first attempted to access the $this->states property (within the definition method) which returns a closure and then attempted to retrieve the attributes from there but have had no luck with this.
I am currently using the factories below:-
<?php
namespace Database\Factories;
use App\Models\Developer;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* #extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Developer>
*/
class DeveloperFactory extends Factory
{
/**
* Specify the corresponding model for the factory
*
* #var string $model
*/
protected $model = Developer::class;
/**
* Define the model's default state.
*
* #return array<string, mixed>
*/
public function definition()
{
return [
'name' => $this->faker->firstName
];
}
public function configure()
{
$this->afterCreating(function (Developer $developer) {
User::factory()->create([
'userable_type' => $developer->getMorphClass(),
'userable_id' => $developer->id
]);
});
}
}
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
/**
* TODO: get any attributes that are passed into this factory
* e.g.userable_type, userable_id
*
* If these attributes are passed into the factory, stop the faker randomly generating a
* factory for a random user type and use the one passed into the factory
*/
$userableModel = (new $this->faker->userTypeModel)->factory()->create();
return [
'userable_type' => $userableModel->getMorphClass(),
'userable_id' => $userableModel->id,
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => bcrypt('admin1234'), // password
'remember_token' => Str::random(10)
];
}
/**
* Indicate that the model's email address should be unverified.
*
* #return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}

Related

JWTAuth always returns false in Laravel 6

I want to create a Laravel 6 backend with JWT authentication and when I want to sign in a user, JWTAuth always returns false, I google it but can't find any solution for it.
here are my project files codes,
this is my UserController
<?php
namespace App\Http\Controllers;
use App\Http\Requests\UserRequest;
use App\User;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;
class UserController extends Controller
{
public function signup(Request $request)
{
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->save();
return response()->json(['message'=>'User Created Successfully!',$user],201);
}
public function signin(Request $request)
{
$credentials = [];
$credentials['email'] = $request->input('email');
$credentials['password'] = bcrypt($request->input('password'));
try{
if(!$token = JWTAuth::attempt($credentials)){
return response()->json(['error'=>'Invalid Credentials!'],401);
}
}catch(JWTException $e){
return response()->json(['error' => 'Could Not Create Token!'],500);
}
return response()->json(['token'=>$token],200);
}
}
User Model
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* #return mixed
*/
public function getJWTIdentifier() {
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* #return array
*/
public function getJWTCustomClaims() {
return [];
}
public function setPasswordAttribute($password)
{
if ( !empty($password) ) {
$this->attributes['password'] = bcrypt($password);
}
}
}
And Api Route
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('/user','UserController#signup');
Route::post('/user/signin','UserController#signin');
when I want to sign in a user, JWTAuth:attempt($credentials), I don't know what I am missing or wrong?
Is there any solution?

I get this error when I try to register a new user. 'The department id must be an integer.'

I get this error when I try to register a new user: 'The department id must be an integer'. All the answers I'm receiving are helpful just that I get confused at times. So I've added more codes for clarification purposes. I have been battling around for some hours now. Here is the migration file.
Schema::create('departments', function (Blueprint $table) {
$table->bigIncrements('department_id');
$table->integer('department_name');
$table->integer('department_code')->unique();
$table->text('department_discription')->nullable();
$table->tinyInteger('department_status')->default(1);
$table->softDeletes();
$table->timestamps();
This is the DepartmentController codes
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreateDepartmentRequest;
use App\Http\Requests\UpdateDepartmentRequest;
use App\Repositories\DepartmentRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Response;
class DepartmentController extends AppBaseController
{
/** #var DepartmentRepository */
private $departmentRepository;
public function __construct(DepartmentRepository $departmentRepo)
{
$this->departmentRepository = $departmentRepo;
}
/**
* Display a listing of the Department.
*
* #param Request $request
*
* #return Response
*/
public function index(Request $request)
{
$departments = $this->departmentRepository->all();
return view('departments.index')
->with('departments', $departments);
}
/**
* Show the form for creating a new Department.
*
* #return Response
*/
public function create()
{
return view('departments.create');
}
/**
* Store a newly created Department in storage.
*
* #param CreateDepartmentRequest $request
*
* #return Response
*/
public function store(CreateDepartmentRequest $request)
{
$input = $request->all();
$department = $this->departmentRepository->create($input);
Flash::success('Department saved successfully.');
return redirect(route('departments.index'));
}
/**
* Display the specified Department.
*
* #param int $id
*
* #return Response
*/
public function show($id)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Flash::error('Department not found');
return redirect(route('departments.index'));
}
return view('departments.show')->with('department', $department);
}
/**
* Show the form for editing the specified Department.
*
* #param int $id
*
* #return Response
*/
public function edit($id)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Flash::error('Department not found');
return redirect(route('departments.index'));
}
return view('departments.edit')->with('department', $department);
}
/**
* Update the specified Department in storage.
*
* #param int $id
* #param UpdateDepartmentRequest $request
*
* #return Response
*/
public function update($id, UpdateDepartmentRequest $request)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Flash::error('Department not found');
return redirect(route('departments.index'));
}
$department = $this->departmentRepository->update($request->all(), $id);
Flash::success('Department updated successfully.');
return redirect(route('departments.index'));
}
/**
* Remove the specified Department from storage.
*
* #param int $id
*
* #throws \Exception
*
* #return Response
*/
public function destroy($id)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Flash::error('Department not found');
return redirect(route('departments.index'));
}
$this->departmentRepository->delete($id);
Flash::success('Department deleted successfully.');
return redirect(route('departments.index'));
}
}
here is the department code
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Departments
* #package App\Models
* #version September 21, 2020, 4:31 pm UTC
*
* #property integer $department_name
* #property integer $department_code
* #property string $department_discription
* #property boolean $department_status
*/
class Departments extends Model
{
use SoftDeletes;
public $table = 'departments';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $dates = ['deleted_at'];
public $fillable = [
'department_name',
'department_code',
'department_discription',
'department_status'
];
/**
* The attributes that should be casted to native types.
*
* #var array
*/
protected $casts = [
'department_id' => 'integer',
'department_name' => 'integer',
'department_code' => 'integer',
'department_discription' => 'string',
'department_status' => 'boolean'
];
/**
* Validation rules
*
* #var array
*/
public static $rules = [
'department_name' => 'required|integer',
'department_code' => 'required|integer',
'department_discription' => 'nullable|string',
'department_status' => 'required|boolean',
'deleted_at' => 'nullable',
'created_at' => 'nullable',
'updated_at' => 'nullable'
];
}
in your migration, try make 'department_id' the primary key declaratively:
$table->primary('department_id');
then (like sta said in the comment) in your Department model:
protected $primaryKey = "department_id";
or change it's name to just 'id'
you should create a depatment like this:
\App\Models\Department::create([
'department_name' => 1,
'department_code' => 1,
'department_discription' => 'first department',
'department_status' => 1
]);
don't forget to add columns name to fillable variable in your Department model
If I understand your question, you are going to need to change your department_id field in the db.
Create a new migration and for your department_id key, set it like this.
$table->unsignedBigInteger('department_id')->autoIncrement();
That should take care of your issue. What it will do is maintain the integrity of your db in the event that you need to add a relationship because it will create that field the same type as the id on other tables (unsignedBigInteger), but it will also autoIncrement the field.

What does CDBException mean here and how do I resolve it?

Please, I am totally new to Yii1.1, I am following a video tutorial and I have benn trying to follow up closely. I am trying to create and update the album model as indicated in the video tutorial. I typed everything the presenter typed: my codes are given below:
The AlbumController
class AlbumController extends Controller
{
/**
* #var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* #return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* #return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('#'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* #param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Album;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Album']))
{
$model->attributes=$_POST['Album'];
if($model->save()){
//$this->redirect(array('view','id'=>$model->id));
Yii::app()->user->setFlash('saved', 'Data saved!');
$this->redirect(array('update','id'=>$model->id));
}
else{
Yii::app()->user->setFlash('failure', 'Data not saved!');
}
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Album']))
{
$model->attributes=$_POST['Album'];
if($model->save()){
//$this->redirect(array('view','id'=>$model->id));
Yii::app()->user->setFlash('saved', "Data saved!");
$this->redirect(array('update','id'=>$model->id));
}else{
Yii::app()->user->setFlash('failure', "Data not saved!");
}
}
$this->render('update',array(
'model'=>$model,
));
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* #param integer $id the ID of the model to be deleted
*/
}
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Album');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Album('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Album']))
$model->attributes=$_GET['Album'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* #param integer $id the ID of the model to be loaded
* #return Album the loaded model
* #throws CHttpException
*/
public function loadModel($id)
{
$model=Album::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* #param Album $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='album-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
The Album model class
/**
* This is the model class for table "tbl_album".
*
* The followings are the available columns in table 'tbl_album':
* #property integer $id
* #property string $name
* #property string $tags
* #property integer $owner_id
* #property integer $shareable
* #property string $created_dt
*
* The followings are the available model relations:
* #property User $owner
* #property Photo[] $photos
*/
class Album extends CActiveRecord
{
/**
* #return string the associated database table name
*/
public function tableName()
{
return 'tbl_album';
}
/**
* #return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('owner_id, shareable, category_id', 'numerical', 'integerOnly'=>true),
array('name, tags', 'length', 'max'=>255),
array('description', 'length', 'max'=>1024),
array('description', 'match', 'pattern'=>'/[\w]+/u'),// \-\_\'\ \,\p{L}0-!
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('id, name, tags, owner_id, shareable, created_dt', 'safe', 'on'=>'search'),
);
}
/**
* #return array relational rules.
*/
//defined function beforeSave()..
protected function beforeSave(){
if(parent::beforeSave()){
if($this->isNewRecord){
$this->created_dt = new CDbExpression("NOW()");
$this->owner_id = Yii::app()->user->id;
}
return true;
}else
return false;
}
public function scopes(){
return array(
'shareable'=>array(
'order'=>'created_dt DESC',
'condition'=>'shareable=1',
)
);
}
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'photos' => array(self::HAS_MANY, 'Photo', 'album_id'),
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'tags' => 'Tags',
'owner_id' => 'Owner',
'category_id'=>'Category',
'description'=>'Description',
'shareable' => 'Shareable',
'created_dt' => 'Created Dt',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* #return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('tags',$this->tags,true);
$criteria->compare('description',$this->description);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* #param string $className active record class name.
* #return Album the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
The Photo Model Class
/**
* This is the model class for table "tbl_photo".
*
* The followings are the available columns in table 'tbl_photo':
* #property integer $id
* #property integer $album_id
* #property string $filename
* #property string $caption
* #property string $alt_text
* #property string $tags
* #property integer $sort_order
* #property string $created_dt
* #property string $lastupdate_dt
*
* The followings are the available model relations:
* #property Comment[] $comments
* #property Album $album
*/
class Photo extends CActiveRecord
{
private $_uploads;
/**
* #return string the associated database table name
*/
public function tableName()
{
return 'tbl_photo';
}
/**
* #return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('album_id, sort_order', 'numerical', 'integerOnly'=>true),
array('filename', 'length', 'max'=>500),
array('tags', 'length', 'max'=>256),
array('caption, alt_text, created_dt, lastupdate_dt', 'safe'),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('id, album_id, filename, caption, alt_text, tags, sort_order, created_dt, lastupdate_dt', 'safe', 'on'=>'search'),
);
}
/**
* #return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'comments' => array(self::HAS_MANY, 'Comment', 'photo_id'),
'album' => array(self::BELONGS_TO, 'Album', 'album_id'),
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'album_id' => 'Album',
'filename' => 'Filename',
'caption' => 'Caption',
'alt_text' => 'Alt Text',
'tags' => 'Tags',
'sort_order' => 'Sort Order',
'created_dt' => 'Created Dt',
'lastupdate_dt' => 'Lastupdate Dt',
);
}
public function getImageParam(){
if(empty($this->_uploads)){
$this->_uploads = Yii::app()->params['uploads']. "/";
return $this->_uploads;
}
}
public function getUrl(){
return $this->getImageParam()."uploads/".CHtml::encode($this->filename);
}
public function getThumb(){
return $this->getImageParam()."thumbs/".CHtml::encode($this->filename);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* #return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('album_id',$this->album_id);
$criteria->compare('filename',$this->filename,true);
$criteria->compare('caption',$this->caption,true);
$criteria->compare('alt_text',$this->alt_text,true);
$criteria->compare('tags',$this->tags,true);
$criteria->compare('sort_order',$this->sort_order);
$criteria->compare('created_dt',$this->created_dt,true);
$criteria->compare('lastupdate_dt',$this->lastupdate_dt,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* #param string $className active record class name.
* #return Photo the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
The Photo Model
/**
* This is the model class for table "tbl_photo".
*
* The followings are the available columns in table 'tbl_photo':
* #property integer $id
* #property integer $album_id
* #property string $filename
* #property string $caption
* #property string $alt_text
* #property string $tags
* #property integer $sort_order
* #property string $created_dt
* #property string $lastupdate_dt
*
* The followings are the available model relations:
* #property Comment[] $comments
* #property Album $album
*/
class Photo extends CActiveRecord
{
private $_uploads;
/**
* #return string the associated database table name
*/
public function tableName()
{
return 'tbl_photo';
}
/**
* #return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('album_id, sort_order', 'numerical', 'integerOnly'=>true),
array('filename', 'length', 'max'=>500),
array('tags', 'length', 'max'=>256),
array('caption, alt_text, created_dt, lastupdate_dt', 'safe'),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('id, album_id, filename, caption, alt_text, tags, sort_order, created_dt, lastupdate_dt', 'safe', 'on'=>'search'),
);
}
/**
* #return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'comments' => array(self::HAS_MANY, 'Comment', 'photo_id'),
'album' => array(self::BELONGS_TO, 'Album', 'album_id'),
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'album_id' => 'Album',
'filename' => 'Filename',
'caption' => 'Caption',
'alt_text' => 'Alt Text',
'tags' => 'Tags',
'sort_order' => 'Sort Order',
'created_dt' => 'Created Dt',
'lastupdate_dt' => 'Lastupdate Dt',
);
}
public function getImageParam(){
if(empty($this->_uploads)){
$this->_uploads = Yii::app()->params['uploads']. "/";
return $this->_uploads;
}
}
public function getUrl(){
return $this->getImageParam()."uploads/".CHtml::encode($this->filename);
}
public function getThumb(){
return $this->getImageParam()."thumbs/".CHtml::encode($this->filename);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* #return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('album_id',$this->album_id);
$criteria->compare('filename',$this->filename,true);
$criteria->compare('caption',$this->caption,true);
$criteria->compare('alt_text',$this->alt_text,true);
$criteria->compare('tags',$this->tags,true);
$criteria->compare('sort_order',$this->sort_order);
$criteria->compare('created_dt',$this->created_dt,true);
$criteria->compare('lastupdate_dt',$this->lastupdate_dt,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* #param string $className active record class name.
* #return Photo the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
I am getting this error: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (school2go2.tbl_album, CONSTRAINTtbl_album_ibfk_1FOREIGN KEY (owner_id) REFERENCEStbl_user(id) ON DELETE NO ACTION ON UPDATE NO ACTION). The SQL statement executed was: INSERT INTOtbl_album(name,tags,description,shareable,created_dt,owner_id) VALUES (:yp0, :yp1, :yp2, :yp3, NOW(), :yp4)
Please I am totally new to yii and even StackOverflow, pardon my inappropriate editing.I am still learning.
The error translates to: You are trying to insert an album without a corresponding owner.
Impossible to help more without knowing how you got that error.

Laravel 5 - Middleware always restricts access while being logged in

I've got a quite annoying problem at the moment :( hope you can help me out there ...
I'm using the basic login form of laravel at the moment with a pretty much untouched User model.
So, the logging in apparently works (tracked it), but whenever I try to restrict content access (restrict routes) with the help of a middleware, it denies the access.
So, why does it deny me the access if I'm apparently logged in?
I'm really not seeing what I'm doing wrong.
Thanks for your help in advance! (for further information I copied you my middleware, authcontroller and my user model)
<?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 = 'user';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['user_properties_ID', 'email', 'password'];
}
I'm also using the standard AuthController
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* #param \Illuminate\Contracts\Auth\Guard $auth
* #param \Illuminate\Contracts\Auth\Registrar $registrar
* #return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
// $this->middleware('guest', ['except' => 'getLogout', 'getLogin']);
}
}
And my middleware looks like this:
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated {
/**
* The Guard implementation.
*
* #var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* #param Guard $auth
* #return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (! $this->auth->check())
{
// return $next($request);
return response('Unauthorized.', 401);
// return redirect()->intended();
// return new RedirectResponse(url('/trainingsplan-hinzufuegen'));
}
return $next($request);
}
}
routes.php
<?php
/*
|--------------------------------------------------------------------------
| Home
|--------------------------------------------------------------------------
*/
Route::group(['prefix' => '', 'middleware' => 'guest', 'namespace' => 'Modules\TrainaryCore\Http\Controllers', 'as' => 'home'], function()
{
Route::get('/home', 'TrainaryCoreController#render');
});
/*
|--------------------------------------------------------------------------
| Registrierung
|--------------------------------------------------------------------------
*/
Route::group(['prefix' => '', 'namespace' => 'Modules\TrainaryCore\Http\Controllers', 'as' => 'registrieren'], function()
{
Route::get('/registrieren', 'TrainaryCoreController#render');
Route::post('/registrieren', 'RegistrationController#validateForm');
});
/*
|--------------------------------------------------------------------------
| Trainingsplan
|--------------------------------------------------------------------------
*/
Route::group(['prefix' => '', 'middleware' => 'guest', 'namespace' => 'Modules\TrainaryCore\Http\Controllers', 'as' => 'schedule'], function()
{
Route::get('/trainingsplan-hinzufuegen', 'TrainaryCoreController#render');
Route::post('/trainingsplan-hinzufuegen', 'ScheduleController#validateForm');
});
Well, what's happening is ok. If you use the GUEST middleware, it'll only allow you in if you are a guest. You gotta use the AUTH middleware.
okay, Laravel uses as a standard 'id' for tables ...
so, if you want to rename it you have to add following to your Model:
protected $primaryKey = 'ID';
that's the solution boys

Multiple file upload with Symfony2

I'm trying to upload multiple files via a form, but I can only upload one file at a time, the last one I mark in the browser. Is there a way to upload more images with Symfony2 using a simple form?
Here is the twig template of the form I'm using to be able to mark more than one file:
{{ form_widget(form.post_image, { 'attr': {'multiple': 'multiple' }}) }}
Ok binding issue solved (enctype syntax error) : i'll give you the code i use. maybe it will help...
I have a Gallery Entity
class Gallery
{
protected $id;
protected $name;
protected $description;
private $urlName;
public $files; // the array which will contain the array of Uploadedfiles
// GETTERS & SETTERS ...
public function getFiles() {
return $this->files;
}
public function setFiles(array $files) {
$this->files = $files;
}
public function __construct() {
$files = array();
}
}
I have a form class that generate the form
class Create extends AbstractType {
public function buildForm(FormBuilder $builder, array $options) {
$builder->add('name','text',array(
"label" => "Name",
"required" => TRUE,
));
$builder->add('description','textarea',array(
"label" => "Description",
"required" => FALSE,
));
$builder->add('files','file',array(
"label" => "Fichiers",
"required" => FALSE,
"attr" => array(
"accept" => "image/*",
"multiple" => "multiple",
)
));
}
}
Now in the controller
class GalleryController extends Controller
{
public function createAction() {
$gallery = new Gallery();
$form = $this->createForm(new Create(), $gallery);
// Altering the input field name attribute
$formView = $form->createView();
$formView->getChild('files')->set('full_name', 'create[files][]');
$request = $this->getRequest();
if($request->getMethod() == "POST")
{
$form->bindRequest($request);
// print "<pre>".print_r($gallery->getFiles(),1)."</pre>";
if($form->isValid())
{
// Do what you want with your files
$this->get('gallery_manager')->save($gallery);
return $this->redirect($this->generateUrl("_gallery_overview"));
}
}
return $this->render("GalleryBundle:Admin:create.html.twig", array("form" => $formView));
}
}
Hope this help...
NB: If someone know a better way to alter this f** name attribute, maybe in the FormView class or by declaring a new field type, feel free to show us your method...
No extra classes needed (except the gallery_manger service but the issue you describe happens before...)
I don't really know what's wrong. Check for your template (maybe wrong enctype... or name attr missmatching)
first try to do a single file upload, check the documentation:
file Field Type
How to handle File Uploads with Doctrine
Once it works, you have to edit some lines.
add input file multiple attribute.
append [] at the end of the input file name attribute (mine is
create...[] because my form class name is create, if your is
createType it will be createType...[])
init $files as an array.
Copy/paste your code here.
All the suggestions I've found here are workarounds for the real situation.
In order to be able to have multiple attachments, you should use form collection.
Quote from the documentation:
In this entry, you'll learn how to create a form that embeds a collection of many other forms. This could be useful, for example, if you had a Task class and you wanted to edit/create/remove many Tag objects related to that Task, right inside the same form.
http://symfony.com/doc/2.0/cookbook/form/form_collections.html
Example case: You have a document, which form is specified by DocumentType. The document must have multiple attachments, which you can have by defining AttachmentType form and adding it as a collection to the DocumentType form.
For sf > 2.2 :
In you form type class, add this overrided method :
public function finishView(FormView $view, FormInterface $form, array $options) {
$view->vars['form']->children['files']->vars['full_name'] .= '[]';
}
Note that i try to do the same thing in sf2 using this syntax:
In the controller:
public function stuffAction() {
$form = $this->createFormBuilder()
->add('files','file',array(
"attr" => array(
"accept" => "image/*",
"multiple" => "multiple",
)
))
->getForm();
$formView = $form->createView();
$formView->getChild('files')->set('full_name', 'form[files][]');
// name param (eg 'form[files][]') need to be the generated name followed by []
// try doing this : $formView->getChild('files')->get('full_name') . '[]'
$request = $this->getRequest();
if($request->getMethod() == "POST") {
$form->bindRequest($request);
$data = $form->getData();
$files = $data["files"];
// do stuff with your files
}
}
return $this->render('Bundle:Dir:index.html.twig',array("form" => $formView));
}
$files will be an array of uploaded files...
Calling $form->createView() to alter the name attribute is certainly not the best way / cleanest way to do it but it's the only one i found that keeps the csrf functionality working, because altering the name attribute in a twig template makes it invalid...
Now I still have an issue using a form class which generate the form, I don't know why during the binding of the form data & object attached to the form my array of uploaded files is transformed in array of (file) name ???
use this methode :
$form = $this->createFormBuilder()
->add('attachments','file', array('required' => true,"attr" => array(
"multiple" => "multiple",
)))
->add('save', 'submit', array(
'attr' => array('class' => 'btn btn-primary btn-block btn-lg'),
'label' => 'save'
))
->getForm();
then you add [] to the name of your input via jQuery :
<input id="form_attachments" name="form[attachments]" required="required" multiple="multiple" type="file">
jQuery code :
<script>
$(document).ready(function() {
$('#form_attachments').attr('name',$('#form_attachments').attr('name')+"[]");
});
</script>
Here is easy example to upload multiple files. I have similar problem with upload files.
https://github.com/marekz/example_symfony_multiply_files_example
For symfony 3.*
First: Both form declatartion:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Form\FilesType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class UserType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('lastName')
->add('files', CollectionType::class,array(
'entry_type' => FilesType::class,
'allow_add' => true,
'by_reference' => false,
))
;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_user';
}
}
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FilesType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file');
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Files'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_files';
}
}
Now, my entities:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User {
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=255)
*/
private $lastName;
/**
* #ORM\ManyToMany(targetEntity="Files", cascade={"persist"})
*/
private $files;
function __construct() {
$this->files = new ArrayCollection();
}
/**
* Get id
*
* #return int
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return User
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return User
*/
public function setLastName($lastName) {
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* Get files
*
* #return ArrayCollection
*/
function getFiles() {
return $this->files;
}
/**
* Set files
* #param type $files
*/
function setFiles($files) {
$this->files = $files;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Files
*
* #ORM\Table(name="files")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilesRepository")
*/
class Files
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="file", type="string", length=255, unique=true)
* #Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
* #Assert\File(mimeTypes={ "application/pdf" })
*/
private $file;
/**
*
* #return Files
*/
function getUser() {
return $this->user;
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set file
*
* #param string $file
*
* #return Files
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* Get file
*
* #return string
*/
public function getFile()
{
return $this->file;
}
}
Finaly, Symfony Controller:
/**
* Creates a new user entity.
*
* #Route("/new", name="user_new")
* #Method({"GET", "POST"})
*/
public function newAction(Request $request) {
$user = new User();
$form = $this->createForm('AppBundle\Form\UserType', $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$attachments = $user->getFiles();
if ($attachments) {
foreach($attachments as $attachment)
{
$file = $attachment->getFile();
var_dump($attachment);
$filename = md5(uniqid()) . '.' .$file->guessExtension();
$file->move(
$this->getParameter('upload_path'), $filename
);
var_dump($filename);
$attachment->setFile($filename);
}
}
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('user_show', array('id' => $user->getId()));
}
return $this->render('user/new.html.twig', array(
'user' => $user,
'form' => $form->createView(),
));
}
You need to alter the input file name attribute which need to map an array.
<input type="file" name="name[]" multiple />
Methods getChild and set() were removed in 2.3.
Instead of this you should use children[] and vars properties
before:
$formView->getChild('files')->set('full_name', 'form[files][]');
after:
$formView->children['files']->vars = array_replace($formView->children['files']->vars, array('full_name', 'form[files][]'));
Symfony introduced 'multiple' option to file field type in symfony 2.5
$builder->add('file', 'file', array('multiple' => TRUE));
What would happen if there would be some validation errors? Will Symfony Form repost multiple file upload field. Because I tried it and I think for this purpose you need to use collection of file fields. Than symfony form must render all fields have added before correctly.