I use 3 arrays to store in table courses. Please help me to save array in string or save array in database...my problem with array:
I can't store and save image in database
I can't save array outcomes and requirements in database
<div class="col-md-10"><input type="file" class="form-control" name="images" id="images" ></div>
//and this
<input type="text" class="form-control" name="outcomes[]" id="outcomes">
//or this
<input type="text" class="form-control" name="requirements[]" id="requirements">
My store function call:
public function store(CourseRequest $request)
{
$imageUrl = $this->upload2Images($request->file('images'));
auth()->user()->course()->create(
$request->except(['_token','files']),
['images'=>$imageUrl]);
return redirect(route('courses.index'));
}
and upload2Images()
is
public function upload2Images($file)
{
$year = Carbon::now()->year;
$month = Carbon::now()->month;
$imagePath = "upload/images/{$year}/{$month}/";
$filename = $file->getClientOriginalName();
$file = $file->move(public_path($imagePath), $filename);
$url['images'] = "upload/images/{$year}/{$month}/$filename";
return $url;
}
When I use
$data = array_merge(
$request->except(['_token','files','outcomes','requirements']),
$images
);
$id = auth()->user()->course()->create($data)->id;
It works fine but outcomes and requirements is array that I hide this way. Can I pass this arguments?
You're calling:
$url['images'] = "upload/images/{$year}/{$month}/$filename";
return $url;
Then:
$imageUrl = $this->upload2Images($request->file('images'));
So $imageUrl
is an array. but the code to save the record expects that to be a string.
`...->create( $request->except(['_token','files']),['images'=>$imageUrl]);`
Also, you seem to be passing 2 parameters to create
when it only takes one
Maybe you meant to call something like the below?
$images = $this->upload2Images($request->file('images'));
$data = array_merge($request->except(['_token','files']), $images);
auth()->user()->course()->create($data);
solved thanks i use in model of this
protected $casts = [
'images' => 'array',
'requirements' => 'array',
'outcomes' => 'array',
];
and controller use this
$images = $this->upload2Images($request->file('images'));
$requirements = $request->requirements;
$outcomes = $request->outcomes;
$data = array_merge($request->except(['_token']), ['images'=>$images],['requirements'=>$requirements],['outcomes'=>$outcomes]);
$id = auth()->user()->course()->create($data)->id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.