I wrote some code learn from a php/laravel video tutorial . but the result is not the same. the problem step is:
- create a db table named questions and a model named Question.
- add a function to save data into db.
in this function, use bellow code:
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']; } }
run in browser http://localhost/api/question/add?title=rrrrr&description=tttttt and check db, there will be two same records. like bellow:
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
it is not same result as it is in the video tutorial.
- if I uncomment dump($result); , it will insert 1 record normally.
I am using wampserver on windows, PHP version is 7.3.1. I am a freshman on PHP. So I don't know what is the problem.
rq() is in 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());
});