0

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?

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

i use array_merge befor say you. but not work....i use this $data = array_merge($request->except(['_token','files','outcomes','requirements']), $images); and work ...
outcomes and requirements is array that i hide this way. can i pass this arrguments? @Wesley Smith
@tohid Im not sure I follow this bit "outcomes and requirements is array that i hide this way. can i pass this arrgument", can you maybe re-phrase that?
thank you bro ...i say outcomes and requirements stored array and when i insert in except(['_token','requirements ','outcomes ']) ..it is work...and for solve this problem i insert in model protected $casts = [ 'images' => 'array', 'requirements' => 'array', 'outcomes' => 'array', ];
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.