我有一个名为CardsController的控制器和一个型号名称Card.I想知道当我在控制器中使用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');
}
}
答案 0 :(得分:1)
按照惯例,&#34; snake case&#34;,该类的复数名称将用作表名,除非明确指定了另一个名称。因此,在这种情况下,Eloquent将假设Flight模型将记录存储在航班表中。
https://laravel.com/docs/5.3/eloquent#eloquent-model-conventions