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 checkif (User::where('email', '=', Input::get('email'))->count() > 0) {
// user found
}
Or even nicerif (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
Dynamic Class Property $$Value in PHP
Reverse Letters in Each Word of a String Without Using Native Splitting or Reversing Functions
Create an Assoc Array with Equal Keys and Values from a Regular Array
Png Transparency Resize with Simpleimage.PHP Class
How to Sort an Array by Similarity in Relation to an Inputted Word
Pdo_Sqlite Driver Not Present.. What to Do
MySQL Fulltext Search Multiple Words
PHP Explode Array Then Loop Through Values and Output to Variable
Compare Object Properties and Show Diff in PHP
Can One Modify the Templates Created by Artisan Migrate Command
Why Do PHP Array Examples Leave a Trailing Comma
How to Make a Chat Room Script with PHP
PHP - Regex - How to Extract a Number with Decimal (Dot and Comma) from a String (E.G. 1,120.01)
Base64 Over Http Post Losing Data (Objective-C)