Ad

Laravel Sql Table Error When I Try To Upload

- 1 answer

This is the error i get

SQLSTATE[HY000]: General error: 1 table posts has no column named caption (SQL: insert into "posts" ("caption", "image", "user_id", "updated_at", "created_at") values (Caption,

Why is that

post table code

Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('caption');
        $table->string('image');
        $table->timestamps();

        $table->index('user_id');

blade.php code

<input id="caption" 
                                type="text" 
                                class="form-control @error('caption') is-invalid @enderror" 
                                name="caption"
                                value="{{ old('caption') }}" 
                                autocomplete="caption" autofocus>

and i have my PostsController.php as

public function store()
    {
        $data = request()->validate([
            'caption' => 'required',
            'image' => ['required', 'image'],
        ]);

        auth()->user()->posts()->create($data);    


        dd(request()->all());
    }

Why is the error coming up?

Ad

Answer

As you are trying to store data into the post table you can do simple like:

$data = request()->validate([
    'caption' => 'required',
    'image' => ['required', 'image']        
]);

$data['user_id'] = Auth::id();

$post = Post::create($data);

Also, you need to import Post model to insert data into post table as below:

namespace App\Http\Controllers;

use App\Post;
Ad
source: stackoverflow.com
Ad