這是我如何分解控制器邏輯和使用模型的中小型項目。
1.創建表和模型
該命令將創建模型和--migration會創建一個引用,你可以用它來創建模型的表中的藍圖類遷移文件。
php artisan make:model UserLobbyInfo --migration
你似乎已經創建了一個數據庫,所以你可能要刪除的--migration,除非你想用它來創建一個使用藍圖架構。我個人喜歡使用遷移。你的模型將直接在應用程序文件夾下的Laravel 5
2.創建修改模型文件
你會發現在App文件夾中的模型文件。在你的模型中,如果你的模型不遵循Laravel約定,你應該添加你要插入或更新的字段(可批量填充的項目)和你的表的名字(Laravel假定駱駝的外殼表示不同的單詞,並且你的表格以一個's',所以它認爲你的表將是user_lobby_infos,在你的情況下,你的表名是user_lobby_info)。這是我如何根據你的數據在你上面的查詢更新:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserLobbyInfo extends Model
{
// Define mass fillable items
protected $fillable = array('userid','lobby','typing');
//Define table
protected $table = 'user_lobby_info';
}
你如何使用你的模型
這種模式現在已經從它的擴展照亮提供給它的所有方法\數據庫\雄辯\ Model類,所以你可以做以下的多:
//To query all content:
$index = UserLobbyInfo::all();
//To query specific content:
$userLobby = UserLobbyInfo::where('id', '=', 1)->first();
//Save things:
$userLobbyInfo = UserLobbyInfo::where('id', '=', 1)->first();
$userLobbyInfo->lobby = Request::get('lobbyid')
$userLobbyInfo->save();
//Using the model for your query above this is how you can send an update:
UserLobbyInfo::where('userid', '=', Auth::User()->id)
->where('lobby', '=', Request::get('lobbyid'))
->update([ 'typing' => $level ]);
3.創建控制器與CRUD相關的方法預罐頭這個命令將創建一個所有你通常在一個CRUD應用程序使用的方法(指數顯示,創建,保存,編輯,更新,刪除)
php artisan make:controller UserLobbyController --resource
在每一個你想這些功能的控制器用您需要的方法添加相應的模型。
4.添加一個CRUD應用傳統上使用,並鏈接到--resource方法 如果使用--resource,你將能夠使用資源的功能,將你提供的所有的所有路由這些資源所需的路線。
Route::resource('userlobby', 'UserLobbyController');
,在你的路由文件將創建下列路線的CRUD應用程序運行典型的「PHP工匠路線:列表| grep的userlobby」一線你會看到這些路線:
| | GET|HEAD | userlobby | userlobby.index | App\Http\Controllers\[email protected] | web |
| | POST | userlobby | userlobby.store | App\Http\Controllers\[email protected] | web |
| | GET|HEAD | userlobby/create | userlobby.create | App\Http\Controllers\[email protected] | web |
| | GET|HEAD | userlobby/{userlobby} | userlobby.show | App\Http\Controllers\[email protected] | web |
| | PUT|PATCH | userlobby/{userlobby} | userlobby.update | App\Http\Controllers\[email protected] | web |
| | DELETE | userlobby/{userlobby} | userlobby.destroy | App\Http\Controllers\[email protected] | web |
| | GET|HEAD | userlobby/{userlobby}/edit | userlobby.edit | App\Http\Controllers\[email protected] | web |
5 。將你的控制器壓縮到CRUD方法 我只是在下面編輯和更新方法,因爲這會變得很長。但願這給你如何擊穿控制器上的一個想法:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\UserLobbyInfo; // Add this to access your new model
use App\Http\Requests;
class UserLobbyController extends Controller
{
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$updateLobby = UserLobbyInfo::where('id', '=', $id)->first(); //This queries the table specifically for the id, just for demo purposes.
return view('lobbies.edit', compact('updateLobby')); //This will send the above defined array to your view to pre-populate.
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$userLobby = UserLobbyInfo::where('userid', '=', Auth::User()->id)
->where('lobby', '=', $request->lobbyid)->first();
//Grab the UserLobby row you want to update.
$updateLobby->typing = $level; //update the typing value
$updateLobby->save(); // save it.
}
對於更復雜的應用程序,我通常較重控制器邏輯遷移出來的一類,並使用它引用控制器類。在編寫具有多個表連接的複雜查詢時(特別是在該連接中需要多個where子句的連接)時,我也只使用DB :: class。
希望這有助於強調如何在Laravel中正確使用模型。
大量的這些信息可以在laravel文檔中找到。我也喜歡這張備忘單:laravel cheat sheet
還有其他問題,或者如果我沒有完全回答你的問題,請告訴我。
我注意到你的控制器方法是你引用數據庫類而不是模型。你能分享你的模型嗎? – Dom
@DomDaFonte我沒有大部分模型設置。我想這是laravel的一部分,我不認爲是有用的,因爲我使用查詢生成器而不是引用現在更有意義的模型。 – Shawn31313
好吧,我相信我現在明白了這個問題,我會回答一個答案,以澄清如何使用Laravel創建簡單的CRUD MVC應用程序。 – Dom