Laravel-Composite Primary Key
Laravel-Composite Primary Key
Laravel-Composite Primary Key
It seems you've hit something that is not currently possible with Laravel, since >increments()already sets ->primary() , so when you add it yourself you end up with two PRIMARY clauses in the resulting SQL. But you may want to try creating the table with the wrong primary key, dropping it, then recreating it: Schema::create("kitchen", function($table) { $table->increments('id'); $table->integer('restaurant_id'); $table->string('name'); }); Schema::table('kitchen', function($table) { $table->dropPrimary('kitchen_id_primary'); }); Schema::table('kitchen', function($table) { $table->primary(array('id', 'restaurant_id')); });
share|improve this answer
Laravel is a web application framework with expressive, elegant syntax. We believe development should be an enjoyable, creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by implementing
common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching. Laravel Here i will be explaining how to re-populate your form fields when validations fails. For the sake of this tutorial i will be using the default route setup. For detailed explanation of form validation and input handling, please visit Laravel 4 official documentation. You can use Input::old(field_name) to access previously submitted data. To repopulate a field set the default value to Input::old(field_name). Example: {{ Form::text(firstname, Input::old(firstname) }}. Below is the route setup and sample code.
?
8 9
// This is the post handler // our form is processed here Route::post('/', function(){
10 11 12 13 14
15 16 17 18 19 20
);
if ($validator->fails())
21
{
22 23 24 25 26}); 27 28
}
// if validation fails redirect with error and old input value return Redirect::to('/')->withErrors($validator)->withInput();
1 2
// form.blade.php
Related Posts:
Lately Ive seen many new people come to Laravel (which is great) and straight away, theyre comparing auth bundles/packages; which one is better etc. I find this behaviour a little strange. Of course I believe that you shouldnt reinvent the wheel, but if youre a beginner, then the wheel is square.
Laravel can do a lot for us without using packages. A lot! You can also easily send password reminders via email with Laravel. I encourage you to explore the classes within vendor/laravel/framework/src/Illuminate/Auth just to see for yourself. Authentication. Even within the Rails community, theres a lot to be said for not using Devise and just rolling-your-own from scratch with has_secure_password. Laravel can do a lot of what an authentication package would do for us. In my app, Im protecting the majority of routes with before & auth filters.
1 2 3 4 5 6 7 8 9 10 11 12
Route::group(['before' => 'guest'], function() { Route::get('login', 'SessionController@create'); Route::post('login', 'SessionController@store'); }); Route::get('logout', 'SessionController@destroy'); // Run the guest filter before going to these routes. // If the user is not a guest, they will be redirected accordingly // These routes override the user resource routes Route::get('register', 'UsersController@create'); Route::post('register', 'UsersController@store');
// Here I'm doing basic session CRUD. You really only need create(), store() and des
13 14 15 16 17
// Before the user tries to access these resources, authenticate them Route::group(['before' => 'auth', 'https'], function() { // user Resource Route::resource('users', 'UsersController');
// etc.
18 19
});
20 21
Above you will see that I think of everything in terms of resources, and you should too (where applicable). When a user logs into an application, a session for that user is created, when they logout of the app, the session is destroyed. Sounds like a resource to me. Although I dont have the session defined as such, its using the same methods youd find in a resource controller, however, we really only need create(), store() and destroy(). Heres my auth filters.
1 2 3
}); Route::filter('auth', function() { if (Auth::guest()) return Redirect::action('SessionController@create');
4 5 6
7 8
});
So thats my simple authentication. I dont have roles (yet) and will worry about them when I need to. But theres one thing were forgetting. Once a user is logged in, whats to stop them from editing the URL and changing the ID of their group to one that doesnt belong to them? Now we need to implement authorisation, as in what is the user permitted to do within your app? Authorisation. Ive kept this very simple so far, but then again, it depends on your application. A user will have many groups, each group will have many discussions etc. So I needed to make sure that they couldnt access groups and discussions that they dont belong to. I was also paranoid about users editing a form (via Firebug or Webkits dev tools) to manipulate a resource that theyve no permission to. So basically, Im checking these things for all resources. I do a very simple check within the User model. Does this user have access to this group?
1 2 3
/** * Does the user have access to this group? *
4 5 6 7 8
* @param integer $id * @return boolean */ public function hasAccessToGroup($id) { $user = Auth::user();
9 10 11 12 13 14 15 16
}
$groups = $user->groups()->get(); foreach ($groups as $group) { if ($group->id == $id) { return true; } } // User is not permitted to access this group. return false;
17 18
Simple check that returns true or false. In my controller I do a simple check and if it returns true (they do have access), we can continue. Its crude, sure, but it works. Id love to hear alternatives.
1 2 3 4
} // GroupsController.php if (Auth::user()->hasAccessToGroup($id)) { // etc.