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()); //这个是错的'.'这个点不是这个表的单元 而是这个类的子关系 //比如 文章的->评论->投票 也就是获得评论的投票
|-------------------------------------------------------------------------- | Routes File |-------------------------------------------------------------------------- | | Here is where you will register all of the routes in 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. | */ use App\Task; use Illuminate\Http\Request; // Route::get('/', function () { // return view('welcome'); // }); Route::get('/tag/{id}', function ($id) { $project = App\Tag::find($id); echo $project->user; foreach ($project->projects as $tags) { echo $tags; } }); // Route::get('taskC', function(){ // echo 'task'; // $task = App\Task::find(1); // print_r($task); // }); /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- |
<?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('/', ['as' => 'posts.index', 'uses' => 'PostsController@index']); //Route::post('/', ['as' => 'posts.store', 'uses' => 'PostsController@store']); Route::get('/', function () { return view('index'); }); Route::resource('api/posts', 'ApiController'); Route::resource('posts', 'PostsController'); Route::get('tag/{id}', ['as' => 'tags.list', 'uses' => function ($id) { $tag = App\Tag::find($id); return view('tags.list', ['tag' => $tag]); }]);
# 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; $institutes = App\Institute::find(1); // return $institutes->reviews; $user = App\User::find(1); // return $user->doctors; return $user->institutes;