[英]laravel model save() will save duplicated data in db
我寫了一些從php / laravel視頻教程學習的代碼。 但是結果不一樣。 問題步驟是:
在此函數中,使用以下代碼:
use Illuminate\\Database\\Eloquent\\Model; class Question extends Model { public function add_question(){ $this->title = rq('title'); $this->user_id = session('user_id'); if(rq('description')) $this->desc = rq('description'); $result = $this->save(); //dump($result); return( $result)? ['status'=> '1' , 'id'=>$this->id] : ['status'=> '0' , 'message'=>'db save fail']; } }
在瀏覽器http:// localhost / api / question / add?title = rrrrr&description = tttttt中運行並檢查db,將有兩條相同的記錄。 像下面這樣:
id title descdescription user_id status created_at updated_at 12 rrrrr tttttt 18 ok 2019/4/7 16:55 2019/4/7 16:55 13 rrrrr tttttt 18 ok 2019/4/7 16:55 2019/4/7 16:55
結果與視頻教程中的結果不同。
rq()在web.php中
function rq($key=null, $default=null){
if(!$key)return Request::all();
return Request::get($key,$default);
}
function question_ins(){
return new App\Question;
}
Route::any('api/question/add', function () {
return(question_ins()->add_question());
});
創建一個名為問題的數據庫表和一個名為問題的模型。
從您的CLI:
php artisan make:model Question --migration
編輯您的問題模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Question extends Model
{
// Just tweak this for whatever your Model looks like
protected $fillable = [
'subject',
'body',
'author'
];
}
編輯您的create_questions_table遷移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('body');
$table->string('subject');
$table->string('author');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('questions');
}
}
在您的CLI中,運行php artisan migrate
添加一個將數據保存到數據庫的功能。
查看有關Controller的Laravel文檔 ,以及如何使用它們。
再次從您的CLI:
php artisan make:controller QuestionController
QuestionController:
<?php
namespace App\Http\Controllers;
use App\Question;
use Illuminate\Http\Request;
class QuestionController extends Controller {
public function store( Request $request )
{
// Create a new instance of your Question Model
//modify input param .
$question = App\Question::firstOrNew([
'body' => $request->body,
'subject' => $request->subject,
'author' => $request->author
]);
// Then save it to the database
$question->save();
return back()->with('success', 'Question posted');
}
}
Question.blade.php
<form method="POST" action="/question">
@csrf
<input type="text" name="body" id="body" class="form-control">
<input type="text" name="subject" id="subject" class="form-control">
<input type="text" name="author" id="author" class="form-control">
<button class="btn btn-primary">Ask Question</button>
</form>
@if( session('success') )
<div class="alert alert-success">
<h3>Question asked!</h3>
</div>
@endif
路線/web.php
Route::post('/question', QuestionController@store)->name('ask-question');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.