How to Detect If a Database Table Exists with Laravel

Is there any way to detect if a database table exists with Laravel

If you are using Laravel 4 or 5 then there is the hasTable() method, you can find it in the L4 source code or the L5 docs:

Schema::hasTable('mytable');

Laravel : How to check if table exists in database on view file

You can use Schema's hasTable() method :

@if(\Illuminate\Support\Facades\Schema::hasTable('temptable')) 
// Your code
@endif

Laravel Checking If a Record Exists

It depends if you want to work with the user afterwards or only check if one exists.

If you want to use the user object if it exists:

$user = User::where('email', '=', Input::get('email'))->first();
if ($user === null) {
// user doesn't exist
}

And if you only want to check

if (User::where('email', '=', Input::get('email'))->count() > 0) {
// user found
}

Or even nicer

if (User::where('email', '=', Input::get('email'))->exists()) {
// user found
}

Laravel - Only query model if table exists

To answer the question being asked, you can use this:

if (Schema::hasTable('mytable')) {

}

On a side note, registering gates from the database in this way is a bit of an anti-pattern. Service providers don't usually have access to the authenticated user yet because they're responsible for preparing the application, and run before things like the session middleware that authenticates users. There are many other ways to structure authorization that won't cause problems like these.

Laravel, check if data/record exists in any related table

Try this:

User::where(function($query) { 
$query->has('storageItem')
->orHas('archiveObject');
})->find(1);

Finding out if a table is empty in laravel

If you want to check an array, use empty($orders), for a Laravel Collection you can use $orders->isEmpty()

Currently you're calling a function isEmpty(), which doesn't exist.

Try this:

$orders = Order::all();

if($orders->isEmpty())
{
echo "Im empty";
die();
}else{
echo "im not empty";
die();
}


Related Topics



Leave a reply



Submit