CakePHP 3 - Conditionally creating newEntity for associated table - php

I have these three tables:
Bookings (hasOne Sessions)
Sessions (hasOne Files) with foreign key booking_id linking to Bookings
Files (belongsTo Sessions) with foreign key session_id linking to Sessions
In the BookingsController, I have two functions:
New (functions the same as the add function)
Confirm (functions like the edit function)
When a user first submits a new Booking data entry, while a newEntity is created and saved for Bookings and Sessions, no newEntity is created and saved for Files. However, when a Booking/Session is being updated and confirmed, that is when a newEntity in Files is created the very first time. Since the Confirm function can be used many times, I use a conditional if statement to determine if an associated Files entry exists or not - if one exists, a newEntity for Files is created, patched and then saved. If not, it is just patched and saved.
In my Confirm function:
public function confirm($id = null)
{
$booking = $this->Bookings->get($id,[
'contain' => ['Sessions']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$booking = $this->Bookings->patchEntity($booking, $this->request->data,[
'contain' => ['Sessions']
]);
$fileTable = TableRegistry::get('Files');
$findFiles = $fileTable->find()->where([
'session_id' => $session['id']
])->first();
if($findFiles == null){
$findFiles = $fileTable->newEntity();
$findFiles = $fileTable->patchEntity($findFiles, $data);
$findFiles->session_id = $booking['session']['id'];
if($fileTable->save($findFiles)){
} else {
}
} else {
$findFiles = $filesTable->patchEntity($findFiles, $data);
if($filesTable->save($findFiles)){
} else {
}
}
if ($this->Bookings->save($booking)) {
$this->Flash->success(__('The booking has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The booking could not be saved. Please, try again.'));
}
$this->set(compact('booking'));
$this->set('_serialize', ['booking']);
}
}
However, when trying to use the Confirm function, I get a integrity constraint violation regarding the booking_id foreign key in the Sessions table. I've pinpointed that by removing all the conditional saving code regarding the Files table, the function works fine, however that means neither a newEntity for Files is created when needed.
The easier method is I think just including Files in the New function, but because some bookings could be cancelled, there could potentially be a lot of empty Files data entries.
Update: Including Model and Confirm function's View and form input.
Below is the View of the Confirm function in BookingsController:
<?= $this->Form->create($booking) ?>
<fieldset>
<legend><?= __('Confirm Booking') ?></legend>
<?php
echo $this->Form->input('session.startdate', ['class'=>'form-control']);
echo $this->Form->input('session.enddate', ['class'=>'form-control']);
echo $this->Form->input('session.no_people', ['class'=>'form-control']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
And the Models for the individual tables.
Bookings:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('bookings');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasOne('Sessions', [
'foreignKey' => 'booking_id'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
return $validator;
}
Sessions:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('sessions');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Bookings', [
'foreignKey' => 'booking_id',
'joinType' => 'INNER'
]);
$this->hasOne('Templates', [
'foreignKey' => 'session_id'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->date('startdate')
->requirePresence('startdate', 'create')
->notEmpty('startdate');
$validator
->date('enddate')
->requirePresence('enddate', 'create')
->notEmpty('enddate');
$validator
->integer('no_people')
->requirePresence('no_people', 'create')
->notEmpty('no_people');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['booking_id'], 'Bookings'));
return $rules;
}
Files:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('files');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Sessions', [
'foreignKey' => 'session_id',
'joinType' => 'INNER'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->allowEmpty('link');
$validator
->allowEmpty('name');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['session_id'], 'Sessions'));
return $rules;
}

The foreign key session_id was a required attribute (this was why Files couldn't ever save). I've changed up the function to the following:
public function confirm($id = null)
{
$booking = $this->Bookings->get($id,[
'contain' => ['Sessions', 'Sessions.Files']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
if($booking->session->file == null){ //checks for existing Files array
$template = $this->Bookings->Sessions->Files->newEntity(); //creates new Files entity if one doesn't exist for the associated session
$template->session_id = $booking->session->id; //fills in the foreign key with the currently active session primary key.
if($this->Bookings->Sessions->Templates->save($template)){ //saves the File entity
} else {
}
}
$booking = $this->Bookings->patchEntity($booking, $this->request->data,[
'contain' => ['Sessions', 'Sessions.Files']
]);
$fileTable = TableRegistry::get('Files');
$file = $fileTable->find('all',[
'contain' => ['Sessions'],
'conditions' => ['Sessions.booking_id' => $booking->id, 'Files.session_id' => $booking->session->id]
])->first();
if($file->session_id != $data['session']['id']){ //checks for any changes in session_id
$file->engineer_id = $data['session']['id']; //changes value to match if there isn't a match
$fileTable->save($template);
}
if ($this->Bookings->save($booking)) {
$this->Flash->success(__('The booking has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The booking could not be saved. Please, try again.'));
}
$this->set(compact('booking'));
$this->set('_serialize', ['booking']);
}
}

Something like this should work. Cake will change the query to an UPDATE if an id exists else uses an INSERT.
public function confirm($id = null)
{
$booking = $this->Bookings->get($id, [
'contain' => ['Sessions' => ['Files']]
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$booking = $this->Bookings->patchEntity($booking, $this->request->data, [
'associated' => ['Sessions' => ['associated' => ['Files']]]
]);
if ($this->Bookings->save($booking)) {
$this->Flash->success(__('The booking has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The booking could not be saved. Please, try again.'));
}
$this->set(compact('booking'));
$this->set('_serialize', ['booking']);
}
}

Related

DropDown: Validate 2 items on 3 only

I have 2 tables, the first is Students with 3 fields (name, firstname, fk_diploma).Then, my second table is named Diplomas and there is 1 field named (type_diploma).
For information, I have 3 values in my field type_diploma:
1) DiplomaA
2) DiplomaB
3) DiplomaC
In my validate system, I want the DiplomaA or DiplomaB to be validated but not the DiplomaC, I must have an error message.
For example: * "Sorry, you do not have the skills for the diplomaC."
Do you have an idea of how I can do that ?
public function store(Request $request)
{
$diploma = Diploma::select('type_diploma')->where('id',$request->fk_diploma)->get();
if($diploma->type_diploma != 'DiplomaC')
{
$request->validate([
'name' => 'required|min:3',
'firstname' => 'required|min:2|max:200',
'fk_diploma' => 'required'
]);
}
$exists = Student::where('name', $request->get('name'))->where('firstname', $request->get('firstname'))->where('fk_diploma', $request->get('fk_diploma'))->count();
if (!$exists){
Student::create($request->all());
return redirect()->route('students.index')
->with('success', 'new data created successfully');
}
else{
return redirect()->route('students.index')
->with('error', 'duplicate');
}
}
My model Diploma
class Diploma extends Model
{
protected $fillable = ['type_diploma'];
public function diplomas(){
return $this->hasMany('App\Student', 'fk_diploma');
}
}
Model Student
class Student extends Model
{
protected $fillable = ['name', 'firstname', 'fk_diploma'];
public function diplomas(){
return $this->belongsTo('App\Diploma' , 'fk_diploma');
}
This is not the best way to do it, but its the only one i could think right now:
1) change the type of your request to public function store(Request $request)
2) Do this in your function:
public function store(dateRequest $request)
{
$diploma = Diploma::select('type_diploma')->where('id',$request->fk_diploma)->get();
if($diploma->type_diploma != 'DiplomaA' && $diploma->type_diploma != 'DiplomaB')
{
$request->validate([
'name' => 'required|min:3',
'firstname' => 'required|min:2|max:200',
'fk_diploma' => 'required'
]);
}
$exists = Student::where('name', $request->get('name'))->where('firstname', $request->get('firstname'))->where('fk_diploma', $request->get('fk_diploma'))->count();
if (!$exists){
Student::create($request->all());
return redirect()->route('students.index')
->with('success', 'new data created successfully');
}
else{
return redirect()->route('students.index')
->with('error', 'duplicate');
}
}
This will work for you:
$this->validate($request, [
'fk_diploma' => 'required|not_in:XXX',
]);
XXX - id of the diploma you don't want to accept

how to append the username in url instead of user_id in cakephp 3

I want my url like this format http://localhost/blog/users/username instead of this http://localhost/blog/users/view/6
I have this code in Users view index.ctp
<?php foreach ($users as $user): ?>
<?= $this->Html->link(__('View Profile'), ['action' => 'view', $user['user']['slug']]) ?>
<?php endforeach; ?>
routes.php
<?php
$routes->connect('/user/*', array('controller' => 'users', 'action' => 'view'));
?>
//public function view($id = null)
public function view($username)
{
$users = $this->Users->get($username, [
'contain' => ['Subjects'] // i have relation
]);
$this->set('users', $users);
$this->set('_serialize', ['user']);
}
I tried this link but it not solved my problem
public function edit($id = null)
{
//$logged_user_id=$this->request->Session()->read('Auth.user.id');
$logged_user_id=$this->Auth->user('id');
if($logged_user_id==$id){
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('User profile successfuly updated.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
} else {
$this->Flash->error(__('You are not allowed to do this.'));
return $this->redirect(['action' => 'index']);
}
}
In index.ctp
<?php foreach ($users as $user): ?>
<?= $this->Html->link(__('View Profile'), ['action' => 'view', $user->username]) ?>
<?php endforeach; ?>
Please change $user->username as per your structure.
You don't have to do anything in your routs.php
the username will receive as an argument of the function view
function view($username){
//Your code
}
The get function uses the model's primary key field. It might be possible to change your primary key to username, but I suspect that will cause you other problems. Instead, try this:
$users = $this->Users->find('first')
->where(['username' => $username])
->contain(['Subjects']);
Also, is there a reason that your variable here is plural ($users)? You should only be getting a single user from this, right?

Save user data on click button

I'm working on laravel 5.4 and I have this code:
public function apply($id){
$user = User::where('id', $id)->get()->first();
$data = [
'name' => $user->first_name,
'family' => $user->last_name,
'email' => $user->email,
'username' => $user->username,
'gender' => $user->gender,
'birthday' => $user->birthday,
'cv' => $user->cv,
'about' => $user->about,
'education' => $user->education,
'experiences' => $user->experiences,
];
$company = Company::get()->first();
Mail::send('emails.apply', $data, function ($message) use ($company)
{
$message->from('noreply#gmail.com', 'Robert Nicjoo');
$message->subject('New Apply');
$message->to($company->email);
});
Mail::send('emails.uapply', $data, function ($message) use ($user)
{
$message->from('noreply#gmail.com', 'Robert Nicjoo');
$message->subject('You Applied successfully');
$message->to($user->email);
});
Session::flash('success', 'Your application was sent to company.');
return redirect()->back()->with('session', $data);
}
This will send email to company when user click on apply button and send user info to them, now I also want to save data of the user include user_id, ad_id and company_id in another table so both user and company owners can have access to their history of applied ads.
I also have this table to save data on:
public function up()
{
Schema::create('applies', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('ad_id')->unsigned();
$table->integer('company_id')->unsigned();
$table->timestamps();
});
Schema::table('ads', function($table) {
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('ad_id')->references('id')->on('ads');
$table->foreign('company_id')->references('company_id')->on('ads');
});
}
but in my controller (first codes) I need to know how to save those information in new table (second codes)?
Update:
Ad Model >>
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ad extends Model
{
protected $fillable = [
'company_id', 'title', 'slug', 'image', 'description', 'address', 'job_title', 'salary',
];
public function company(){
return $this->belongsTo(Company::class);
}
public function category(){
return $this->belongsTo(Category::class);
}
public function location(){
return $this->belongsTo(Location::class);
}
public function employment(){
return $this->belongsTo(Employment::class);
}
}
since your blade is like this:
<a class="btn btn-info btn-round" href="{{ route('apply.btn', Auth::user()->id) }}">
your route should look like
Route::get('apply/{id}', 'ApplyController#apply')->name('apply.btn');
why id only ? because in the discussion we had, i found out that ad_id and company_id was taken from the controller .. then in your controller this should work
public function apply($id)
{
$ad = Ad::first();
$company = Company::first();
$apply = new Apply();
$apply->user_id = $id
$apply->ad_id = $ad->id;
$apply->company_id = $company->id;
$apply->save();
// some more codes //
}
to avoid duplicates using user_id .. add a validation function like
function validateApply(array $data)
{
return Validator::make($data, [
'user_id' => 'required|numeric|unique:apply,user_id,NULL,id,ad_id,'.$data->ad_id,
]);
}
unique:apply - it means it will check the apply table the user_id already applied ..
then in the code above just do
$validateApply= $this->validateApply(['user_id'=>$id,'ad_id'=>$ad->id]);
if(!$validateApply->fails())
{
// do the above code here
}
else
{
// duplicate !!! so do your code here
}
then to retrieve the data assuming apply is already belongsTo the user as well the user hasOne apply
Auth::user()->apply->first()->somefield;
// im not sure how the hasOne works but try
Auth::user()->apply->somefield;
Your Route should be:
Route::post('apply/{$user_id}/{company_id}/{ad_id}','ApplyController#apply');
I think you have created model for ads.
So, simply save data like this:
Your function be like
public function apply(Request $request){
// other code
$apply = new Apply();
$apply->user_id = $request->user_id;
$apply->ad_id = $request->ad_id;
$apply->company_id = $request->company_id;
$apply->save();
// other code
}
And one more thing, You should have ad_id in your post request.

Insert into two tables that are in one to one relationslip laravel

I have 2 tables that are in one to one relationship:
tours :
id|title|content
featured_image:
id|tour_id|name|path
My models FeaturedImage.php:
class FeaturedImage extends Model
{
public function tour()
{
return $this->hasOne('App\Tour');
}
}
Tour.php
class Tour extends Model
{
public function featuredimage()
{
return $this->belongsTo('App\FeaturedImage');
}
}
I want to save tour_id in featured_image table when tour is created. I'm using same form to fill tours table and to upload featured_image.
This is my store method looks like:
public function store(Request $request)
{
//validate the date
$this->validate($request, [
'title' => 'required|max:255',
'content' => 'required'
]);
//store the date
$tour = new Tour;
$tour->title = $request->title;
$tour->content = $request->trip_code;
$tour->save();
$featured_image= new FeaturedImage;
// save featured_image
if($request->hasFile('featured_image')){
$image = $request->file('featured_image');
$filename = $image->getClientOriginalName();
$location = public_path('images/featured_image/'.$filename);
Image::make($image)->resize(800, 600)->save($location);
$featured_image->path= $location;
$featured_image->tour()->associate($tour);
$featured_image->save();
}
//redirect to
Session::flash('success','Tour is successfully created !');
return redirect()->route('tours.show',$tour->id);
}
I'm successful to save data into tours table but unable to save in featured_image table. I 'm getting this error:
Call to undefined method Illuminate\Database\Query\Builder::associate()
I would be thankful if anyone can help me out.
You can user Mass Assignment to create your entries into DB like this:
$this->validate(request()->all(), [
'title' => 'required|max:255',
'content' => 'required'
]);
$tour_inputs = array_only(
$tour_inputs.
[
'title',
'content',
]
);
$tour = Tour::create($tour_inputs);
if($request->hasFile('featured_image')) {
$image = $request->file('featured_image');
$filename = $image->getClientOriginalName();
$location = public_path('images/featured_image/'.$filename);
Image::make($image)->resize(800, 600)->save($location);
$featuredImage = $tour->featuredImage()->save(new FeaturedImage([
'name' => $filename,
'path' => $location,
]));
}
Remember to define the $fillables inside your models, your models should look like this,
do check your relations, that you've made in the models, according to me they aren't correct:
class Tour extends Model
{
protected $fillables = [
'title',
'content',
];
public function featuredImage()
{
return $this->hasOne('App\FeaturedImage');
}
}
class FeaturedImage extends Model
{
protected $fillables = [
'name',
'path',
'tour_id',
];
public function tour()
{
return $this->belongsTo('App\Tour');
}
}
Hope this helps!
From your code the relationships that you have defined are in reverse order.
I mean logically, a Tour has one FeaturedImage and a FeaturedImage belongs to a Tour.
class Tour extends Model
{
//Mass Assignable fields for the model.
$fillable = ['title', 'content'];
public function featuredimage()
{
return $this->hasOne('App\FeaturedImage');
}
}
and
class FeaturedImage extends Model
{
//Mass Assignable fields for the model
$fillable = ['tour_id', 'name', 'path'];
public function tour()
{
return $this->belongsTo('App\Tour');
}
}
Then in your controller
public function store(Request $request)
{
//validate the data
$this->validate($request, [
'title' => 'required|max:255',
'content' => 'required'
]);
//store the data
$tour = Tour::firstOrCreate([ //protection against duplicate entry
'title' => $request->get('title'),
'content' => $request->get('trip_code')
]);
if($tour) //if the Tour exists then continue
{
// save featured_image
if($request->hasFile('featured_image')){
$image = $request->file('featured_image');
$filename = $image->getClientOriginalName();
$location = public_path('images/featured_image/'.$filename);
Image::make($image)->resize(800, 600)->save($location);
$featured_image = $tour->featuredimage()->create([
'path' => $location,
'name' => $filename //if you have this field on your FeaturedImage
}
//you could also have an else block to redirect back if the input doesn't have a file
//redirect to
Session::flash('success','Tour is successfully created !');
return redirect()->route('tours.show',$tour->id);
}
else
{
//if there occurs any error display the error message and redirect back - probably with validation errors or exception errors
Session::flash('error','Error message');
return redirect()->back()->withInput()->withErrors();
}
}
And don't forget to add the mass assignable fields to the $fillable array on your models.
UPDATE
For cases where a single form submission includes database transactions in multiple tables, you should use a try{}catch{} to ensure that either all related transactions run without any issue or neither of the transactions go through - to avoid data discrepancy.
You can rewrite your controller code as
public function store(Request $request)
{
//validate the data
$this->validate($request, [
'title' => 'required|max:255',
'content' => 'required'
]);
//store the data
//use the DB::beginTransaction() to manually control the transaction
//You would ideally want to persist the data to the database only if the input provided by the user
//has valid inputs for Tour as well as FeaturedImage, in case if any one invalid input you do not
//want to persist the data
DB::beginTransaction();
try
{
//firstOrCreate gives protection against duplicate entry for tour with same title and content
$tour = Tour::firstOrCreate([
'title' => $request->get('title'),
'content' => $request->get('trip_code')
]);
//proceed further only if $tour exists
if($tour)
{
// get featured_image
if($request->hasFile('featured_image')){
$image = $request->file('featured_image');
$filename = $image->getClientOriginalName();
$location = public_path('images/featured_image/'.$filename);
Image::make($image)->resize(800, 600)->save($location);
//save the featured_image
$featured_image = $tour->featuredimage()->create([
'path' => $location,
'name' => $filename //if you have this field on your FeaturedImage
}
}
}
catch(\ValidationException $e)
{
//In case of validation error, rollback the database transactions to avoid data discrepancy.
DB::rollBack();
$errors = $e->getMessage();
Session::flash('error', 'Whoops.. Please check the provided inputs');
return redirect()->back()->withInput()->withErrors['errors', $errors];
}
catch(\Exception $e)
{
//In case of any other error, rollback the database transactions to avoid data discrepancy.
DB::rollBack();
$errors = $e->getMessage();
Session::flash('error', 'Whoops.. Something went wrong. Please try again');
return redirect()->back()->withInput()->withErrors['errors', $errors];
}
//If both the transactions to the database i.e. saving the Tour as well as FeaturedImage ran without problem
//Commit to the database
DB::commit();
//redirect to
Session::flash('success','Tour is successfully created !');
return redirect()->route('tours.show',$tour->id);
}
Hope this helps.

How to use Sentry 2 in Laravel 4

I have a Personcontroller and a Festivalcontroller in my laravel4 application. The actions in those controllers can only be accessible by an administrator.
If my database only has a user with test#hotmail.com, that user can access the routes of those 2 controllers. If my database has no user with test#hotmail.com, but it has other users, those other users can't access the routes of those 2 controllers. And when my database has a user with test#hotmail.com, and has other users, everyone can access the routes of those 2 controllers.
I only want the user with email test#hotmail.com to access the routes of those controllers.
I installed Sentry2 by doing this:
In composer.json file require:
"cartalyst/sentry": "2.0.*"
Run
php composer.phar update
In app > config > app.php:
'Cartalyst\Sentry\SentryServiceProvider', => to the providers array
'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry', => to the aliases array
After the installation I made the SentrySeeder file:
<?php
class SentrySeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
DB::table('groups')->delete();
DB::table('users_groups')->delete();
Sentry::getUserProvider()->create(array(
'email' => 'test#hotmail.com',
'password' => "test",
'activated' => 1,
));
$user = Sentry::getUserProvider()->findByLogin('test#hotmail.com');
$adminGroup = Sentry::getGroupProvider()->findByName('Test');
$user->addGroup($adminGroup);
}
}
In my PersonController
class PersonController extends BaseController {
public function index()
{
try
{
$user = Sentry::findUserByLogin('test#hotmail.com');
if ($user)
{
$person = Person::with('user')->orderBy('person_id')->paginate(10);
return View::make('persons.index')
->with('person', $person);
}
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
echo 'User was not found.';
}
}
}
Login action in LoginController
public function login()
{
$input = Input::all();
$rules = array(
'user_email' => 'required',
'user_password' => 'required'
);
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('user_password'));
}
else {
$attempt = Auth::attempt([
'user_email' => $input['user_email'],
'password' => $input['user_password']
]);
if ($attempt) {
return Redirect::to('/home');
}
else {
return Redirect::to('login');
}
}
Store a user in database
public function store()
{
$input = Input::all();
$rules = array(
'user_email' => 'required|unique:users|email',
'user_username' => 'required|unique:users',
);
$validator = Validator::make($input, $rules);
if($validator->passes())
{
$password = $input['user_password'];
$password = Hash::make($password);
$location = new Location();
$person = new Person();
$user = new User();
$person->person_firstname = $input['person_firstname'];
$person->person_surname = $input['person_surname'];
$user->user_username = $input['user_username'];
$user->user_email = $input['user_email'];
$user->user_password = $password;
$location->save();
$person->save();
$user->location()->associate($location);
$user->person()->associate($person);
$user->save();
Session::flash('message', 'Successfully created user!');
return Redirect::to('login');
}
else {
return Redirect::to('persons/create')->withInput()->withErrors($validator);
}
}
Looks like you need to use your own users table and also use Sentry's. So you'll need to add related Sentry's columns to yours. It's easy:
1) Go to vendor\cartalyst\sentry\src\migrations.
2) Create one new migration for every file you see there, example:
php artisan migrate:make add_sentry_groups_table
3) Copy the up() and down() code (ONLY!) to your new migrations.
4) And, for the users migration, you'll have to do some changes:
Instead of Schema::create('users' ... you do Schema::table('users' ..., to add more columns to your table.
Delete all commands for columns that you alread have in your current users table, examples of lines you must delete:
$table->increments('id');
$table->timestamps();
5) Run a normal ´php artisan migrate´.
After that you should have the Sentry's tables ready to work.
EDIT
As you're not using the usual 'email' and 'password' columns, publish Sentry's configuration:
php artisan config:publish cartalyst/sentry
And alter
'login_attribute' => 'user_email',

Categories