我写了一些从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 并检查数据库,将有两个相同的记录。像下面这样:
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());
});
答案 0 :(得分:0)
创建一个名为Questions的数据库表和一个名为Question的模型。
了解有关定义模型here
的信息在您的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
添加了将数据保存到数据库的功能。
查看控制器上的Laravel docs以及如何使用它们。
再次从您的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
Routes / web.php
Route::post('/question', QuestionController@store)->name('ask-question');