嗨!我还看了其他问题,但找不到答案。
我想创建数据,但是给我这个错误:
SQLSTATE [HY000]:常规错误:1364字段“ user_id”没有 默认值
控制器:
public function store(BannerRequest $request)
{
Banner::create($request->all());
flash()->success('Success', 'Your banner has been created.');
return back(); // temporary
}
和我的桌子:
Schema::create('banners', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('street', 40);
$table->string('city', 40);
$table->string('zip', 15);
$table->string('country', 40);
$table->string('state', 40);
$table->integer('price');
$table->text('description');
$table->timestamps();
});
这是我的模特:
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Banner;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Photo extends Model
{
protected $table = 'banners_photos';
protected $fillable = ['name', 'path', 'Thumbnail_path'];
protected $file;
public function banners()
{
return $this->belongsTo(Banner::class);
}
public static function formFile(UploadedFile $file)
{
$photo = new static;
$photo->file = $file;
$photo->fill([
'name' => $photo->fileName(),
'path' => $photo->filePath(),
'Thumbnail_path' => $photo->ThumbnailPath()
]);
return $photo;
}
public function fileName()
{
$name = sha1(time() . $this->file->getClientOriginalName());
$extension = $this->file->getClientOriginalExtension();
return "{$name}.{$extension}";
}
public function filePath()
{
return $this->baseDir() . DIRECTORY_SEPARATOR . $this->fileName();
}
public function ThumbnailPath()
{
return $this->baseDir() . '/tn-' . $this->fileName();
}
public function baseDir()
{
return 'images/photos';
}
public function upload()
{
$this->file->move($this->baseDir(), $this->fileName());
$this->makeThumbnail();
return $this;
}
public function makeThumbnail()
{
Image::make($this->filePath())->fit(200)->save($this->ThumbnailPath());
return $this;
}
}
此代码已经有效,并且重构后变得很困难。
感谢您的帮助。
答案 0 :(得分:2)
根据该消息user_id
未设置,因此它可能不在$request->all()
中。
如果存在user_id
,那么您可能要检查user_id是否在$fillable
模型的Banner
属性中。
如果您尝试将横幅广告分配给当前用户,则可以执行以下操作:
$data = $request->all();
$data['user_id'] = $request->user()->id;
Banner::create($data);
假设BannerRequest
中的所有其他数据正确无误,并且用户登录后应该可以使用。
侧注: ->onUpdate('cascade')
看起来很危险,如果修改了用户,数据库将尝试删除标语。不确定这是否是您的意图。
答案 1 :(得分:0)
在您的偏头痛中,您有这个
SELECT
CASE
WHEN a.TwoPartColumn LIKE '%-%'
THEN LEFT(a.TwoPartColumn, CHARINDEX('-', a.TwoPartColumn) - 1)
ELSE a.TwoPartColumn
END AS FirstPart,
CASE
WHEN a.TwoPartColumn LIKE '%-%'
THEN RIGHT(a.TwoPartColumn, LEN(a.TwoPartColumn) - CHARINDEX('-', a.TwoPartColumn))
ELSE '001'
END AS SecondPart,
LEFT(rtrim(b.RightPadThis) + '0000000000', 10) AS RightPadded,
RIGHT('00000000'+ rtrim(b.LeftPadThis,0), 8) AS LeftPadded,
b.Units AS Units,
CONCAT
(
CASE
WHEN a.TwoPartColumn LIKE '%-%'
THEN LEFT(a.TwoPartColumn, CHARINDEX('-', a.TwoPartColumn) - 1)
ELSE a.TwoPartColumn
END,
CASE
WHEN a.TwoPartColumn LIKE '%-%'
THEN RIGHT(a.TwoPartColumn, LEN(a.TwoPartColumn) - CHARINDEX('-', a.TwoPartColumn))
ELSE '001'
END,
LEFT(rtrim(b.ItemCd) + '0000000000', 10),
RIGHT('00000000'+ rtrim(b.LeftPadThis,0))), 8),
b.Units
) AS Concatenated
FROM TableA AS a
JOIN TableB AS b ON b.Id = a.Id
WHERE b.Param = @Param
错误是因为在您的$table->integer('user_id')->unsigned();
中没有request->all()
字段,如果需要,请添加它,如果没有,请在迁移中添加
user_id
答案 2 :(得分:0)
解决方案:
在public function store
中:
$banner = auth()->user()->publish(new Banner($request->all()));
在用户模型中添加public function publish
:
public function publish(Banner $banner)
{
return $this->banners()->save($banner);
}