当我将数据存储到数据库时,某处的某个字段会消失。
这是我的代码:
public function artStore(FacilityRequest $request)
{
$art = new Facility;
$art->category()->attach($request->input('categories_list'));
if(Input::hasfile('image'))
{
$request->file('image')->move(public_path('images/art/'), $request->file('image')->getClientOriginalName());
$art->image = 'images/art/' . $request->file('image')->getClientOriginalName();
}
$art = Facility::create($request->all());
}
类别附加良好,但$art
本身变为空。有人可以告诉我为什么吗?
这是错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'facility_id' cannot be null (SQL: insert into `category_facility_facility` (`category_facility_id`, `facility_id`) values (2, ))
当我做dd($ request)时会发生这种情况;
+query: ParameterBag {#289 ▼
#parameters: []
}
+server: ServerBag {#293 ▼
#parameters: array:30 [▼
"DOCUMENT_ROOT" => "C:\xampp\htdocs\uzdarbis\laravel\public"
"REMOTE_ADDR" => "::1"
"REMOTE_PORT" => "57855"
"SERVER_SOFTWARE" => "PHP 5.6.12 Development Server"
"SERVER_PROTOCOL" => "HTTP/1.1"
"SERVER_NAME" => "localhost"
"SERVER_PORT" => "8888"
"REQUEST_URI" => "/create-art-work"
"REQUEST_METHOD" => "POST"
"SCRIPT_NAME" => "/index.php"
"SCRIPT_FILENAME" => "C:\xampp\htdocs\uzdarbis\laravel\public\index.php"
"PATH_INFO" => "/create-art-work"
"PHP_SELF" => "/index.php/create-art-work"
"HTTP_HOST" => "localhost:8888"
"HTTP_CONNECTION" => "keep-alive"
"HTTP_CONTENT_LENGTH" => "13360"
"HTTP_CACHE_CONTROL" => "max-age=0"
"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
"HTTP_ORIGIN" => "http://localhost:8888"
"HTTP_UPGRADE_INSECURE_REQUESTS" => "1"
"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
"HTTP_CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryF4qxwnPWgFSBZl7r"
"HTTP_REFERER" => "http://localhost:8888/facilities/add-new"
"HTTP_ACCEPT_ENCODING" => "gzip, deflate"
"HTTP_ACCEPT_LANGUAGE" => "lt,en-US;q=0.8,en;q=0.6,ru;q=0.4,pl;q=0.2"
"HTTP_COOKIE" => "XSRF-TOKEN=eyJpdiI6IlByRUloN3djVVgxWDZ2MmpEN0lZRWc9PSIsInZhbHVlIjoiSU5lNkZieWcrY01KTG05Q3M5YmswcHNSQ3A0N05DQ2loTXVWbmszdnZldEl4TytMVkdZMTRYYUIwQjAxdEwwdmRjWFZTMnZtMzkzZUtMZHhnUE5nZWc9PSIsIm1hYyI6IjRjMDEzZTEzNzA0N2M1NWFhNmVlMTQ5Mjc5YmI0MjIzODc0NTRhZGJmZDkyNjJlMTk4ZTM0ZjEwMDc1YTRkMzEifQ%3D%3D; laravel_session=eyJpdiI6IlNPb3I3OHZqejVHOGg3V0FjWm9naGc9PSIsInZhbHVlIjoiSXkrcVhVRnJmXC9EVEVJOUJwR0hhYzZDVkZxcXh1V3NVUDM4RkdiNmQrZThwV0VPeklIZWxwSmNyeHp1NThBbm1rOXNLY0Q1S29xVDJNMmo4XC9RalBlQT09IiwibWFjIjoiMjNhMjE4NmUyMTUwZWE5YTJkYjhlMzU0ZWI3ODNhOTVmNzdiZTc1NWEwYjE5NzYzNDg0NzQ5N2U0Yjk3Njg1NCJ9"
"REQUEST_TIME_FLOAT" => 1448714404.5915
"REQUEST_TIME" => 1448714404
"CONTENT_LENGTH" => "13360"
"CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryF4qxwnPWgFSBZl7r"
]
}
这是我的表格:
<div class="formaIkelimo" style="width: 60%; margin: 0 auto">
{!! Form::open(['url' => '/create-art-work/', 'files' => 'true']) !!}
<div class="form-group">
{!! Form::label('categories_list', 'Kategorija') !!}
{!! Form::select('categories_list[]', $categories, null, ['id' => 'categories_list','class' => 'form-control']) !!}
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="image" id="image" class="form-control">
</div>
<div class="form-group">
{!! Form::submit('Upload', ['class' => 'form-control btn btn-success']) !!}
</div>
{!! Form::close() !!}
</div>
答案 0 :(得分:0)
据我所知,你不是发送整数而是发送其他内容。我认为数据库需要一个整数。
你可以使用:UICollectionView
查看请求包含的内容?
答案 1 :(得分:0)
错误是因为您尝试将类别附加到任何内容。这导致错误:
$art = new Facility;
$art->category()->attach($request->input('categories_list'));
艺术是Facility的一个实例。它尚未进入数据库,因此它没有id
。当您尝试附加类别时,facility_id
为空。这导致了你的错误。处理此问题的最佳方法是在创建设施后将其附加到最后:
public function artStore(FacilityRequest $request)
{
$art = new Facility;
if(Input::hasfile('image'))
{
$request->file('image')->move(public_path('images/art/'), $request->file('image')->getClientOriginalName());
$art->image = 'images/art/' . $request->file('image')->getClientOriginalName();
}
$art = Facility::create($request->all());
$art->category()->attach($request->input('categories_list'));
}
这是假设您的请求输入正确。下次,您应该dd($request->all())
而不是$request
。
修改:我还意识到您的image
属性无法使用您当前的方法正确保存。我建议这样做,这也有点清洁:
public function artStore(FacilityRequest $request)
{
if(Input::hasFile('image'))
{
$request->file('image')->move(public_path('images/art/'), $request->file('image')->getClientOriginalName());
$request->merge(['image' => 'images/art/' . $request->file('image')->getClientOriginalName()]);
}
$art = Facility::create($request->all());
$art->category()->attach($request->input('categories_list'));
}
将文件移动到指定区域后,merge
方法基本上会用新值覆盖image
属性。这样,您可以在最后一行创建Facility对象。然后,您附加类别。