arslan2037's avatar

Undefined index: admin_id

I have a table for admin and another table for admin picture, i set a relation between them like this

in Admin Model public function adminpicture() { return $this->hasOne('App\Adminpicture'); }

and in Adminpicture Model

public function admin()
{
    return $this->BelongsTo('App\Admin');
}

here is migrations for both table

Schema::create('admins', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();


    });


 Schema::create('adminpictures', function (Blueprint $table) {
        $table->increments('id');
        $table->string('file_name');
        $table->integer('admin_id')->unsigned();
        $table->timestamps();

        $table->foreign('admin_id')->references('id')->on('admins')->onDelete('cascade');

    });

My controller method for upload picture is here

public function addPicture()
{
    if(Input::hasFile('file'))
    {
        $file = array('file' => Input::file('file'));
        $rules = array('file' => 'required|mimes:jpg,png,jpeg');
        $validator = Validator::make($file, $rules);

        if ($validator->fails()) {
            echo 'Not allowed!!';
        }
        else
        {
            if (Input::file('file')->isValid()) {
                $destinationPath = 'AdminPicture';
                $extension = Input::file('file')->getClientOriginalExtension();
                $fileName = Auth::guard('admin')->user()->id.'.'.$extension;

                if (File::exists($fileName))
                {
                    File::delete($fileName);
                }
                else {
                    $displayImage = Input::file('file')->move($destinationPath, $fileName);
                    $request['file_name'] = $fileName;
                    $request['admin_id'] = $_GET['admin_id'];
                    Adminpicture::create($request);

                    echo 'Upload Succesfully <br>';
                }

            }

            else {
                Session::flash('error', 'uploaded file is not valid');
                return Redirect::to('admin');
            }
        }
    }
    else
    {
        echo 'Nothing to upload';

    }
}

but i get this error, please help me to solve out this problem and after that i want to display this picture also i view

0 likes
22 replies
zachleigh's avatar

Are you sending the admin_id through a form? What does your view look like?

SaeedPrez's avatar

I would probably do something like this instead..

// Get the admin user
// If it's the logged in user, instead use auth()->user()
$admin = App\Admin::findOrFail($request->input('admin_id'));

// Create a new Adminpicture object
$adminPicture = new App\Adminpicture;
$adminPicture->file_name = $fileName;

// Save it
$admin->adminpicture()->save($adminPicture);

PS. It's kind of redundant to call the relationship adminpicture() and it would make a lot more sense to just call it picture(). This way you would get the picture by using $admin->picture instead of $admin->adminpicture

Greeting Guide

2 likes
arslan2037's avatar

i have a error like this Undefined index: admin_id after solve this then i want to display that picure

zachleigh's avatar

Yes. The problem is probably here: $_GET['admin_id'];. How are you sending the admin_id?

arslan2037's avatar

here is form for this

<form action="editProfilePic"  method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}" >
<label for="admin_id">Admin ID:</label>
<input type="text" name="admin_id" id="admin_id"><br><br>
<label for="file">Choose file:</label>
<input type="file" name="file" id="file"><br><br>

<input type="submit" name="submit" value="Submit">
arslan2037's avatar

Now i have another error like this

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`multiauth_5.2`.`adminpictures`, CONSTRAINT `adminpictures_admin_id_foreign` FOREIGN KEY (`admin_id`) REFERENCES `admins` (`id`) ON DELETE CASCADE) (SQL: insert into `adminpictures` (`file_name`, `updated_at`, `created_at`) values (1.png, 2016-07-07 11:10:45, 2016-07-07 11:10:45))
arslan2037's avatar

i think it could not insert admin id into the table

arslan2037's avatar

i think now problem with this line

Adminpicture::create($request);
zachleigh's avatar

Yes. You have a database problem. What is multiauth_5.2? The "." might be the root of the issue, but I dont know.

arslan2037's avatar

multiauth_5.2 is my project name, i have a question is , how i pass an array to Adminpicture::create() method?

arslan2037's avatar

i don't think this error is due to name of database, i think error in insert query please help me to solve it

arslan2037's avatar

and now solve other problem as well to change it like this

$data =      array(
                        'admin_id' => $_POST['admin_id'],
                        'file_name' => $fileName
                    );

                    $value = Adminpicture::Insert($data);

Please or to participate in this conversation.