如何在laravel中搜索多个表中的数据?

问题描述 投票:0回答:1

我是laravel的新手,如何获取倍数表数据以进行过滤器搜索?我已经查看过ID,主题名称,标准名称主题名称等数据,但是我搜索的主题仅显示主题名称,但是我需要搜索它显示的所有数据的全部信息。

这是我的表名和我需要的列(三个表不同)

1. Topics -> id, topic_name
2. Standards -> standard_name
3. subject -> subject_name

请帮助我..!预先感谢..!

这是我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\TopicRequest;
use App\Topic;
use App\Standard;
use App\Subject;
use DB;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;

class TopicController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     **/
     public function index()
     {
          $topics = Topic::leftJoin('standards','topics.standard_id', '=', 'standards.id')->
                   leftJoin('subjects','topics.subject_id', '=', 'subjects.id')
         ->select('topics.*' ,'standards.standard_name','subjects.subject_name')
         ->orderBy('id', 'ASC')
         ->paginate(10);
         return view('topic.index', compact('topics'));
     }
    public function create()
    {
        $standards = Standard::select('id','standard_name')->paginate(10);
        $subjects = Subject::select('id','subject_name')->paginate(10);
        return view('topic.create', compact('standards','subjects'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name'    =>  'required',
            'standard_id' => 'required',
            'subject_id' => 'required',
        ]);
        $topic = new Topic([
            'topic_name'    =>  $request->get('name'),
            'standard_id'    =>  $request->get('standard_id'),
            'subject_id'    =>  $request->get('subject_id'),
        ]);
        $topic->save();
        return redirect()->route('topic.index')->with('success', 'Data Added');
    }

    /**
     * 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)
    {
        $topic = Topic::find($id);
        $standards = Standard::select('id','standard_name')->paginate(10);
        $subjects = Subject::select('id','subject_name')->paginate(10);
        return view('topic.edit', compact('topic', 'id','standards','subjects'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $topic = Topic::find($id);
        $topic->topic_name = $request->get('name');
        $topic->standard_id = $request->get('standard_id');
        $topic->subject_id = $request->get('subject_id');
        $topic->save();
        return redirect()->route('topic.index')->with('success', 'Data Updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $topic = Topic::find($id);
        $topic->delete();
        return redirect()->route('topic.index')->with('success', 'Data Deleted');
    }
     public function search()
    {

        $search = Input::get('search');
        $topics = Topic::where( 'id', 'LIKE', '%' . $search . '%' )->orwhere( 'topic_name', 'LIKE', '%' . $search . '%' )->paginate(10);
        return view('topic.index',compact('topics'));


    }
}
laravel search
1个回答
0
投票

如果表中的搜索将彼此独立;您必须通过在每个表上执行单独的搜索来分别列出结果。

如果要搜索的表依赖于其他表中的搜索结果,则您无法雄辩地执行此操作。您必须输入原始sql。

如果这将是一个常见的过程,建议您搜索弹性搜索。

© www.soinside.com 2019 - 2024. All rights reserved.