我试图了解如何使用"脂肪模型,瘦身控制器"在Laravel 5.2。基本上,我大多理解为什么,以及什么,但不是如何。我已经谷歌搜索了一段时间,我找到了几个页面描述了为什么(以及一些页面描述了为什么不这样做)和什么,但是没有任何页面可以让你很容易理解如何使用瘦控制器来创建胖模型。 / p>
我创建了一个非常基本的Todo列表,没有登录或验证,只是最基本的todo-note功能。这个应用程序基本上使用"瘦模型,脂肪控制器"我想重写应用程序,以便它使用"胖模型,瘦子控制器"代替。
我在MySQL数据库中有三个表:
当我为表创建迁移时,我使用$table->foreign('user_id')->references('id')->on('users');
和$table->foreign('project_id')->references('id')->on('projects');
进行了notes表迁移。由于某种原因,它不起作用,因此在数据库中notes.user_id
和notes.project_id
不是users.id
和projects.id
的外键,这从一开始就是这个想法。我猜测下面的问题并不重要,但如果确实如此,有人请告诉我,以便我可以尝试解决这个问题。
我有以下模型(删除了doc块)
应用\ user.php的:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function notes()
{
return $this->hasMany(Note::class);
}
}
应用\ Project.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
public function notes()
{
return $this->hasMany(Note::class);
}
}
应用\ Note.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
}
我有以下控制器(删除了doc块)
应用\ HTTP \控制器\ UserController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Response;
class UserController extends Controller
{
public function index()
{
try {
$statusCode = 200;
$users = User::orderBy('uname', 'asc')->get()->toArray();
$response = [];
foreach ($users as $user) {
$this_row = array(
'id' => $user['id'],
'name' => $user['uname'],
);
$response[] = $this_row;
}
} catch (Exception $e) {
$statusCode = 400;
} finally {
return Response::json($response, $statusCode);
}
}
}
应用\ HTTP \控制器\ ProjectController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Project;
use Response;
class ProjectController extends Controller
{
public function index()
{
try {
$statusCode = 200;
$projects = Project::orderBy('pname', 'asc')->get()->toArray();
$response = [];
foreach ($projects as $project) {
$this_row = array(
'id' => $project['id'],
'name' => $project['pname'],
);
$response[] = $this_row;
}
} catch (Exception $e) {
$statusCode = 400;
} finally {
return Response::json($response, $statusCode);
}
}
}
应用\ HTTP \控制器\ NoteController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Note;
use App\User;
use App\Project;
use Input;
use Response;
use Redirect;
class NoteController extends Controller
{
public function index()
{
try {
$statusCode = 200;
$notes = Note::where('removed', 0)
->orderBy('time_created', 'asc')->get()->toArray();
$response = [];
foreach ($notes as $note) {
$user = User::find($note['user_id']); // Username for note
$project = Project::find($note['project_id']); // Project name
$this_row = array(
'id' => $note['id'],
'user' => $user['uname'],
'project' => $project['pname'],
'content' => $note['content'],
'completed' => $note['completed'],
'created' => $note['time_created'],
'deadline' => $note['time_deadline']
);
$response[] = $this_row;
}
} catch (Exception $e) {
$statusCode = 400;
} finally {
return Response::json($response, $statusCode);
}
}
public function destroy(Request $request)
{
try {
$statusCode = 200;
$note = Note::find($request->id);
$note->removed = 1;
$note->save();
} catch (Exception $e) {
$statusCode = 400;
} finally {
return $statusCode;
}
}
public function edit($request)
{
try {
$statusCode = 200;
$note = Note::find($request);
$response = array(
'id' => $note['id'],
'content' => $note['content'],
'completed' => $note['completed'],
'deadline' => $note['time_deadline']
);
} catch (Exception $e) {
$statusCode = 400;
} finally {
return Response::json($response, $statusCode);
}
}
public function update(Request $request)
{
try {
$statusCode = 200;
$note = Note::find($request->id);
$note->content = $request->content;
$note->time_deadline = $request->deadline;
if ($request->completed == "true") {
$note->completed = 1;
} else {
$note->completed = 0;
}
$note->save();
} catch (Exception $e) {
$statusCode = 400;
} finally {
return $statusCode;
}
}
public function store(Request $request)
{
try {
$statusCode = 200;
$note = new Note;
$note->user_id = $request->user;
$note->project_id = $request->project;
$note->content = $request->content;
$note->time_deadline = $request->deadline;
$note->save();
} catch (Exception $e) {
$statusCode = 400;
} finally {
return $statusCode;
}
}
}
最后,这是我的app / Http / routes.php(删除了评论)
<?php
Route::get('/', function () {
return view('index');
});
Route::get('/notes', 'NoteController@index');
Route::get('/notes/{id}', 'NoteController@edit');
Route::delete('/notes', 'NoteController@destroy');
Route::put('/notes', 'NoteController@store');
Route::post('/notes', 'NoteController@update');
Route::get('/projects', 'ProjectController@index');
Route::get('/users', 'UserController@index');
Route::group(['middleware' => ['web']], function () {
//
});
完整的代码可以在我的GitHub here找到。
我使用Angular接收控制器发送的JSON。这适用于我当前的页面,但正如您所看到的,我的控制器有很多逻辑,我想转移到模型。我不明白我是怎么做的,所以这就是我的问题:
答案 0 :(得分:6)
你的瘦身控制器可能如下所示,这与你所做的相同:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Project;
class ProjectController extends Controller
{
public function index()
{
$projects = Project::orderBy('pname', 'asc')->get(['id', 'name']);
return response()->make($projects);
}
}
但正如法比奥所说,如果你想更进一步,请查看存储库。这是一篇好文章:https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/
在大多数情况下,我将存储库包装到服务中以创建业务逻辑。 控制器只处理路由,模型仅包含关系或更改器和访问器。但它可能因开发方法而有所不同。
另外,不要在foreach循环中进行db查询,利用Eloquent,例如:
$notes = Note::where('removed', 0)
->with(['user', 'project'])
->orderBy('time_created', 'asc')->get();
你可以这样访问:
foreach($notes as $note)
{
echo $note->user->uname;
}