1

When I am going to save data to database using save($data) . Then there show a error ErrorException Array to string conversion.

Here is controller :

public function create(Request $request){
        $company = new Company();
        $data = [
        'name' => $request['name'] ,
        'address' => $request['address'] ,
        'city' => $request['city'] ,
        'state' => $request['state'] ,
        'zip' => $request['zip'] ,
        'country' => $request['country'] ,
        'email' => $request['email'] ,
        'logo' => $request['logo']
        ];

        $file = $request->file('logo');
        if($request->hasFile('logo')){
            $destinationPath = 'upload';
            $file->move($destinationPath,$file->getClientOriginalName());
        }

        //dd($data);

        $company->save($data);

        return true ;

    }
2
  • Check this link: stackoverflow.com/questions/34411465/… Commented Dec 21, 2019 at 9:12
  • If logo is a file. You should save the path where it's stored. Commented Dec 21, 2019 at 9:23

3 Answers 3

1

try this ..

public function create(Request $request){

    $data = [
    'name' => $request['name'] ,
    'address' => $request['address'] ,
    'city' => $request['city'] ,
    'state' => $request['state'] ,
    'zip' => $request['zip'] ,
    'country' => $request['country'] ,
    'email' => $request['email'] ,
    'logo' => $request['logo']
    ];

    $file = $request->file('logo');
    if($request->hasFile('logo')){
        $destinationPath = 'upload';
        $file->move($destinationPath,$file->getClientOriginalName());
    }

    //dd($data);

    DB::table('companies')->insert($data); //dont forget to import DB namespace.  

    return true ;

}

the table name is companies so I used that. Model( company )

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks . It's work fine but it also provide following error : UnexpectedValueException The Response content must be a string or object implementing __toString(), "boolean" given.
1

Add the attribute protected $fillable = [ 'column_a', 'column_b', .. ]; to your model. Then you can use the mass assignment like this.

Too much extra code. You should use modern variants.

public function create(Request $request){
    $file = $request->file('logo');
    if($request->hasFile('logo')){
        $file->move('upload', $file->getClientOriginalName());
    }

    Company::create([
        'name' => $request->name,
        'address' => $request->address,
        'city' => $request->city,
        'state' => $request->state,
        'zip' => $request->zip,
        'country' => $request->country,
        'email' => $request->email,
        'logo' => $request->logo
    ]);

    //You can also use this if "$fillable" array in model is filled:
    //Company::create($request->all());

    return true;
}

Comments

0

Since you instantiating the model. you could do like this instead of passing an array.

$company = new Company();
$company->name = $request['name'];
$company->address = $request['address'];
$company->city = $request['city'];
$company->state = $request['state'];
$company->zip = $request['zip'];
$company->country = $request['country'];
$company->email = $request['email'];
$company->save();

if $request['name'] like variables doesn't work with associative array then i would personally change it to more like object e.g $request->name and so on with every other variables.

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.