Mailgun Laravel - laravel-4

I installed Mailgun for Laravel. I then tried to run the example
$data = [];
Mailgun::send('emails.welcome', $data, function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
But I got the following error:
"Argument 1 passed to Bogardo\\Mailgun\\Mailgun::__construct() must be an instance of Illuminate\\View\\Environment, instance of Illuminate\\View\\Factory given, called in /Users/koushatalebian/CLG/CL2G/app/vendor/bogardo/mailgun/src/Bogardo/Mailgun/MailgunServiceProvider.php on line 33 and defined"
What is going on?

If you are using Laravel 4.2, Please use Illuminate\View\Factory instead of Illuminate\View\Environment.
Bogardo mail gun package pointing wrong file.
/Users/koushatalebian/CLG/CL2G/app/vendor/bogardo/mailgun/src/Bogardo/Mailgun/MailgunServiceProvider.php
View / Pagination Environment Renamed
If you are directly referencing the Illuminate\View\Environment class or
Illuminate\Pagination\Environment class, update your code to reference Illuminate\View\Factory and
Illuminate\Pagination\Factory instead. These two classes have been renamed to better reflect their
function.
Edit:
You can use the correct class by editing the following file:
vendor/bogardo/mailgun/src/Bogardo/Mailgun/Mailgun.php
in Line 5:
remove use Illuminate\View\Environment; and use use Illuminate\View\Factory;
in line 53:
remove
public function __construct(Environment $views)
{
$this->views = $views;
}
use
public function __construct(Factory $views)
{
$this->views = $views;
}
Hope this will fix.

Related

How to test a route in Laravel that uses both `Storage::put()` and `Storage::temporaryUrl()`?

I have a route in Laravel 7 that saves a file to a S3 disk and returns a temporary URL to it. Simplified the code looks like this:
Storage::disk('s3')->put('image.jpg', $file);
return Storage::disk('s3')->temporaryUrl('image.jpg');
I want to write a test for that route. This is normally straightforward with Laravel. I mock the storage with Storage::fake('s3') and assert the file creation with Storage::disk('s3')->assertExists('image.jpg').
The fake storage does not support Storage::temporaryUrl(). If trying to use that method it throws the following error:
This driver does not support creating temporary URLs.
A common work-a-round is to use Laravel's low level mocking API like this:
Storage::shouldReceive('temporaryUrl')
->once()
->andReturn('http://examples.com/a-temporary-url');
This solution is recommended in a LaraCasts thread and a GitHub issue about that limitation of Storage::fake().
Is there any way I can combine that two approaches to test a route that does both?
I would like to avoid reimplementing Storage::fake(). Also, I would like to avoid adding a check into the production code to not call Storage::temporaryUrl() if the environment is testing. The latter one is another work-a-round proposed in the LaraCasts thread already mentioned above.
I had the same problem and came up with the following solution:
$fakeFilesystem = Storage::fake('somediskname');
$proxyMockedFakeFilesystem = Mockery::mock($fakeFilesystem);
$proxyMockedFakeFilesystem->shouldReceive('temporaryUrl')
->andReturn('http://some-signed-url.test');
Storage::set('somediskname', $proxyMockedFakeFilesystem);
Now Storage::disk('somediskname')->temporaryUrl('somefile.png', now()->addMinutes(20)) returns http://some-signed-url.test and I can actually store files in the temporary filesystem that Storage::fake() provides without any further changes.
Re #abenevaut answer above, and the problems experienced in the comments - the call to Storage::disk() also needs mocking - something like:
Storage::fake('s3');
Storage::shouldReceive('disk')
->andReturn(
new class()
{
public function temporaryUrl($path)
{
return 'https://mock-aws.com/' . $path;
}
}
);
$expectedUrl = Storage::disk('s3')->temporaryUrl(
'some-path',
now()->addMinutes(5)
);
$this->assertEquals('https://mock-aws.com/some-path', $expectedUrl);
You can follow this article https://laravel-news.com/testing-file-uploads-with-laravel, and mix it with your needs like follow; Mocks seem cumulative:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
public function testAvatarUpload()
{
$temporaryUrl = 'http://examples.com/a-temporary-url';
Storage::fake('avatars');
/*
* >>> Your Specific Asserts HERE
*/
Storage::shouldReceive('temporaryUrl')
->once()
->andReturn($temporaryUrl);
$response = $this->json('POST', '/avatar', [
'avatar' => UploadedFile::fake()->image('avatar.jpg')
]);
$this->assertContains($response, $temporaryUrl);
// Assert the file was stored...
Storage::disk('avatars')->assertExists('avatar.jpg');
// Assert a file does not exist...
Storage::disk('avatars')->assertMissing('missing.jpg');
}
}
Another exemple for console feature tests:
command : https://github.com/abenevaut/pokemon-friends.com/blob/1.1.3/app/Console/Commands/PushFileToAwsCommand.php
test : https://github.com/abenevaut/pokemon-friends.com/blob/1.1.6/tests/Feature/Console/Files/PushFileToCloudCommandTest.php

count(): Parameter must be an array or an object that implements Countable

I'm facing strange case. I face an error in production env not while in dev it's working fine.
Development:
Laravel 5.4.28
PHP 7.0.13
MYSQL 5.7.17
Production:
Laravel 5.4.28
PHP 7.2.1
MYSQL 5.7.20
In implementation code. I used:
namespace App;
use Illuminate\Support\Facades\Storage;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class Artwork extends Model
{
use Searchable;
In development it works fine. But in production it gives me this error:
count(): Parameter must be an array or an object that implements Countable
in Builder.php (line 936)
as you can see in this pic:
Any idea what is the reason behind this? and how to fix?
Put this code at the beginning of your route file, it will work fine
if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
}
This is a documented change in PHP 7.2. You need to either update Laravel to 5.6 or downgrade PHP to version 7.1.
Replace
$originalWhereCount = count($query->wheres);
by
$originalWhereCount = count((array)$query->wheres);
in
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php
I was facing similar issue in Laravel 5.6. Where I was getting error for object based array. I knew that data in that particular variable will always remain object so i used to convert the object to array. Here is code sample:
$objectData = (array)$objectData;
echo "Total Elements in array are: ".count($objectData);
My server was on PHP 7.1 when I updated to PHP 7.2 I got the same issue.
After searching I found why this occurs. (This occurs because of a PHP update.).
so in my case, the error is solved by typecasting.
I just update all code where I used to count
Before
//this is before
count($adminDetails)
After updated
//after update all i typecast all the code where i used count
count((array)$adminDetails)
Goodluck
This error occurs because you are using a higher PHP version and your Laravel application is on an older PHP version.
✅ Simple solution:
Open: app/Providers/AppServiceProvider.php
And in: public function register() { ... } function add following code:
if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
}
In php 7.2+ count does not work on relation objects, you need to use:
$model->relation()->exists()
Not this (less than php 7.2):
count($model->relation)
i ran into the same problem (PHP 7.2 + Laravel 5.3) but i don't see any "good" answers here. For me, the problem occurs when i tried to start a Builder from a scope method on the model: SomeModel::forUser() calls scopeForUser(). Trying to build a new Query, it trips on a count($this->wheres) that gets no initial value (null). Because the magic static call to the scope starts the builder, no other conditions have been placed in the object so the property is still null at that point.
i thought it's worth sharing my solution first, then perspective on why i consider it better than Ben's answer. It's not personal, i just disagree.
Solution
i took a cue from this answer about overriding some of the core Illuminate\Database classes...
Extend Illuminate\Database\Eloquent\Model
Mine is App\Overrides\Database\Eloquent\Model
Extend Illuminate\Database\Eloquent\Builder
Mine is App\Overrides\Database\Eloquent\Builder
Extend Illuminate\Database\Query\Builder
Can you guess? App\Overrides\Database\Query\Builder
Tell Laravel to use YOUR Eloquent\Model:
config/app.php 'aliases' array, replace the 'Eloquent' value
with your Eloquent\Model FQN
My Model:
namespace App\Overrides\Database\Eloquent;
/*
* Notes:
* * Using replacement Query\Builder with ALIAS
* * Use of Builder in this class is MY Eloquent\Builder
*/
use App\Overrides\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Model extends EloquentModel
{
public function newEloquentBuilder($query)
{
return new Builder($query);
}
protected function newBaseQueryBuilder()
{
$conn = $this->getConnection();
$grammar = $conn->getQueryGrammar();
return new QueryBuilder($conn, $grammar, $conn->getPostProcessor());
}
}
My Eloquent\Builder:
namespace App\Overrides\Database\Eloquent;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
class Builder extends EloquentBuilder
{
public function __construct($query)
{
parent::__construct($query);
/*
* FIX #1: Set properties treated AS arrays
* to empty arrays on construct.
*/
$this->wheres = [];
// Any other properties treated as arrays should also be initialized.
}
}
My Query\Builder:
namespace App\Overrides\Database\Query;
use Illuminate\Database\Query\Builder as QueryBuilder;
class Builder extends QueryBuilder
{
public function __construct()
{
parent::__construct(...func_get_args());
/*
* FIX #2: Set properties treated AS arrays
* to empty arrays on construct.
*/
$this->wheres = [];
// Any other properties treated as arrays should also be initialized.
}
}
This safely preserves the framework's functionality, since the only actual change you're making is initializing properties that should have been in the first place. Everything else will pass instanceof checks used for dynamic loading and dependency injection.
Opinion
While i agree with #ben-harold about every comment he made saying "NEVER edit vendor code," i disagree with the "solution." It's an oversimplification to a much more complex problem.
Upgrade Laravel: to ensure support for PHP 7.2, jumping up several minor versions - if not major releases - is impractical for a lot of teams. As a long term objective, yes of course. As something i can do to get rid of the bug for my deadline? Nope. Upgrading takes a lot of planning and frequently a lot of rewrites as structures, names, and functionality change. It's something to prioritize, but not a need-it-now answer.
Downgrade PHP: same problem. Downgrading into PHP 5.x means A) PHP is EOL, which may be a deal breaker for a lot of customers who have security policies, and B) any usage of PHP 7.x language features have to be scrapped. As with upgrading the framework this is very likely to cause a lot of headaches. It's also an even less useful solution, since walking backward in the language just puts you farther behind and will require more long-term effort.
place the below line ob code before the class name in your controllers
if (version_compare(PHP_VERSION, '7.2.0', '>=')) {
// Ignores notices and reports all other kinds... and warnings
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
// error_reporting(E_ALL ^ E_WARNING); // Maybe this is enough
}
I was facing the same issue with an external created table (Not using migration or command),
After creating the model, I just assigned a table name, but the problem was in my model protected $fillable where I assign string instead of array and error occurred.
There is 2 possible solution for that.
Assign an array to your protected $fillable = ['filed1', 'filed2'];
Remove protected $fillable completely (Not Recommended)
class Abc extends Model
{
protected $table = 'cities';
protected $fillable = ['field1','field2', ...];
}
Model looking for countable parameter:
class ClassName extend Model {
protected $fillable=['column_name']; // column in DB of Model is in array
}
Before
count($collection['colors'])
Error:Expected type 'Countable|array'. Found 'string'
After
count((array)$collection['colors'])
It works for me!
'vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php' to:
$originalWhereCount = is_array($query->wheres) ? count($query->wheres) : 0;
I;m using laravel 6.x
for this case you can use this way:
$id = \DB::table('xxxx')->where('id', $id)->count();
I Solve this in Laravel 5.6
// in controller
public function index()
{
$todos = Todo::all();
return view('todos.index')->with(['todos' => $todos]);
}
// in view page
#if(count($todos) > 0)
#foreach($todos as $todo)
<div class="well">
<h3>{{$todo->text}}</h3>
<span class="label label-danger">{{$todo->due}}</span>
</div>
#endforeach
#endif

how to integrate twilio php video with codeigniter

I am trying use twilio video in a codeigniter.
(1) in config.php added this
$config['composer_autoload'] = 'Twilio/autoload.php';
(2) added Twilio directory where my application folder is.
folder structure
application
system
Twilio
In My controller code
public function startRoom()
{
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Jwt\Grants\SyncGrant;
use Twilio\Jwt\Grants\IPMessagingGrant;
$TWILIO_ACCOUNT_SID = '';
$TWILIO_API_KEY = '';
$TWILIO_API_SECRET = '';
}
I am getting this error
Parse error: syntax error, unexpected 'use' (T_USE) in D:\xampp\htdocs\video_code\application\controllers\Welcome.php on line 15
Line 15 is - use Twilio\Jwt\AccessToken;
Twilio developer evangelist here.
Tpojka had the right answer, I just wanted to follow up so that other people that might see this can see the answer.
The key is that, as the documentation says:
The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.
So, you want to update your controller code to look like this:
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Jwt\Grants\SyncGrant;
use Twilio\Jwt\Grants\IPMessagingGrant;
public function startRoom()
{
$TWILIO_ACCOUNT_SID = '';
$TWILIO_API_KEY = '';
$TWILIO_API_SECRET = '';
// and so on...
}
Hope this helps.

Issue using View()->Composer() in AppServiceProvider

I am trying to pass data to a view using the boot method in the AppServiceProvider, and have followed the steps outlined in one of the Laracast fundamentals videos. However, I am getting an error in my controller when trying to use this variable.
In my AppServiceProvider:
public function boot()
{
view()->composer('home', function($view){
$view->with('current', Post::orderBy('created_at', 'desc')->first());
});
}
So this should pass the data to my home view as far as I understand, so that I don't need to keep referencing it in all the methods I have in the controller for that view. I then have a homeController with the following method:
public function index()
{
//Get user
$user = Auth::user();
//OMDB API setup - Get Movie name, remove spaces, add into API request
$movie_one = str_replace(" ", "+", $current->movie_one);
$movie_two = str_replace(" ", "+", $current->movie_two);
return view('home', compact(array('user')));
}
Previously I was adding a variable called current in this method to get an entry from my Post table, but wanted to add it to the AppServiceProvider as I will have to re-declare it in other methods as well. The issue I have is that I try to use the $current object again in this method, but it isn't available to it? I get the following error:
ErrorException in homeController.php line 30:
Undefined variable: current
What can I do here? Can I pass that data from to a controller as well as the home view? Or is that not possible?
the code you written inside the boot method, will only be accessible inside home view, if you want to access the Post object inside controller too, you have to follow this, through this you can access your Post object in controller, as will as view.
app()->singleton('current',function($app){
return Post::orderBy('created_at', 'desc')->first();
});
So in your code, you can access the Post object like this,
$post_obj = App::make('current');
$post_obj->movie_two;
or directly
App::make('current')->movie_two;

Extending Form Validation in Codeigniter call to a member function on a non-object error

I know this has been asked before, and I've looked through every answer posted, but to no avail. Basically, I am trying to extend the Codeigniter form validation library on a CI2+ project, but every single time I get the error:
Fatal error: Call to a member function get_errors_array() on a non-object
Below is my code:
application/core/CB_Form_validation.php
class CB_Form_validation extends CI_Form_validation{
function __construct($rules = array()) {
parent::__construct($rules);
log_message('debug', "CB_Form_validaiton class initialized");
}
public function get_errors_array() {
return $this->_error_array;
}
}
and then in my ajax file I have the construct etc.
public function save(){
if($this->form_validation->run() == FALSE){
}
}
and inside that if statement I have tried the following:
echo $this->form_validation->get_errors_array();
echo $this->cb_form_validation->get_errors_array();
echo get_errors_array();
I have also tried placing the CB_Form_validation.php in application/libraries as well. Just as well, I have also tried this in my construct of my ajax controller
$this->load->library('CB_Form_validation');
$this->load->library('core/CB_Form_validation');
And I have set CB_ as my custom class prefix
Turns out that to fix this, you should do the following:
Move your custom form validation class to the application/libraries folder
You can keep the construct of your custom class how it is
Use $this->form_validation->method() to access the function you would like
As long as your loading the form_validation library, you don't need to perform $this->load->library('custom_class') because CI picks up on your custom prefix anyways
Hope this helps someone

Resources