Documente Academic
Documente Profesional
Documente Cultură
Seeding
[usm4n@usm4n-desktop][~]
mysql -u root -p
Enter password:
'driver'
=> 'mysql',
3.
'host'
=> 'localhost',
4.
5.
6.
7.
8.
9.
'prefix'
10.
=> '',
),
1.
<?php
2.
use Illuminate\Database\Migrations\Migration;
3.
use Illuminate\Database\Schema\Blueprint;
4.
5.
6.
7.
/**
8.
9.
10.
* @return void
11.
*/
12.
13.
14.
15.
$table->increments('id');
16.
$table->string('title');
17.
$table->string('read_more');
18.
$table->text('content');
19.
$table->unsignedInteger('comment_count');
20.
$table->timestamps();
21.
$table->engine = 'MyISAM';
22.
});
23.
24.
25.
/**
26.
27.
28.
* @return void
29.
*/
30.
31.
32.
33.
$table->dropIndex('search');
34.
$table->drop();
35.
});
36.
37.
}
}
1.
<?php
2.
3.
use Illuminate\Database\Migrations\Migration;
4.
use Illuminate\Database\Schema\Blueprint;
5.
6.
7.
8.
/**
9.
10.
11.
* @return void
12.
*/
13.
14.
15.
16.
$table->increments('id');
17.
$table->unsignedInteger('post_id');
18.
$table->string('commenter');
19.
$table->string('email');
20.
$table->text('comment');
21.
$table->boolean('approved');
22.
$table->timestamps();
23.
});
24.
25.
/**
26.
27.
28.
* @return void
29.
*/
30.
31.
32.
Schema::drop('comments');
33.
34.
35.
The post_id field will help us in defining one to many relationship using Eloquent
ORM seamlessly. We will use approved field of comments table for comments
moderation purpose. We will cover the users table in Authentication Section.
The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord
implementation for working with your database. Each database table has a
corresponding Model which is used to interact with that table.Laravel Eloquent ORM
Documentation
We use singular variants of table names as our Eloqent Model names, this
convention helps the Eloquent to figure out the table to be used with a particular
Model. For instance: if the name of the Model is Post Eloquent will assume a table
with name posts to be used.
Here is the code for our Post and Comment Models:
1.
<?php
2.
// file: app/models/Post.php
3.
4.
5.
6.
7.
return $this->hasMany('Comment');
8.
9.
10.
11.
// file: app/models/Comment.php
12.
13.
14.
15.
16.
17.
return $this->belongsTo('Post');
}
18.
1.
<?php
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
eget accumsan nulla nisi ut nisi. Etiam non convallis ligula. Nulla urna augue,
13.
14.
15.
16.
17.
18.
19.
$post->content = $content;
20.
$post->save();
21.
22.
$maxComments = mt_rand(3,15);
23.
24.
25.
26.
$comment->commenter = 'xyz';
27.
28.
$comment->email = 'xyz@xmail.com';
29.
$comment->approved = 1;
30.
$post->comments()->save($comment);
31.
$post->increment('comment_count');
32.
33.
34.
35.
}
}
The outer for loop creates a new Post Model on each iteration and saves it after
setting the properties: title , read_more , content . The inner loop creates random
number of comments for each Post and increments
the comment_count in posts table.
The Artisan CLI tool provides us with an easy way to interact with Laravel application
from command line. We use artisan tinker command to use the interactive shell. Lets
practice some Eloquent queries on our test data.
[usm4n@usm4n-desktop][~]
artisan tinker
>