我是la-ravel的初学者,模型的工作问题如何在项目上工作。
firstly used the api.php to link the page
Route::any('/user',['uses'=>'PagesController@my']);
创建控制器
public function my(Request $request){
$val=validator::make($request->all(),[
'id'=>'required',
'name'=>'required',
'email'=>'required',
'mobile'=>'required'
]);
//return 'rahul';
return response()->json([$val]);
}
model是指向控制器的链接
use App\rahul;
use Validator;
创建模型页面设计
class rahul extends Model{
protected $table = "display";
public function my($data)
{
$save = new rahul;
$save->id = $data['id'];
$save->name = $data['name'];
$save->email = $data['email'];
$save->mobile = $data['mobile'];
$save->save();
return $save->id;
}
}
并且,最后一步将完成它创建数据库之类的 enter image description here 数据库名称是lara2,表名显示简单页面设计
但是如何使用模型的问题
答案 0 :(得分:0)
Route::get('first', 'ApiController@first')->name('first');
Route::post('store', 'ApiController@store')->name('store');
使用api.php中的代码 下一步模型是创建
php artisan make: model Article
模型页面
class Article extends Model{ protected $fillable = ['name', 'email'];}
下一步创建控制器 控制器页面(apicontroller)
public function first(){
return view('first');
}
public function store(Request $request)
{
$article = Article::create($request->all());
return response()->json($article, 201);
}
刀片式网页设计
<form method="POST" action="store">
{{csrf_field()}}
<input type="text" name="name">
<input type="email" name="email">
<input type="submit" name="submit"></form>
迁移是创建文章表名称连接
public function up()
{
Schema::create('Articles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('Articles');
}