Php Laravel5.8我无法将数据存储到我的sql server
我正在设置用户配置文件页面,但无法将数据插入mysql服务器 我无法解决这个问题 我在我的个人资料文件夹中有index.blade.php,从索引页面,转到create.blade.php 当我进入提交按钮时,我的页面重定向到index.blade.php, 但在mysql服务器中,没有记录任何内容 web.php create.blade.php Profile.phpPhp Laravel5.8我无法将数据存储到我的sql server,php,database,laravel,laravel-5,store,Php,Database,Laravel,Laravel 5,Store,我正在设置用户配置文件页面,但无法将数据插入mysql服务器 我无法解决这个问题 我在我的个人资料文件夹中有index.blade.php,从索引页面,转到create.blade.php 当我进入提交按钮时,我的页面重定向到index.blade.php, 但在mysql服务器中,没有记录任何内容 web.php create.blade.php Profile.php <?php namespace App; use Illuminate\Database\Eloquent\Mod
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $fillable = [
'user_id','name', 'gender', 'country', 'bod', 'description'
];
}
如果需要更新现有记录,请执行以下操作:
$user_id = auth()->user()->id;
$update = Profile::where('user_id',$user_id)->update([
'name'=>$request->name,
'gender'=>$request->gender,
'country'=>$request->country,
'bod'=>$request->bod,
'description'=>$request->description
]);
if($update){
return redirect()->route('profile.index');
}else{
//do something if fails
}
但如果您需要添加新的配置文件,则:
$create= Profile::create([
'user_id'=>auth()->user()->id,
'name'=>$request->name,
'gender'=>$request->gender,
'country'=>$request->country,
'bod'=>$request->bod,
'description'=>$request->description
]);
if($create){
return redirect()->route('profile.index');
}else{
//do something if fails
}
尝试此操作,假设您正在尝试发布新记录:
public function store(Request $request) {
$profile = new Profile;
$profile->name = $request->input('name');
$profile->gender = $request->input('gender');
$profile->country = $request->input('country');
$profile->bod = $request->input('bod');
$profile->description = $request->input('description');
$profile->save();
return redirect()->route('profile.index');
}
在您的配置文件模型中,将“姓名”、“性别”、“countrybod”和“描述”设置为可填充字段。您的代码仅在配置文件模型存在时更新它。如果不知道记录是否存在,则可能需要尝试该方法。它将更新现有记录,或者,如果没有此类记录,它将创建一个新记录:
公共函数storeRequest$request{
$user\u id=auth->user->id;
Profile::updateOrCreate
['user\u id'=>$user\u id],//搜索要求
[
//用于更新或创建记录的字段
'name'=>请求'name',
“性别”=>请求“性别”,
'国家'=>请求'国家',
'bod'=>请求'bod',
'description'=>请求'description'
]
;
返回重定向->路由'profile.index';
}
在您的用户模型中,创建如下userProfile关系
class User extends Model
{
protected $fillable = ['what ever you want to fill in user model'];
public function userProfile()
{
return $this->hasOne(Profile::class, 'user_id');
}
}
然后,您可以在控制器中像这样更新关系数据
$user = ['user_id'=>auth()->user()->id];
$order->location()->updateOrCreate($user, $arrayOfData);
在你的模型中使用这个
protected $fillable = ['name', 'gender', 'country'];
protected $guarded=[];
或者如果你能用这个
protected $fillable = ['name', 'gender', 'country'];
protected $guarded=[];
当您添加$request'name'时,结果会是什么;运行内部存储功能?您得到的值是否正确?不清楚您是在尝试存储新记录还是更新旧记录。如果是新的,我想您应该使用save而不是update。@GayanS.Muthukumarana“函数名必须是字符串”出现。@fms我想插入第一条记录。我已经给出了答案。尝试一下谢谢,但是没有用,我在尝试dd$request'name'时仍然有一个相同的错误;添加$request->name;,结果是什么?我有个名字,我把它叫做“约翰·多伊”!好的,那么您只需要在您的配置文件模型中添加以下内容:protected$filleble=[姓名、性别、国家、董事会、描述];我可以插入数据,但我需要用户id,因为用户有一个配置文件。您可以在帖子中包含配置文件模型吗?您可以添加$profile->user\u id=auth->user->id;非常感谢。我能做到!但是如何存储编辑的数据呢?我遇到一个错误['user\u id'=>$user\u id],这里没有定义$user\u id。您是否通过运行以下命令分配了$user\u id:$user\u id=auth->user->id;?这只是为了替换Profile::where语句,而不是整个方法体。@yoheimezu:更新了我的答案以显示修改后的store方法的完整代码。
protected $guarded=[];