如何从数据库中根据 laravel-8 中的名称搜索书籍?

DevOps 培训

我开发了一个文件控制器,它将书籍存储在我的数据库中,其中包含名称、作者、描述、文件、数量、价格。我正在尝试根据名称对书籍进行搜索操作,在这里我很困惑,我尝试了不同的不同文章,但没有得到. 请帮助我如何编写一个函数,该函数根据搜索名称(接受名称请求)返回书籍数据并显示书籍。FileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Books;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use App\Http\Resources\Books as BookResource;

class FileController extends Controller
{
    
    public function upload(Request $request ){
        $book = new Books();
        $book->price=$request->input('price');
        $book->name=$request->input('name');
        $book->quantity=$request->input('quantity');
        $book->author=$request->input('author');
        $book->description=$request->input('description');
        $book->file=$request->input('file');
        $book->user_id = auth()->id();
        $book->save();
        return new BookResource($book);
    }


    public function display_Book($id)
    {
        $book=Books::findOrFail($id);
        if($book->user_id==auth()->id())
            return new BookResource($book);
        else{
            return response()->json(['error' => 'UnAuthorized/invalid id'], 401);    
            }
    }
     public function searchbooks(Request $request)
    {
        // $books=Books::all();
        // if(User::find($books->user_id=auth()->id())->books){
        $query = Books::query();
        if ($request->has('q')) {
            $searchParam = $request->get('q');

            $query->where('name', 'LIKE', "%{$searchParam}%");
           
        }
    return BookResource::collection($query);
    }

    public function displayBooks()
    {
        $books=Books::all();
        return User::find($books->user_id=auth()->id())->books; 
    }
    
    public function updateBook(Request $request, $id){
        $book=Books::findOrFail($id);
        if($book->user_id==auth()->id()){
            $book=Books::where('id',$id)
                ->update(array('name'=>$request->input('name'),
                               'price' => $request->input('price'),
                               'author' => $request->input('author'),
                               'description'=>$request->input('description'),
                               'quantity'=>$request->input('quantity'),
                               'file'=>$request->input('file')
            ));
            return['updated successfully'];
        }
    }
}

Books migration table

<?php

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

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('author');
            $table->integer('price');
            $table->integer('quantity');
            $table->string('file')->nullable();
            $table->longText('description');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

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

APi.php[api routes]


Route::group(["middleware"=>['auth.jwt']],function(){
    Route::post('addBooks',[FileController::class,'upload']);
    Route::get('getBooks',[FileController::class,'displayBooks']);
    Route::get('showBook/{id}',[FileController::class,'display_Book']);
    Route::put('updateBook/{id}',[FileController::class,'updateBook']);
    Route::delete('deleteBook/{id}',[FileController::class,'DeleteBook']);
    Route::get('searchBooks/{name}',[FileController::class,'searchbooks']);
});

像这样我得到

斯拉瓦尼
Public function searchbooks($name){
return Books::where("name",$name)->get();
}

我希望这会奏效

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从数据库中获取数据以在Laravel 8中查看页面?

如何显示数据库中的数据以在 Laravel 8.* 中查看

如何在 Laravel 8.* 中从数据库中删除数据

Laravel 8不显示数据库中的数据

如何在 Laravel 8 中将数组数据发布到数据库中

如何从数据库获取数据到 Laravel 8.* 中的布局

如何从 Laravel 8 中的数据库中获取样式元素

Laravel 8:使用 if 语句更新数据库中的记录

Laravel 8 不更新数据库中的信息

Laravel 8 中的备份数据库

如何使用laravel 8显示保存在数据库中的图像?

如何使用Laravel在数据库中搜索未知的属性名称

如何根据 Laravel 中的数据库查询结果选中复选框?

在laravel 8中搜索关系

在数据库中插入芯片并从laravel 8中的数据库中显示并实现css

在Laravel 8中使用vuejs从数据库中获取数据

显示使用 laravel 8 从数据库中获取的下拉列表中的数据

如何编写数据库查询以获取laravel-8中列值为1的所有记录?

如何将选定的复选框 ID 存储到我在 Laravel 8 上的数据库中

如何使数组从Laravel中的数据库中获取数据

Laravel 8 materialize css 芯片在数据库中存储芯片数据

Laravel 8:Eloquent Query 似乎显示从数据库中检索到的数据

使用数据库中的key和secret在laravel 8中实现Facebook登录

使用 Laravel8 将单列中的标签输入值存储到数据库中

Laravel 8 分别上传两张图片。在数据库中添加相同的图像名称

如何在 Ionic 中显示 Laravel 数据库数据

如何从Laravel中的数据库删除数据?

Laravel 8在数据库中手动保存JSON

BadMethodCallException:在 Laravel 8 的数据库播种器中调用未定义的方法