Laravel多次上传仅将20插入到数据库中(Laravel Multiple Uploading Only Inserts 20 into DB)
我已经创建了一个允许多个图片上传的API,但是当我尝试上传超过20个图片时,它只会插入20条记录,而且一次不会。 它将在服务器上保存全部金额。 没有错误引发,并且日志中没有指示问题。
我保存的表格(events_images)如下所示:
id, event_id, title, path, size, extension, description, created_at, updated_at
这是我的Laravel脚本:
public function store(Request $request) { $id = $request->id; $images = $request->file('image'); $count = 0; // confirm that files exists in the request if(empty($images)): return response()->json(['error' => 'no images exist'], 500); endif; // validate image(s) before we store to db to make sure there are no failures // TODO : add to validation $validator = Validator::make($request->all(), [ 'image.*' => 'image|mimes:jpeg,jpg,png' ]); if($validator->fails()): return response()->json(['error' => 'upload failed, please make sure you are uploading a jpeg, jpg, or png'], 500); endif; // store images in DB and upload foreach($images as $image): $title = Uuid::uuid1(); $extension = $image->getClientOriginalExtension(); $path = $title . '.' . $extension; $size = $image->getClientSize(); $description = NULL; // store in db EventsImage::Create([ 'title' => $title, 'path' => $path, 'extension' => $extension, 'size' => $size, 'description' => $description, 'event_id' => $id ]); // save to filesytem $raw = Storage::disk('media')->put('events' . '/' . $id . '/' . $path, file_get_contents($image)); // create and store thumb $thumb = Image::make($image)->resize(150, null, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk('media')->put('events' . '/' . $id . '/t_' . $path, (string) $thumb->encode()); // create and store medium $medium = Image::make($image)->resize(300, null, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk('media')->put('events' . '/' . $id . '/m_' . $path, (string) $medium->encode()); // create and store full size // TODO : if smaller than 1920 then don't do anything $full = Image::make($image)->resize(1920, null, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk('media')->put('events' . '/' . $id . '/' . $path, (string) $full->encode()); $count++; endforeach; $event = Event::with('EventImages')->find($id); return response()->json(['success' => true, 'message' => $count . ' images have been uploaded successfully', 'data' => $event], 200); }
我查看了发送到API的数据,所有内容都保存在光盘中。 我还注释了文件保存逻辑,以查看它是否是时间事物,它仍然只能插入20条记录。
是否有某种类型的限制设置? 是否因为我一次使用循环来保存一条记录?
编辑
我使用Postman和Angular 4并获得相同的结果。 我上传了超过20张图片,Laravel只看到20张图片。
I have created an API that will allow multiple image uploads, but when I try to upload more than 20 it will only insert twenty records and no more at a time. It will save the full amount on the server. There are no errors thrown and nothing in the logs indicating an issue.
The table I'm saving to (events_images) looks like:
id, event_id, title, path, size, extension, description, created_at, updated_at
Here is my Laravel script:
public function store(Request $request) { $id = $request->id; $images = $request->file('image'); $count = 0; // confirm that files exists in the request if(empty($images)): return response()->json(['error' => 'no images exist'], 500); endif; // validate image(s) before we store to db to make sure there are no failures // TODO : add to validation $validator = Validator::make($request->all(), [ 'image.*' => 'image|mimes:jpeg,jpg,png' ]); if($validator->fails()): return response()->json(['error' => 'upload failed, please make sure you are uploading a jpeg, jpg, or png'], 500); endif; // store images in DB and upload foreach($images as $image): $title = Uuid::uuid1(); $extension = $image->getClientOriginalExtension(); $path = $title . '.' . $extension; $size = $image->getClientSize(); $description = NULL; // store in db EventsImage::Create([ 'title' => $title, 'path' => $path, 'extension' => $extension, 'size' => $size, 'description' => $description, 'event_id' => $id ]); // save to filesytem $raw = Storage::disk('media')->put('events' . '/' . $id . '/' . $path, file_get_contents($image)); // create and store thumb $thumb = Image::make($image)->resize(150, null, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk('media')->put('events' . '/' . $id . '/t_' . $path, (string) $thumb->encode()); // create and store medium $medium = Image::make($image)->resize(300, null, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk('media')->put('events' . '/' . $id . '/m_' . $path, (string) $medium->encode()); // create and store full size // TODO : if smaller than 1920 then don't do anything $full = Image::make($image)->resize(1920, null, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk('media')->put('events' . '/' . $id . '/' . $path, (string) $full->encode()); $count++; endforeach; $event = Event::with('EventImages')->find($id); return response()->json(['success' => true, 'message' => $count . ' images have been uploaded successfully', 'data' => $event], 200); }
I have looked at the data being sent to the API and everything is there, and all the files save to the disc. I have also commented out the file saving logic to see if it's a time thing and it still would only insert 20 records.
Is there some type of limit setting? Is it because I'm using a loop to save one record at a time?
Edit
I am using Postman and Angular 4 and getting the same result. I upload more than 20 images, and Laravel only sees 20.
原文:https://stackoverflow.com/questions/49884572
最满意答案
使用SqlCommand.ExecuteScalar方法,如下所示:
command.CommandText = @"select max(PolicyID) from Policies"; int maxPolicyId = (int)command.ExecuteScalar();
另外,如果您要这样做来插入一个具有唯一ID的新策略行,则不必这样做,因为在select和insert之间插入不同的策略行是完全可能的。
而是使用
IDENTITY
列或UNIQUEIDENTIFIER
列。编辑 :要在你的代码中使用它,请执行以下操作:
int maxId; using (SqlConnection dataConnection = new SqlConnection(@"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True")) using (SqlCommand dataCommand = new SqlCommand("select max(PolicyID) from Policies", dataConnection)) { dataConnection.Open(); maxId = Convert.ToInt32(dataCommand.ExecuteScalar()); }
Use the SqlCommand.ExecuteScalar method, like this:
command.CommandText = @"select max(PolicyID) from Policies"; int maxPolicyId = (int)command.ExecuteScalar();
Also, if you're doing this to insert a new Policy row with a unique ID, you must not do it like this, because it's entirely possible that a different Policies row will be inserted between the select and the insert.
Instead, use an
IDENTITY
column or aUNIQUEIDENTIFIER
column.EDIT: To use this in your code, do this:
int maxId; using (SqlConnection dataConnection = new SqlConnection(@"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True")) using (SqlCommand dataCommand = new SqlCommand("select max(PolicyID) from Policies", dataConnection)) { dataConnection.Open(); maxId = Convert.ToInt32(dataCommand.ExecuteScalar()); }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
我想你想表达的是: select (case when instr('&email', '@') > 0 then 1 else 0 end) into a from dual; if (a = 1) then dbms_output.put_line('Valid Email'); else dbms_output.put_line('Please Enter a valid Email'); end if; 你不需要为此目的select ,但我将它留在。我或者,只是做: ...
-
您需要使用表变量/临时表来存储多个值: SELECT E.NO_EVENT INTO #temp FROM TEST_APP.dbo.EVENT E WHERE E.NO_EVENT like '1819________' 如果您使用的是SQL Server 2017,则可以使用STRING_AGG/STRING_SPLIT : Declare @IDC varchar(500); SELECT @IDC = (SELECT STRING_AGG(E.NO_EVENT, ',') ...
-
使用SqlCommand.ExecuteScalar方法,如下所示: command.CommandText = @"select max(PolicyID) from Policies"; int maxPolicyId = (int)command.ExecuteScalar(); 另外,如果您要这样做来插入一个具有唯一ID的新策略行,则不必这样做,因为在select和insert之间插入不同的策略行是完全可能的。 而是使用IDENTITY列或UNIQUEIDENTIFIER列。 编辑 :要在你的代码 ...
-
选择变量中的语句(Select statement in a variable)[2023-05-20]
您可以使用ref cursors来获得所需的结果。 这是一个例子。 declare type t_cursor is ref cursor; c_cursor t_cursor; l_sql varchar2(123); l_var number; begin l_sql := 'select count(*) from T1'; -- your query goes here open c_cursor for l_sql; ... -
如果您的主要目标是获得日期的平均值,那么您不需要将日期存储到变量中,您可以直接执行以下操作。 SELECT CAST(AVG(CAST(OrderDate AS FLOAT)) AS DATETIME) FROM Orders WHERE CustID = '1234' 您还可以在变量中存储所有日期的平均值,您可以像下面这样执行。 DECLARE @AvgDate DATETIME SELECT @AvgDate = CAST(AVG(CAST(OrderDate AS FLOAT)) AS DAT ...
-
SQL one select语句来自一个表存储数据的局部变量(SQL one select statement from one table store data in local variable)[2023-10-03]
SELECT @TITLE = MIN(NULLIF(TITLE,'')), @DESCRIPTION = MIN(NULLIF(DESCRIPTION,'')) FROM dbo.Experiences WHERE UserId = @UserId GROUP BY UserId SELECT @TITLE = MIN(NULLIF(TITLE,'')), @DESCRIPTION = MIN(NULLIF(DESCRIPTION,'')) FROM dbo.Experiences WHERE Us ... -
你正在寻找类似的东西 Dim strSql As String Dim groupId As String strSql = "Select ID from RevenueGroup where description = '" & ListO.Value & "'" Dim rec As Recordset set rec= CurrentProject.Connection.Execute strSql groupId = rec(0) 您需要将查询结果设置为记录集,然后从结果中提取第一个值。 ...
-
小数的默认精度为0(参见此处 )。 尝试这个: DECLARE @result float; SELECT @result = cast(val2 as float) / val1 from table; 当两个操作数都是整数时,SQL Server会进行整数除法。 所以,我把它扔到漂浮。 如果你真的想要小数,那么使用更好的声明,例如: DECLARE @result decimal(18, 6); 最后,当您的表有多行时,您的选择是不明确的。 我会建议: SELECT top 1 @result = ...
-
使用Prepared语句将MySQL SELECT结果存储在php变量中(store MySQL SELECT result in php variable using Prepared statements)[2022-05-06]
正如我在评论中提到的,PHP现在有一些内置方法来处理密码的加密和解密,您可能会发现这些方法有助于解决您的问题: password_hash和password_verify As I mentioned in the comment, PHP now has a couple built in methods to handle encryption and decryption of passwords that you might find helps solve your problem: passwo ...