Creating default object from empty value

Published 7 months ago by kgurleyen

Help me pleas. - I get such an error in my project.

Not!!: I get an error in updating. it just fails to update

Guess : salary_type

Editcontroller Upload class

public $data;
public $msg = [];
public $uri = [];

/**
 * EditController constructor.
 */
public function __construct()
{
    parent::__construct();

    // From Laravel 5.3.4 or above
    $this->middleware(function ($request, $next) {
        $this->commonQueries();

        return $next($request);
    });
}

/**
 * Common Queries
 */
public function commonQueries()
{
    // References
    $data = [];

    // Get Countries
    $data['countries'] = $this->countries = CountryLocalizationHelper::transAll(CountryLocalization::getCountries());
    $this->countries = $data['countries'];
    view()->share('countries', $data['countries']);

    // Get Categories
    $data['categories'] = Category::trans()->where('parent_id', 0)->with([
        'children' => function ($query) {
            $query->trans();
        },
    ])->orderBy('lft')->get();
    view()->share('categories', $data['categories']);
    
     // Günlük Aylık Yıllık Kiralama
       $data['salary_type'] = SalaryType::where('translation_lang', $this->lang->get('abbr'))->get();
        view()->share('salary_type', $data['salary_type']);

    // Get Post Types
    $data['postTypes'] = PostType::trans()->get();
    view()->share('postTypes', $data['postTypes']);

    // Count Packages
    $data['countPackages'] = Package::trans()->where('currency_code', config('country.currency'))->count();
    view()->share('countPackages', $data['countPackages']);

    // Count Payment Methods
    $data['countPaymentMethods'] = $this->countPaymentMethods;

    // Save common's data
    $this->data = $data;
}

/**
 * Show the form the create a new ad post.
 *
 * @param $postId
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function getForm($postId)
{
    return $this->getUpdateForm($postId);
}

/**
 * Update ad post.
 *
 * @param $postId
 * @param PostRequest $request
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
 */
public function postForm($postId, PostRequest $request)
{
    $post->salary_type_id = $request->input('salary_type');
    return $this->postUpdateForm($postId, $request);
    
}

}

Edit.blade

@foreach ($salary_type as $type) <option value="{{ $type->tid }}" @if(old('salary_type', $post->salary_type_id)==$type->tid)selected="selected"@endif> {{ $type->name }} @endforeach

Best Answer (As Selected By kgurleyen)
bobbybouwmann

Before helping you out I want to say that this code looks like a mess. You're currently using things that are not meant for that purpose. For example the view()->share('categories', $categories) should not be used in your controller. Instead you should return this in your controller

return view('post.edit')->with('categories, $categories);

Next to that your controller method is doing a thousand things at the same time. It's really hard to debug this and find the bug in this. Also returning multiple times in a controller makes it hard to read!

Without any error it's really hard to help you further. What exactly is not working?

bobbybouwmann

i don't see a postUpdateForm method here?

What error do you get? That might help as well!

kgurleyen

I started getting this error after adding Salary_type @bobbybouwmann

bobbybouwmann

I already see the error. There is no $post variable in that method and you try to call that

$post->salary_type_id = $request->input('salary_type');

I think you should do this call in your postUpdateForm method, and not in the postForm method!

But like I said, I don't know how your postUpdateForm method looks like...

kgurleyen

my post form edit.blade. he was working before I just added this code.

@foreach ($salary_type as $type) <option value="{{ $type->tid }}" @if(old('salary_type', $post->salary_type_id)==$type->tid)selected="selected"@endif> {{ $type->name }} @endforeach

bobbybouwmann

Do you get any error in your application? Any errors in your log file? Anything that points us in the right direction.

I'm really trying to help here, but you need to provide more information if you want help! Start by sharing what getUpdateForm and postUpdateForm are doing as a starter.

kgurleyen
bobbybouwmann

Before helping you out I want to say that this code looks like a mess. You're currently using things that are not meant for that purpose. For example the view()->share('categories', $categories) should not be used in your controller. Instead you should return this in your controller

return view('post.edit')->with('categories, $categories);

Next to that your controller method is doing a thousand things at the same time. It's really hard to debug this and find the bug in this. Also returning multiple times in a controller makes it hard to read!

Without any error it's really hard to help you further. What exactly is not working?

kgurleyen

@bobbybouwmann you, really good person . thank you for your support. After I added this code to my system. ("Creating default object from empty value") I got the error.

I think the problem is in these codes

-----CreateController-----

  $data['salary_type'] = SalaryType::where('translation_lang', $this->lang->get('abbr'))->get();  view()->share('salary_type', $data['salary_type']);




    $postInfo = [

'salary_type_id'       => $request->input('salary_type'),
 
    ];

--- create.blade.php----

<select name="salary_type" id="salary_type" class="form-control selecter">@foreach ($salary_type as $type) <option value="{{ $type->tid }}" @if(old('post_type')==$type->tid)selected="selected"@endif> {{$type->name }} </option> @endforeach </select>

---- Edit.Controller -----

     $data['salary_type'] = SalaryType::where('translation_lang', $this->lang->get('abbr'))->get();view()->share('salary_type', $data['salary_type']);


 */
public function getForm($postId)
{
    return $this->getUpdateForm($postId);
}

/**
 * Update ad post.
 *
 * @param $postId
 * @param PostRequest $request
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
 */
public function postForm($postId, PostRequest $request)
{
    return $this->postUpdateForm($postId, $request);
    
}

--edit.blade--

       <select name="salary_type" id="salary_type" class="form-control selecter">@foreach ($salary_type as $type)<option value="{{ $type->tid }}" @if(old('salary_type', $post->salary_type_id)==$type->tid)selected="selected"@endif>{{ $type->name }}  </option>  @endforeach </select>

----SalaryType (Models)----

protected $table = 'salary_type';


protected $appends = ['tid'];


public $timestamps = false;


protected $guarded = ['id'];

protected $fillable = ['name', 'active', 'translation_lang', 'translation_of', 'lft', 'rgt', 'depth',];
public $translatable = ['name'];



/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
protected static function boot()
{
    parent::boot();
    
    static::addGlobalScope(new ActiveScope());

    // before delete() method call this
    static::deleting(function ($salaryType) {
        // Delete all translated entries
        $salaryType->translated()->delete();
    });
}
kgurleyen

Please sign in or create an account to participate in this conversation.