2016-10-31 5 views
2

私はCardsControllerとモデル名Cardというコントローラを持っています。コントローラでcard :: all()を使用すると、カードdbテーブルとはどのように関係しているか知りたいと思います。誰かが私に示唆することができる?慣例により特定のモデルとlaravelのコントローラとの関係

cntroller: 

     <?php 

      namespace App\Http\Controllers; 

    use DB; 
    use Illuminate\Http\Request; 
    use App\card; 
    class CardsController extends Controller 
    { 
     // 
     public function index() 
     { 
      # code... 
      // $cards = DB::table('cards')->get(); 
      $cards= card::all(); 
      return view('cards.index',compact('cards')); 
     } 

     public function show($id) 
     { 
      $cards = card::find($id); 
      return view('cards.show',compact('cards')); 
     } 
    } 

Model: 


    <?php 

    namespace App; 

    use Illuminate\Database\Eloquent\Model; 

    class card extends Model 
    { 
     // 
    } 

miggrations table: 

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateCardsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('cards', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('title'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('cards'); 
    } 
} 

答えて

1

別の名前が明示的に指定されていない限り、「蛇のケース」、クラスの複数の名前がテーブル名として使用されます。したがって、この場合、EloquentはFlightモデルがフライトテーブルにレコードを格納すると見なします。

https://laravel.com/docs/5.3/eloquent#eloquent-model-conventions

関連する問題