1

I try to make library of pdf files. which i want to store pdf title-name and file name also upload this pdf in project storage. but server show me this error.I can't understand what can I do.

Method App\Http\Controllers\Elibrary::save does not exist. my error message this is my elibrary controller file which i chech filename and store file name in database also stored in public/images location

I find this code on this linkuload file tutorial

       <?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Elibrary;
class ElibraryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request){
        $elibrary = Elibrary::orderBy('id','DESC')->paginate(5);
        return view('e-library',compact('elibrary'))
            ->with('i', ($request->input('page', 1) - 1) * 5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
     public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'efile' => 'required|max:4000',
        ]);
        if($file= $request->file('file')){
            $name = $file->getClientOriginalName();
            if($file->move('images', $name)){
                $elibrary = new Post;
                $elibrary->efile = $name;
                $elibrary->save();
                return redirect()->route('e-library');
            };
        }
        $elibrary = new Elibrary([
            'title'    =>  $request->get('title'),
            'efile'    =>  $request->file('file'),
            ]);
        $elibrary->save();
        return redirect()->route('e-library');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

This is my route file code

Route::post('/store', 'Elibrary@store')->name('store');

this is e-library.blade.php file from

<form action="/store" method="post" enctype="multipart/form-data">
                @csrf()
                  <div class="form-group">
                     <input type="text" class="form-control"name="title" placeholder="Name">
                  </div>
                  <div class="form-group">
                     <input type="file" class="form-control"name="efile" >
                  </div>
                  <div class="form-group">
                     <input type="submit" class="btn btn-primary btn-send-message" >
                  </div>
               </form>

this is my model file of Elibrary.php

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Elibrary extends Model
{
    public $fillable = ['title','efile'];
}

this is my migration file

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateElibrariesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('elibraries', function (Blueprint $table) {
           $table->bigIncrements('id');
            $table->string('title');
            $table->string('efile');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('elibraries');
    }
}

How i can show this pdf with the help show function in show.blade.php

2 Answers2

2

You're creating new instances of Elibrary in your controller methods. Elibrary is a controller class but it looks like you're treating it as a model.

Maybe try changing all of your new Elibrary() to new Post since it looks like that might be what you're trying to accomplish.

If thats the case, you will also need to make efile fillable in your Post model.

Brian Thompson
  • 13,263
  • 4
  • 23
  • 43
  • QLSTATE[42S02]: Base table or view not found: 1146 Table 'hawks1.posts' doesn't exist (SQL: insert into `posts` (`title`, `updated_at`, `created_at`) values (god, 2019-11-04 17:02:53, 2019-11-04 17:02:53)) – Vishal Lagend Nov 04 '19 at 17:03
  • Have you ran a migration for that table? – Brian Thompson Nov 04 '19 at 17:04
  • SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'efile' cannot be null (SQL: insert into `elibraries` (`title`, `efile`, `updated_at`, `created_at`) values (god, ?, 2019-11-04 17:09:55, 2019-11-04 17:09:55)) – Vishal Lagend Nov 04 '19 at 17:10
  • Can you post your migration file? These new errors are going to be related to it, and not to the original problem. – Brian Thompson Nov 04 '19 at 17:11
  • now see my code, i update my all code of controller file and also model file – Vishal Lagend Nov 04 '19 at 17:11
  • If `efile` is not required, which since its inside a conditional I would assume thats the case, you need to tell the database that its optional as well. Change migration to this `$table->string('efile')->nullable();`. Then rerun your migrations if its is safe to do so `php artisan migrate:rollback` `php artisan migrate` – Brian Thompson Nov 04 '19 at 17:15
  • then how can i fetch pdf file without save his name? – Vishal Lagend Nov 04 '19 at 17:17
  • Thats an implementation question not really relevant to your errors. You either need to validate your input to insure its present, or not make it required. It looks like you're new controller is using both `Post` and `Elibrary`? Is that intentional? – Brian Thompson Nov 04 '19 at 17:20
  • I'm looking at your code. You have both `Post` and `Elibrary` being saved in the `store` method. – Brian Thompson Nov 04 '19 at 17:23
  • thanks bro code done...............................................................................................A.....................big...........................................thanks......................................for...................................you – Vishal Lagend Nov 04 '19 at 17:29
-1
$elibrary = Post::orderBy('id','DESC')->paginate(5);
  • 1
    Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Nov 05 '19 at 02:59