Back

Laravel 5.2: use mass-assignment with dimsav laravel-translatable


I am using Laravel 5.2 Package dimsav laravel-translatable 6.*

This is my worked controller

$about = new About();
        $about->link = $request->link;
        $about->save();

        $localization = $request['localization'];
        //dd($localization);
        // check if locales arabic and english only
        $locales = array_keys($localization);
        foreach ($locales as $locale) {
            if (!in_array($locale, array('en', 'ar'))) {
                Session::flash('message', 'Lang Error');
                return Redirect::back();
            }
        }

        foreach ($localization as $locale => $parameters) {
            $about->translateOrNew($locale)->title = $parameters['title'];
            $about->translateOrNew($locale)->details = $parameters['details'];
        }

        $about->save();

but when I try to use mass-assignment

$input = $request->all();

        $localization = $input['localization'];
        // check if locales arabic and english only
        $locales = array_keys( $localization );
        foreach ( $locales as $locale ) {
            if ( ! in_array( $locale, array( 'en', 'ar' ) ) ) {
                Session::flash( 'message', 'Lang Error' );

                return Redirect::back();
            }
        }


        foreach ( $localization as $locale => $parameters ) {
            $input->translateOrNew( $locale )->title   = $parameters['title'];
            $input->translateOrNew($locale)->details = $parameters['details'];
        }
dd($input);


About::create( $input );

Got an error

> Call to a member function translateOrNew() on a non-object

any help please what I am doing wrong here.

UnderDogg replied 8 months ago

Ok, so I'm guessing you're getting the error in 2nd foreach loop, since I don't see the error line in your post. dd($input) in there, because it's not an object, it's either a string or an array.

You're calling a function from an object that isn't there, at least It's not called input


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us