Insert array of arrays in Lumen with Eloquent ORM - php

The POST request I'm sending looks like this:
{ "array1":
[
{"title":"my blogADD","description":"myblogdescriptionADD","status":1},
{"title":"my blogUPDATEDADD","description":"myblogdescriptionUPDATEDADD","status":1},
{"title":"my blog33ADD","description":"myblogdescription33ADD","status":1}
]
}
Its JSON format, headers have been set.
The controller code which gets the request looks like this:
public function create(Request $request){
$this->validate($request, [
'array1' => 'present|array',
'array1.*.title' => 'required',
'array1.*.description' => 'required'
]);
$data = $request->getContent();
$data = json_decode($data, true);
//dd($data);
Article::insert($data);
}
Now, I've looked into multiple questions and answers on SO on this problem, and the findings are somehow contradictory.
Model::insert() shall be able to insert multiple rows in ONE call.
However, as you can see, this hasn't worked for me so far.
Model::create() is only able to create one new row, but I found solutions which use loops to iterate over the arrays. I would very very much like to avoid such a solution, unless someone can FOR CERTAIN tell me that there is no other, simple solution. Because I very much believe that there must be one.
When I input the json_decoded ARRAY then I get the response that an Array to String conversion is hindering the process.
When I input the mere JSON-String, then I get the error:
"Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, string given, called in E:\LumenTut\firstTut\vendor\illuminate\database\Eloquent\Builder.php on line 1350"
Well, here are two links to SO posts which, in my opinion, basically dealt with the same problem. But somehow it seems they could solve it and I can't, so I wonder what I am missing:
How to insert a multidimensional array in a database using laravel
laravel 5.6 bulk inserting json data
For completeness, here is the full Code of ArticleController.php:
EDIT:
<?php
namespace App\Http\Controllers;
//use Validator;
use App\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
//
}
//
public function showAllArticles(){
return response()->json(Article::get(['title', 'description', 'status'])); // ::get([]) spezifiziert die zu referenzierenden Attribute
// ::all() referenziert alle Attribute einer Tabelle/Relation
}
public function showOneArticle($id){
return response()->json(Article::find($id));
}
public function create(Request $request){
$this->validate($request, [
'array1' => 'present|array',
'array1.*.title' => 'required',
'array1.*.description' => 'required'
]);
$data = $request->getContent();
//$data = json_decode($data, true);
//dd($data);
Article::insert($data);
}
public function update($id, Request $request){
$this->validate($request, [
'title' => 'required',
'description' => 'required'
]);
$article = Article::findOrFail($id);
$article->update($request->all());
return response()->json($article, 200);
}
public function delete($id, Request $request){
Article::findOrFail($id)->delete();
return response('Deleted Successfully', 200);
}
public function resetRecords(Request $request){
Article::where('id', '>', 2)->delete();
}
}

From the looks of it, it feels like you are trying to push array1 directly in your table, whereas you need to push the content of it so maybe try like this, in your controller code:
$requestData = $request->all();//this will give you an array with key array1
$data = $requestData['array1'];//this will give you data you want to insert
Article::insert($data);

Based on the error. You are not passing an array. You can change the $data with
$data = $request->all();
$request->all() returns the data from the post in array.
You can rewrite your create method with the following.
public function create(Request $request){
$request->validate([
'array1' => 'present|array',
'array1.*.title' => 'required',
'array1.*.description' => 'required'
]);
$data = $request->all();
Article::insert($data['array1']);
}

Related

validation getting failed in Laravel API

In laravel controller validation getting failed, please help.
Repository: https://github.com/dhawlesudhir/basic_app.git
ProductController.php:
protected function validateRequest()
{
return request()->validate([
'name' => 'required|min:10|max:255',
'price' => 'required|integer|min:100',
'category_id' => 'required|exists:categories,id'
]);
}
public function store()
{
$data = $this->validateRequest();
$product = Product::create($data);
return new ProductResource($product);
}
api.php:
Route::apiResource('/products', ProductController::class);
Laravel throwing validation because you haven't set json in postman.
I can see currently you have set Text.
Set type to json like in screenshot
Otherwise Laravel receives empty array from request()->all()
Also make sure to set header Accept:application/json .

Laravel test response with The given data was invalid

I'm doing unit test with laravel, so I called controller function and I get like a respnse an array
I have been response with this
return back()->with('success', 'Lots was generated')
and
return $this->lots_available;
The test give me as response this:
There was 1 error:
Tests\Feature\LotTest::test_lots
Illuminate\Validation\ValidationException: The given data was invalid.
I don't understand the reazon to this response, I'm beginning with the test
This is my function test
public function test_lots()
{
$this->withoutExceptionHandling();
$product = factory(Product::class)->create([
'size' => 20
]);
$lots = factory(Lot::class, 10)->create([
'product_id' => $product->id,
]);
$admin = factory(User::class)->create([
'role_id' => 3
]);
$client_request = 500;
$this->actingAs($admin)
->post(route('lots.distribution'), [$product, $client_request])
->assertStatus(200);
}
And this my called method
public function distribute(ProductRequest $product, $client_order)
{
$this->lots = $product->lots;
$this->client_order = $client_order;
$this->getLotAvailable();
return $this->lots_available;
}
Assuming your route is something like Route::post('/distribute/{product}/{client_order}')
route('lots.distribution') needs the parameters inside the function call
route('lots.distribution', [$product, $client_request])
Then you need to send the data that passes your rules in ProductRequest otherwise you will get a validation error. If you try a dd(session('errors')) after the post, you will probably see errors about missing fields.
->post(
route('lots.distribution', [$product, $client_request]),
['title => 'unique_title', 'sap_id' => 'unique_id']
)
Finally in your method, I'm assuming that the request ProductRequest is different than the Model Product:
public function distribute(ProductRequest $request, Product $product, $client_order)
Put the response in a variable and use dd() to print it.
You will find it on the messages method.
Worked for me.
dd($response);

Problem with the function update() using Repository pattern to write REST API

I have a problem that all the create-read-delete using Repository Pattern is good but the update function is error. I still have the data but the information is not updated.
This is my code in EventController
public function update(EventRequest $request, $id)
{
$events = $this->repository->update($request->all());
return $this->sendResponse($events->toArray(), 'Successfully updated the Event!!');
}
This is i use DI for inject from the Repository, this is EventRepository.php
public function update($id, array $array) {
$events = $this->model->findOrFail($id);
$events->update($array);
return $events;
}
when i use dd($array) and the result returns [] without anything. Can anyone help me. Did i write anything wrong in this. Or i write the wrong Request
public function rules()
{
// $id = $this->events ? ',' . $this->events->id : '';
return $rules = [
'event_title' => 'required|max:255',
'event_type_id' => 'required|integer|between:1,3',
'from_date' => 'required|date_format:Y-m-d H:i:s',
'to_date' => 'date_format:Y-m-d H:i:s|nullable',
'is_recurring' => 'boolean|required',
'remarks' => 'nullable',
];
}
This method takes two arguments:
public function update($id, array $array) {
However, that's not how you are calling it:
$this->repository->update($request->all());
I take it $request->all() gives you an array, so pass the ID first.
$this->repository->update($id, $request->all());

Laravel 5.5 Method save does not exist when updating entries with modified primary key

I am working with laravel 5.5 to update entries. The problem is after changing the primary key 'id', which is elequoent default pk to 'project_id'. adding an item works fine but updating an item is not working properly. Here is the error I am getting.
Method save does not exist.
Here is my Model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $primaryKey = 'project_id';
public function user()
{
return $this->belongsTo(User::class);
}
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Here is my controller function.
public function editProject($id){
$project = Project::where('project_id', $id)->firstOrFail();
$data = ["project_info" => $project];
return view('projects.edit')->with($data);
}
public function updateProject(Request $request){
$data = $request->all();
$validator = Validator::make($data, [
'project_title' => 'required',
'project_description' => 'required',
'project_start_date' => 'required',
'project_end_date' => 'required',
'project_status' => 'required',
]);
$response = [];
if ($validator->fails()){
$response["errors"] = [$validator->messages()->first()];
$response["success"] = false;
return json_encode($response);
}
else{
$project = Project::where("project_id", $request->input('project_id'))->get();
$project->project_title = $request->project_title;
$project->user_id = Session::get('user_id');
$project->project_description = $request->project_description;
$project->project_start_date = $request->project_start_date;
$project->project_end_date = $request->project_end_date;
$project->project_status = $request->project_status;
$project->save();
return redirect('/listProjects');
}
}
Using get() returns a collection. Despite the fact you are passing in a 'unique' ID, the project_id, it will still return a collection - the collection will simply have one element in it.
Subsequently, your code will not work as you have experienced, or at least not without a few changes to make $project reference the first element in the collection.
It's a quick fix though, just change this:
$project = Project::where("project_id", $request->input('project_id'))->get();
to this:
$project = Project::where("project_id", $request->input('project_id'))->first();
By using first(), eloquent will return the first element that matches the query and actually return the element (as opposed to a collection with one element) and so you can directly edit and save it.
Here is the solution I found.
$project_id = $request->input('project_id');
$project = Project::find($project_id);
$project->save();
You can find it by id using
Project::find($id);
Or get the first element like James said:
$project = Project::where("project_id", $request->input('project_id'))->first();

How to solve Method App\JenisSurat::__toString() must return a string value on Laravel 5?

I want to input data to database. I am using Laravel 5. When I clicked the submit button. I got an error like the image below. Here is controller:`
public function tambahjenissurat(Request $request)
{ $this->validate($request, [
'jenis_surat' => 'required'
]);
$jenis_surat = $request['jenis_surat'];
$jenis_surat = new JenisSurat();
$jenis_surat->jenis_surat = $jenis_surat;
$jenis_surat->save();
return redirect()->route('jenissurat');
}`
Your code is expecting a string but you are passing an object. that could be the problem. try to give different names for object and variable. Something like this should fix the problem. See the lines below EDIT sections
public function tambahjenissurat(Request $request)
{ $this->validate($request, [
'jenis_surat' => 'required'
]);
**EDIT**
$jenis_surat_var = $request['jenis_surat'];
$jenis_surat = new JenisSurat();
**EDIT**
$jenis_surat->jenis_surat = $jenis_surat_var;
$jenis_surat->save();
return redirect()->route('jenissurat');
}

Categories