How To Check Table Is Exists Or Not In Database Using Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

The EXISTS operator is used to test for the existence of any record in a subquery.EXISTS operator returns true if the subquery returns one or more records.In laravel we can use the schema class to find out if a given table name is exist or not by using Where Exists Clauses we can check data is exists or not

How to Check Table Is Exists Or Not In Database With Laravel


In SQl we can check the data using below query
 

select * from student
where exists (
    select 1 from course where course.CourseID = student.CourseID 
)


To learn more about SQL, please visit our BajarangiSoft site.
 

Where Exists Clauses
The whereExists method allows you to write where exists SQL clauses. The whereExists method accepts a Closure argument, which will receive a query builder instance allowing you to define the query that should be placed inside of the "exists" clause:

Example(1)
app/Http/Controllers/studentController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;

class studentController extends Controller
{
    function index()
    {

        $student = DB::table('student')
            ->whereExists(function ($query) {
                $query->select(DB::raw(1))
                    ->from('course')
                    ->whereRaw('course.CourseID = student.CourseID');
            })
            ->get();
        return view('student.index', ['student' => $student]);
    }
}
?>


In above example check the data which present in course table and student table by equaling courseId in both the table.

resources/views/student/index.blade.php
 

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to Check Table Is Exists Or Not In Database With Laravel</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <br />
    <h3 align="center">Check Table Is Exists Or Not In Database With Laravel</h3>
    <br />
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
            <thead>
            <tr>
                <th>name</th>
                <th>id</th>
                <th>address</th>
                <th>gender</th>
                <th>age</th>
            </tr>
            </thead>
            <tbody>
            @foreach($student as $row)
            <tr>
                <td>{{ $row->name }}</td>
                <td>{{ $row->id }}</td>
                <td>{{ $row->address }}</td>
                <td>{{ $row->gender }}</td>
                <td>{{ $row->age }}</td>
            </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>
</body>
</html>


routes/web.php

Route::get('student', 'studentController@index');

Related Post