Array to string conversion error when multiple file upload in Laravel 5.8.38 - laravel

I have an Array to string conversion error, when trying to realize multiple upload files function in Laravel 5.8.38
Cant find any decision about it
In blade form I have simple thing:
<form class="form-horizontal" action="{{route('admin.estates.store')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<label for="estate_image" class="mt-4">Images</label>
<input type="file" name="estate_image[]" multiple>
<input class="btn btn-primary" type="submit" value="Сохранить">
<input type="hidden" name="created_by" value="{{Auth::id()}}">
</form>
In store function I have:
The function creates an estate(one property). If user adds some images for it, we adds this images in local path and adds them in database
if I comment $estate = Estate::create($request->all()); it works fine
but in this case estate doesnt adds in database
public function store(Request $request)
{
$estate = Estate::create($request->all());
if($request->hasFile('estate_image')) {
foreach ($request->file('estate_image') as $image) {
// do some image resize and store it on local path
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images\\' . $filename);
Image::make($image)->resize(800, 400)->save($location);
// add image info in database
$estateimage = new EstateImages();
$estateimage->image_path = $location;
$estateimage->image_alt = 'testalt';
$estateimage->save();
}
}
}
Am array I have from input
array:5 [▼
"name" => array:2 [▼
0 => "image1.jpg"
1 => "image2.jpg"
]
"type" => array:2 [▼
0 => "image/jpeg"
1 => "image/jpeg"
]
"tmp_name" => array:2 [▼
0 => "C:\OSPanel\userdata\php_upload\phpCB51.tmp"
1 => "C:\OSPanel\userdata\php_upload\phpCB52.tmp"
]
"error" => array:2 [▼
0 => 0
1 => 0
]
"size" => array:2 [▼
0 => 164808
1 => 58217
]
]
As understand, foreach doesnt start, but dont understand why (tried to delete all code in foreach, and leave there just simple echo 'Hello!'; , have the same error.
Saw the same problems in StackOverflow, but any of it helped me...

The problem was in first line
$estate = Estate::create($request->all());
So, from blade I receive a name="estate_image[] data which is an array, and Laravel tried to adds an array in database cell. In database cell cold be added just string value and through that, I had that error.
Solve it, by deleting this column from database and deleting this from $fillable variable in main model.
Quite stupid and easy thing from me, but hope, this answer helps someone. :-)

Related

Livewire checkboxes selected by default

What I would like :
By default check all checkboxes
Get the value sheet id for later
First issue :
Is it possible to sort / order them by id ?
Because for the moment it orders them by the order of check.
screenshot 2
If someone can help me I would be glad thanks :D
Here is my view :
#foreach($sheets as $sheet)
<div class="inputGroup">
<input id="{{$loop->index}}" wire:model="selectedBoxes" type="checkbox" value="{{$loop->index}}" checked/>
<label for="{{$loop->index}}">Feuille {{$loop->index+1}}: {{$sheet}}</label>
</div>
#endforeach
Here are the results when I dd($sheets) :
^ array:4 [▼
0 => "All ValueRAM FURY"
1 => "Non-ECC ValueRAM"
2 => "All FURY"
3 => "KSM (Server Premier)"
]
Here is my component :
public $sheets = [];
public $selectedBoxes = [];
...
public function readExcel()
{
...
$data = [];
// Return an import object for every sheet
foreach($import->getSheetNames() as $index => $sheetName)
{
$data = $import->getSheetNames();
$this->sheets = $data;
}
}
Website view :
screenshot 1
welcome to StackOverflow! You could remove the checked tag from your select and let wire:model do it's thing:
public $sheets = [
0 => "All ValueRAM FURY",
1 => "Non-ECC ValueRAM",
2 => "All FURY",
3 => "KSM (Server Premier)"
];
// wire:model will ensure that all are checked by default.
public $selectedBoxes = [true, true, true, true];
and in your view:
(Take a look at the wire:model property and the checked attribute is gone)
#foreach($sheets as $sheet)
<div class="inputGroup">
<input id="sheet-{{$loop->index}}"
wire:model="selectedBoxes.{{ $loop->index }}"
type="checkbox"
value="{{$loop->index}}" />
<label for="sheet-{{$loop->index}}">Feuille {{$loop->index+1}}: {{$sheet}}</label>
</div>
#endforeach
Thanks to Laisender I finded what I wanted.
What changed in my component :
public $counter = 0;
...
foreach($import->getSheetNames() as $index => $sheetName)
{
$data = $import->getSheetNames();
$this->sheets = $data;
array_push($this->selectedBoxes, "$this->counter");
$this->counter += 1;
}
My view :
#foreach($sheets as $sheet)
<div class="inputGroup">
<input id="sheet-{{$loop->index}}" wire:model="selectedBoxes.{{$loop->index}}" type="checkbox" value="{{$loop->index}}"/>
<label for="sheet-{{$loop->index}}">Feuille {{$loop->index+1}}: {{$sheet}}</label>
</div>
#endforeach

Laravel Validating Values in Array in List of Values

This must be a simple question, but unable to find what I am doing wrong. I have a form (shown below) with a multi-select input. It gets sent to the validator as an array. I want to make sure the values selected in the client-side are in the pre-defined list of values.
<form>
<select multiple name="names[]">
<option value="Jerry">Jerry</option>
<option value="Susan">Susan</option>
<option value="Tim">Tim</option>
</select>
</form>
public function postNames(Request $request)
{
$this->validate($request, [
'names.*' => 'in:Jerry,Susan,Tim',
]);
}
When I open the dev tools in the browser and change the value of them and then submit the form, the validator lets it pass even though the name in the request is not in the list as defined in the validator.
Thank you.
UPDATE:
When I dump the request it looks like this.
+request: Symfony\Component\HttpFoundation\ParameterBag {#52 ▼
#parameters: array:1 [▼
"names" => array:1 [▼
0 => "Robert"
]
]
}

Laravel date before_equal is allowing all dates through

$DOB = $request->input('DOB');
$allowed = Carbon::now()->subYears(26)->format('Y-m-d');
$validatedData = $request->validate(
['DOB' => 'required|date|before_or_equal:'.$allowed],
);
<input type="date" name="DOB" class="form-control" required>
I am having a hard time validating dates in Laravel.
If I enter 01/01/2018 in the form, the validation does not catch it. Below it what is returned if I dd() the variables.
dd([$DOB, $allowed]);
array:2 [▼
0 => "2018-01-01"
1 => "1992-04-26"
]
No idea where I am going wrong.

Laravel do check selected items from array on html select as multiple feature

After get all categories and user selected categories for selected post with this result:
$content_categories = ContentCategories::all()->pluck('title', 'id');
Collection {#209 ▼
#items: array:3 [▼
1 => "laravel"
2 => "nodejs"
3 => "php"
]
}
$selected_categories = $manage_content->categories()->get()->pluck('title', 'id');
Collection {#223 ▼
#items: array:2 [▼
1 => "laravel"
2 => "php"
]
}
I'm trying to implement this result on html select and do check selected items when we have in $selected_categories, this code only show $content_categories and can not be check items with $selected_categories by default
{{
Form::select('categories[]',
$content_categories,
$selected_categories,
array('multiple'=>'multiple'))
}}
result:
<select class="multiselect-success" multiple="multiple" name="categories[]">
<option value="1">laravel</option>
<option value="2">nodejs</option>
<option value="3">php</option>
</select>
Get IDs of selected categories without titles:
$selected_categories = $manage_content->categories()->pluck('id');
Or use keys() to get keys only:
Form::select('categories[]', $content_categories, $selected_categories->keys(), array('multiple'=>'multiple'))

How to pass object in views in laravel

I'm having a set of html codes which are being called in a foreach statement in views, I'm trying to push array of object to get the views through controller.
Following is my controller code:
public function get_template($id)
{
$template = Template::findOrFail($id);
$getplugin = json_decode($template->templatedata);
$plugins = Plugin::all();
$userid = $template->user->id;
return view('nitseditor.test', ['plugins' => $plugins, 'template'=> $template, 'getplugin' => $getplugin, 'userid' => $userid]);
}
In my views I'm calling like this:
#foreach($getplugin as $renderer)
#include('themes.' . $template->theme->name . '.Plugins.' . $plugins->find($renderer)->type . '.' . $plugins->find($renderer)->id, ['content' => $plugins->find($renderer)->userplugins()->whereUserId($userid)->first()->pivot->contents])
#endforeach
Views or HTML code which are being generated:
<div id="slideshow">
<div class="revolution-slider">
<ul>
<!-- SLIDE -->
#foreach($content->slider as $sliders)
<li data-transition="{{ $sliders->transition }}" data-slotamount="{{ $sliders->slotamount }}" data-masterspeed="{{ $sliders->masterspeed }}">
<!-- MAIN IMAGE -->
<img src="{{ URL::asset($sliders->url) }}" alt="">
</li>
#endforeach
</ul>
</div>
</div>
Now I'm getting an error of
Trying to get property of non-object (View: C:\wamp\www\NitsEditor\resources\views\themes\Miracle\Plugins\Slider\2.blade.php) (View: C:\wamp\www\NitsEditor\resources\views\themes\Miracle\Plugins\Slider\2.blade.php)
While executing diedump the below code in foreach loop:
foreach ($getplugin as $item){
$plugincontents[] = Plugin::findOrFail($item)->userplugins()->whereUserId($userid)->first()->pivot->contents;
}
dd($plugincontents);
I'm able to get the JSON output which holds the information of that particular view. Like this below:
array:2 [▼
0 => "{"slider": [{"url": "img/home/bg.jpg", "slotamount": "7", "transition": "zoomin", "masterspeed": "1500"}, {"url": "img/home/bg2.jpg", "slotamount": "7", "transition": "zoomout", "masterspeed": "1500"}, {"url": "img/home/LOMEg.png", "slotamount": "7", "transition": "slidedown", "masterspeed": "1500"}]}"
1 => "{"logo": {"logolink": "index.html", "logoimage": "img/home/nitseditorlogo.png"}, "pages": [{"pagelink": "index.html", "pagename": "Mysite"}, {"pagelink": "templates.html", "pagename": "Templates"}, {"pagelink": "aboutus.html", "pagename": "About Us"}, {"pagelink": "contactus.html", "pagename": "Contact Us"}]}"
]
Please help me out. Thanks.
instead of $getplugin = json_decode($template->templatedata); use $getplugin = json_decode($template->templatedata,true);
Well, while doing cross checks I came to know that I need to json_decode in my data in views. I tried and got the result,
In my Views I did json_decode to the array like this:
#foreach($getplugin as $renderer)
#include('themes.' . $template->theme->name . '.Plugins.' . $plugins->find($renderer)->type . '.' . $plugins->find($renderer)->id, ['contents' => json_decode($plugins->find($renderer)->userplugins()->whereUserId($userid)->first()->pivot->contents)])
#endforeach
And finally getting the results as desired.

Resources