/** * Run the database seeds. * * @return void */ public function run() { // $this->call(UserTableSeeder::class); factory(App\User::class, 50)->create()->each(function ($user) { $user->posts()->save(factory(App\Post::class)->make()); $post = App\Post::find(rand(1, App\Post::all()->count())); $user->userPosts()->attach($post); }); }
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('demo/{group_urlname}', function ($group) { $client = DMS\Service\Meetup\MeetupKeyAuthClient::factory(['key' => env('MEETUP_API_KEY')]); $members = $client->getMembers(['group_urlname' => $group, 'fields' => 'bio']); $members = $members->toArray(); // dd($membersArray[0]); return view('members', ['members' => $members, 'group' => $group]); }); Route::get('/', function () { return view('welcome'); }); Route::get('about', 'PagesController@about'); Route::get('posts/create', 'PostsController@create'); Route::post('posts/create', 'PostsController@store'); Route::get('posts/{id}/{slug}', function ($id, $slug) { $post = App\Post::find($id); if ($post && $post->slug === $slug) { return $post; } });
echo "我现在" . $age . "岁!!!"; }]); Route::get('blade', function () { return view('layouts.child'); }); Route::get('model', function () { $s = App\Staff::class; $r = $s::find(1); $rr = $r->photos; dump($rr); }); Route::get('model2', function () { /* * 多态多关联 * */ $post = App\Post::find(1); dump($post->tags->toArray()); $tag = App\Tag::find(1); dump($tag->posts->toArray()); }); /* * 关联查询 * */ Route::get('gl', function () { //有评论的文章 或者对于这个 是 有飞机的航线 //这个has获得的还是本表的内容 has只是个条件 不能获得子表内容 $f = App\Flight::has('fly')->get(); dump($f); //这个才是获得子表的结果 $fly = App\Flight::find(1)->fly; dump($fly->toArray());
<?php get('/', ['middleware' => ['cache.fetch', 'cache.put'], 'uses' => 'HomeController@home']); get('/articles/{slug}', ['as' => 'articles.show', 'uses' => 'HomeController@showArticle']); get('/user/{username}/articles', ['middleware' => ['cache.fetch', 'cache.put'], 'uses' => function ($username) { return 'article of ' . $username; }]); get('auth/github', 'Auth\\AuthController@redirectToProvider'); get('auth/github/callback', 'Auth\\AuthController@handleProviderCallback'); Route::group(['middleware' => 'auth'], function () { get('auth/logout', function () { \Auth::logout(); return redirect('/'); }); resource('posts', 'PostController'); }); get('test/search', function () { $keyword = \Request::get('keyword') || 'lorem'; // Match any fields $posts = App\Post::search($keyword)->paginate(); dd($posts); // Match all fields // Article::search($keyword, true)->paginate(); }); get('test/transaction', function () { \DB::transaction(function () { App\Post::findOrFail(8); $foo = App\Post::find(6)->delete(); }); });
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $post = App\Post::find($id); $post->delete(); return $post; }
| and give it the controller to call when that URI is requested. | */ Route::get('/', function () { /* # Polymorphic Relationship Example # Photo, Staff, Order Models # */ $photo = App\Photo::find(3); /* You Can Check ID [ 1,2,3,4 ] */ $imageable = $photo->imageable; return $imageable; }); Route::get('/many-to-many-polymorphic', function () { $posts = App\Post::find(1); // return $posts->tags; $vieos = App\Video::find(1); // return $vieos->tags; $tags = App\Tag::find(2); // return $tags->posts; foreach ($tags->posts as $post) { echo $post->pivot->tag_id; } // return $tags->videos; return 'Hello World'; }); Route::get('/review', function () { $doctors = App\Doctor::find(1); return $doctors->reviews;