命令行:
php artisan controller:make UserController
This will generate the controller at /app/controller/user.php and user.php.
php artisan db:seed --class=AuthorTableSeederphp artisan db:seed,可以执行多个填充类。该方法是执行的DatabaseSeeder这个类
获取已持久化的用户提交的信息:
Input::old(‘username’)
Querying using raw SQL statements
$sql=" insert into shows(name,rating,actor) VALUES(?,?,?)";
$data1 = array('Doctor Who', '9', 'Matt Smith');
$data2 = array('Arrested Development', '10', 'Jason
Bateman');
$data3 = array('Joanie Loves Chachi', '3', 'Scott
Baio');
DB::insert($sql,$data1);
DB::insert($sql,$data2);
DB::insert($sql,$data3);$sql = "DELETE FROM shows WHERE name = ?";
DB::delete($sql, array('Doctor Who'));
DB::delete($sql, array('Arrested Development'));
DB::delete($sql, array('Joanie Loves Chachi'));
class Show {
public function allShows($order_by = FALSE,
$direction = 'ASC')
{
$sql = 'SELECT * FROM shows';
$sql .= $order_by ? ' ORDER BY ' . $order_by
. ' ' . $direction : '';
return DB::select($sql);
}
}
Route::get("shows",function(){
$shows=new Show();
$shows_by_rating=$shows->allShows('rating','DESC');
dd($shows_by_rating);
});
dd:Dump the passed variables and end the script.
$data1 = array('name' => 'Doctor Who',
'rating' => 9, 'actor' => 'Matt Smith');
$data2 = array('name' => 'Arrested Development',
'rating' => 10, 'actor' => 'Jason Bateman');
$data3 = array('name' => 'Joanie Loves Chachi',
'rating' => 3, 'actor' => 'Scott Baio');
DB::table('shows')->insert(array($data1,$data2,$data3));DB::table('shows')->where('name', 'Doctor Who')
->orWhere('name', 'Arrested Development')
->orWhere('name', 'Joanie Loves Chachi')
->delete();
获取model所有数据。
$shows=Show::all();
echo ‘<h1>All shows</h1>’;
foreach($shows as $show)
{
echo $show->name,’ – ‘.$show->rating . ‘ – ‘. $show->actor .'<br/>’;
}
用户验证:
<?php
class User extends Eloquent {
protected $table = 'users';
private $rules = array(
'email' => 'required|email',
'username' => 'required|min:6'
);
public function validate($input) {
return Validator::make($input, $this->rules);
}
}
Make a route that loads the ORM and tries to save some data:
$user = new User();
$input = array();
$input[’email’] = ‘racerx@example.com’;
$input[‘username’] = ‘Short’;
$valid = $user->validate($input);
if ($valid->passes()) {
echo ‘Everything is Valid!’;
// Save to the database
} else {
var_dump($valid->messages());
}
There are a few other ways to validate our data using models. One way is to use a package
that will handle most of the validation work for us. One great package to use is Ardent, which
can be found at https://github.com/laravelbook/ardent.
Using advanced Eloquent and relationships
Schema::create('show_user', function($table)
{
$table->increments('id');
$table->integer('user_id');
$table->integer('show_id');
$table->timestamps();
});
Create a User.php file in the app/model directory:
class User extends Eloquent {
public function shows()
{
return $this->belongsToMany (‘Show’);
}
}
5. Create a Show.php file in our app/model directory:
class Show extends Eloquent {
public function users()
{
return $this->belongsToMany (‘User’);
}
}
6. Make a route in routes.php to add a new user and attach two shows:
Route::get('add-show', function()
{
$user=new User();
$user->username = 'John Doe';
$user->email = 'johndoe@example.com';
$user->save(); //attach two shows
$user->shows()->attach(1);
$user->shows()->attach(3);
foreach($user->shows()->get() as $show)
{
var_dump($show->name); }
});
Make a route to get all the users attached to a show:
Route::get(‘view-show’, function()
{
$show = Show::find(1)->users;
dd($show);
});
很奇怪attach()方法是怎么插入数据到
上面三张表分别是:(最好在模型设置$table=”name”;
users shows show_user.
是根据名字来的。
把shows改成show不行。
都改成单数
user sho show_user可以。
把show_user改成user_show不行。
因为:
the show_user is derived from the alphabetical order of the related model names.
另外还有一点,user表中的creaetd_at,udpate_at是会存进去的。我们调用$user->save()程序会自动做的。
但是
$user->shows()->attach(1); 就不会了,需要我们自己传进去
$date=date(“Y-m-d H:i:s”);
$user->shows()->attach(1,array(‘created_at’=>$date,’updated_at’=>$date));
调用belongsToMany返回http://laravel.com/api/class-Illuminate.Database.Eloquent.Relations.BelongsToMany.html
BelongsToMany类,调用get方法可以get( array $columns = array('*') )
Execute the query as a “select” statement。
There’s more…
Database relationships can get fairly complicated and this recipe merely scratches the surface
of what can be done. To learn more about how Laravel’s Eloquent ORM uses relationships, view
the documentation at http://laravel.com/docs/eloquent#many-to-many.
该文档部分内容:
Many-to-many relations are a more complicated relationship type. An example of such a relationship is a user with many roles, where the roles are also shared by other users. For example, many users may have the role of “Admin”. Three database tables are needed for this relationship: users
,roles
, and role_user
. The role_user
table is derived from the alphabetical order of the related model names, and should have user_id
and role_id
columns.
We can define a many-to-many relation using the belongsToMany
method:
class User extends Eloquent { public function roles()
{
return $this->belongsToMany('Role');
}}
Now, we can retrieve the roles through the User
model:
$roles = User::find(1)->roles;
If you would like to use an unconventional table name for your pivot table, you may pass it as the second argument to the belongsToMany
method:
return $this->belongsToMany('Role', 'user_roles');
You may also override the conventional associated keys:
return $this->belongsToMany('Role', 'user_roles', 'user_id', 'foo_id');
Of course, you may also define the inverse of the relationship on the Role
model:
class Role extends Eloquent { public function users()
{
return $this->belongsToMany('User');
}}
Creating a CRUD system
To interact with our database, we might need to create a CRUD (create, read, update, and
delete) system. That way, we add and alter our data without needing a separate database
client. This recipe will be using a RESTful controller for our CRUD system.
<?php
class UsersController extends BaseController {
public function getIndex()
{
$users=User::all();
return View::make('users.index')->with('users',$users);
}
public function getCreate()
{
return View::make("users.create");
}
public function postCreate()
{
$user=new User();
$user->username=Input::get("username");
$user->email=Input::get("email"); $user->save();
return Redirect::to('users'); }
//编辑Edit get
public function getRecord($id)
{
$user=User::find($id);
return View::make('users.record')->with('user',$user);
}
//编辑Edit post
public function putRecord()
{
$user = User::find(Input::get('user_id'));
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->save();
return Redirect::to('users');
}
public function deleteRecord()
{
$user = User::find(Input::get('user_id'))
->delete();
return Redirect::to('users');
}
}
路由:Route::controller(‘users’, ‘UsersController’);
仅仅看上面的或许不理解,看官网文档: