我正在尝试插入邮递员发送的JSON,并希望将其存储在数据库中,但它没有插入数据库中。我正在使用雄辩的模型来处理数据库。
这是我的JSON
{"mcode":"123","code":"321","title":"Mr.", "name":"sameer","mobile":"7798616828","email":"sameer@waev.in","address":"amravati","state":"maharashtra", "city":"amravati"}
这是我的控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\member;
class inquiryController extends Controller
{
public function store(Request $request)
{
$member = new member;
// $member->title=$request->title;
$member->mcode = $request->mcode;
$member->code = $request->code;
$member->title = $request->title;
$member->name = $request->name;
$member->mobile = $request->mobile;
$member->email = $request->email;
$member->address = $request->address;
$member->state = $request->state;
$member->city = $request->city;
$member->save();
return "ok";
}
}
这是我的模特
namespace App;
use Illuminate\Database\Eloquent\Model;
class member extends Model
{
//
protected $fillable=['mcode','code','title','name','mobile','email','address','state', 'city'];
}
答案 0 :(得分:0)
您需要将所有可访问的字段放在其中:
protected $fillable = [ 'name', 'mcode' ....];
或者不声明此字段 - 这将导致您将能够添加不是质量大小的数据(即$member->fill($data)
)。这与上面例子中的情况非常相似。
答案 1 :(得分:0)
嗯,$fillable
变量用于控制器中的质量分配,尝试使用以下代码段来查看它是否有效:
// App\Person.php
public class Person {
$fillable = ['name', 'age'];
}
App\Controllers\PersonController.php
public class PersonController extends Controller {
public function store(Request $request) {
$data = $request->only('name', 'age'); // you can use Input::all() too
return App\Person::create($data);
}
}
由于经度,我没有修改你的代码,而是我使用了一个较小的例子,但它是一样的想法。