vendredi 17 avril 2020

want to upload a pdf file into mysql using laravel

hello i am new in laravel. for my project purpose, i have a feature to upload a pdf file in database. but data not stored in databases.

here is my attempted codes=

controller-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use DB;
class PostsController extends Controller
{
    public function index()
    {
        $id = Auth::id();
        $user = DB::table('users')->find($id);
        return view('posts', ['user' => $user]);
    }
    public function __construct()
    {
        $this->middleware('auth');
    }


    public function store()
    {
        //dd(request()->all());
        $data = request()->validate(['pcaption' => 'required']);
         Auth()->user()->posts()->create($data);
         dd(request()->all());

}
}

model-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class post extends Model
{
    protected $guarded = [];

    public function user()
    {
    return $this->belongsTo(User::class);
    }

}

error from localhost-

Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'post' doesn't have a default value (SQL: insert into `posts` (`pcaption`, `user_id`, `updated_at`, `created_at`) values (bh, 3, 2020-04-17 17:42:53, 2020-04-17 17:42:53))

that means the post data didn't save in database. where is my fault



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire