Home > Software design >  when i make save in (admin/settings) i got (local.ERROR: Undefined index: utubelink) error
when i make save in (admin/settings) i got (local.ERROR: Undefined index: utubelink) error

Time:01-09

when I make save in (admin/settings) I got (local.ERROR: Undefined index: utubelink) error it is just in (admin/settings) other admin pages work fine. I can not make any changes like changing the logo or name of the site.

laravel.log

[2022-01-08 15:46:38] local.ERROR: Undefined index: utubelink {"exception":"[object] (ErrorException(code: 0): Undefined index: utubelink at /home/u0462672/new.maxtella.net/app/Http/Controllers/Admin/Settings.php:44)

Settings.php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\Setting;
use Storage;
class Settings extends Controller {
    public function setting() {
        return view('admin.settings.settings', ['title' => trans('admin.settings')]);
}

public function setting_save() {
    $date=$this->validate(request(), [
        'logo'=>v_image(),
        ],[],
        [
            'logo'=>trans('admin.logo'),
            'icon'=>trans('admin.icon')
        ]);
    $data = request()->except(['_token', '_method']);

    if(request()->hasFile('logo')) {
        $file = request()->file('logo');
        $name = str_random(21) . time() . '.' . $file->getClientOriginalExtension();
        $ext = $file->getClientOriginalExtension();
        $size = $file->getSize();
        $mim = $file->getMimeType();
        $realpath = $file->getRealPath();
        $file->move(public_path('upload/settings/'), $name);
        $data['logo'] =  $name;

    }
    if(request()->hasFile('imgindex')) {
        $file = request()->file('imgindex');
        $name = str_random(21) . time() . '.' . $file->getClientOriginalExtension();
        $ext = $file->getClientOriginalExtension();
        $size = $file->getSize();
        $mim = $file->getMimeType();
        $realpath = $file->getRealPath();
        $file->move(public_path('upload/settings/'), $name);
        $data['imgindex'] =  $name;
    }

    $data['utubelink'] = $data['utubelink'] != '' ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';

    Setting::orderBy('id', 'desc')->update($data);
    session()->flash('success', trans('admin.updated_record'));
    return redirect(aurl('settings'));
}
}

how can fix it plz ?

CodePudding user response:

Ok man Here is the issue. The error log clear points that line number 44 has an issue. You are trying to access utubelink from $data array which does not exist.

$data['utubelink'] = $data['utubelink'] != '' ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';

Replace with

$data['utubelink'] = ($data['utubelink'] ?? null) ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';

I hope you are using PHP 7.0 or above. Read more about the Null Coalescing Operator in PHP or just use isset().

CodePudding user response:

You can prevent these type of errors using optional Laravel helper method.

$data['utubelink'] = ! blank(optional($data)['utubelink']) ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';

Read more: https://laravel.com/docs/8.x/helpers#method-optional

  •  Tags:  
  • Related