I am using the following laravel package for my newsletters:
laravel-newsletter.
I have the following code in my newsletterController.php:
public function index() {
Newsletter::subscribe('rincewind#discworld.com', ['FNAME'=>'Goti', 'LNAME'=>'Loki'], 'test');
return 'Ola !';
}
Now when i go to: /newsletter in my application i see Ola !, but when i open my mailchimp dashboard i see don't see rincewind#discworld.com added to the list of emails for the list test.
I also tried the following method of the mailchimp package:
return Newsletter::isSubscribed('codedevakagautam#gmail.com');
This email address already exists in the list test, i get the following error:
Call to undefined method Spatie\Newsletter\Newsletter::isSubscribed()
What am i doing wrong ? Can somebody please guide me.
Please, verify if you have added an entry to the providers array at your config/app.php
// config/app.php
'providers' => [
...
Spatie\Newsletter\NewsletterServiceProvider::class,
...
];
And at aliases array on the same file
// config/app.php
'aliases' => [
..
'Newsletter' => Spatie\Newsletter\NewsletterFacade::class,
];
Also, you need to import the package on your controller. By doing:
use Newsletter;
Be sure to import this way.
If you did all of the steps above, now you can try to use the method:
return Newsletter::isSubscribed('codedevakagautam#gmail.com');
And to subscribe users to a list, you need to send a 'status' parameter.
The possible values for this field is:
subscribed
unsubscribed
cleaned
pending
See the docs here:
http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/
If this does not work for you, you can have a look at this package:
https://github.com/drewm/mailchimp-api
Related
Route::post('order', 'OrderController#store')->name('order');
When I browse to the URL http://127.0.0.1:8000/order it shows the error:
The GET method is not supported for this route. Supported methods: POST.
Which is the correct.
But I want to redirect user to home page instead of showing this error.
First of all note that what you are trying to do seems like an anti-pattern for Laravel. Accessing a route with the wrong method should be denied!
I currently do not know about altering the default way of handling the wrong method error and I wouldn't advise to do that. But you can work around it:
Patching
Method 1
Keep your routes file clean but alter the original route line and add some lines to the beggining of the controller method
Route::match(['get', 'post'], 'order', [OrderController::class, 'store'])->name('order');
public function store(Request $request)
{
if ($request->isMethod('get')) {
return to_route('home');
}
// ...
Method 2
Keep your controller clean, but add a line to your routes file
Route::get('order', fn () => to_route('home'));
I am using Laravel 6.2 and Maatwebsite/Excel 3.1 here I am trying to import a excel file and willing to get data present in it in an Array format and in the Controller I need to validate some data among them and along with some other fields i need to insert that excel fetched array data to the database. Laravel application has no direct database connection. I am calling an API to insert those fields to the database, but the import operation giving Database connection [] not configured. error. Help me to sort out actual issue from it.
Controller Code
public function importExcelData(Request $request) {
// dd($request->excelFile);
$data = Excel::import(new UsersImport, request()->file('your_file'));
dd($data);
}
UsersImport File
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToArray;
class UsersImport implements ToArray
{
public function Array(Array $tables)
{
return $tables;
}
}
?>
After following the error stack appeared in the browser console's network response I got that I can solve this error very easily.
as per Laravel Excel Documentation
enter link description here
we need to run this command in a command prompt
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
after executing this command 'excel.php' file got created under config folder of the application
in this file , we can found this code
'transactions' => [
'handler' => 'db',
],
we need to change it as
'transactions' => [
'handler' => 'null',
],
Now after dumping the import result using dd(), I am seeing those data present in that excel file.
I am creating a package which gives a config file to customize the route url which it will add, I can see config file values in the controller, but same config('app_settings.url') is coming as null in
pakacge/src/routes/web.php
Route::get(config('app_settings.url'), 'SomeController')
my tests are also giving 404 and app_settings config change is not getting picked by route.
function it_can_change_route_url_by_config() {
// this should be default url
$this->get('settings')
->assertStatus(200);
// change the route url
config()->set('app_settings.url', '/app_settings');
$this->get('app_settings')
->assertStatus(200);
$this->get('settings')
->assertStatus(400);
}
app_setting.php
return [
'url' => 'settings',
'middleware' => []
];
It works when I use this package, but tests fail.
Please help How I can give the option to change the route url from config.
To be honest I think it's impossible to make such test. I've tried using some "hacky" solutions but also failed.
The problem is, when you start such test, all routes are already loaded, so changing value in config doesn't affect current routes.
EDIT
As alternative solution, to make it a bit testable, in config I would use:
<?php
return [
'url' => env('APP_SETTING_URL', 'settings'),
'middleware' => []
];
Then in phpunit.xml you can set:
<env name="APP_SETTING_URL" value="dummy-url"/>
As you see I set here completely dummy url to make sure this custom url will be later used and then test could look like this:
/** #test */
function it_works_fine_with_custom_url()
{
$this->get('dummy-url')
->assertStatus(200);
$this->get('settings')
->assertStatus(404);
}
Probably it doesn't test everything but it's hard to believe that someone would use dummy-url in routing, and using custom env in phpunit.xml give you some sort of confidence only custom url is working fine;
After processing form input, I redirect to a new route with some flash data:
return Redirect::route('work.index')
->with('flash', 'New work entry has been entered');
In the controller specified by work.index, I try to access the data
$flashed = Session:get('flash');
However, instead of a string, I end up with an array with two sub-arrays, old and new
Am I doing something wrong? Am I supposed to do this?
$flashed = Session::get('flash')['new'][0]
Store Data for next request
Session::flash('city', 'New work entry has been entered');
Retrieve Data from last request
$data = Session::get('city');
return Redirect::route('work.index')
->with('data', $data);
My Advice is to use Laracasts/Flash package that helps you to manage Flash messages in an easy way.
Here the GitHub repo: https://github.com/laracasts/flash
Installation
First, pull in the package through Composer.
"require": {
"laracasts/flash": "~1.0"
}
And then, if using Laravel, include the service provider within app/config/app.php.
'providers' => [
'Laracasts\Flash\FlashServiceProvider'
];
And, for convenience, add a facade alias to this same file at the bottom:
'aliases' => [
'Flash' => 'Laracasts\Flash\Flash'
];
And you can use it with:
Flash::info('Message')
Flash::success('Message')
Flash::error('Message')
Flash::warning('Message')
Flash::overlay('Modal Message', 'Modal Title')
Now in your theme you can easly integrate it with:
#include('flash::message')
NB:
Note that this package is optimized for use with Twitter Bootstrap.
I am a total newbie with Laravel and learning it now for a week. I have some basic questions that I can't find an answer to. Next week I will start with developing CRM system and I need some info from experienced developers who could tell me is the approach I am attending to make a good one.
I will need some authentication system, like 4 groups of users (Admin, Basic, Manager, Office) where Manager and Admin will add the Basic users. There will be few view and features and every groups will have defined access to each view and feature. Since few days I am searching for packages, watching the tutorials and learning. I found an interesting package for which I think it could help me with this user-group-permission things.The package is Sentry. Could this help me with my requirements?
What is the case when for example I have a user in group Basic and he deletes for example some comment with the button. On the left side down in the browser the user can see the link to this comment when he hovers the link. For example www.test.com/comments/345/delete where the id is 345. What if user types that with another id, that means he can delete another comment. I found some suggestions on how to solve this, to make it with jQuery and javascript so the link wouldn't be shown and POST would be made with for example with AJAX. But since I am a newbie, I am thinking how much time would this take and is this a good approach at all? Could package Sentry from 1. question help me with the permission on what route each group can access?
Any help or advice would be appreciated.
Sentry does what you want, yes. Here's a question with some answers explaining the permissions part.
The visible link part can be avoided by doing a POST request instead of a GET request.
When you open your form, you add a method attribute.
Form::open(array('url' => 'foo/bar', 'method' => 'post'))
A GET request will put the parameters in the URL, hence the visible ID. Using a POST request will put the parameters in the headers, thus hiding it from the URL.
An example could be deleting a comment. A GET request could look like this:
http://www.example.com/comments/delete/1
And the parameters would be defined in your method signature:
public function getDelete ($id) {
Comment::find($id)->delete();
}
Where the POST equivalent would be
http://www.example.com/comments/delete
And the parameters would be defined in your Input class, you would get them using the get method
public function postDelete() {
Comment::find(Input::get('id'))->delete();
}
1) The best package to help you with that is Sentry indeed.
2) To make sure an user can delete only his comments you can do something like this (but there are more solutions either you do it with Ajax or not):
public function destroy($id) {
$user = Sentry::getUser();
$comment = Comment::find($id);
if($comment) {
if($comment->user_id != $user->id) {
return Response::back(); // optional message: Permission denied!
}
$comment->delete();
return Response::back(); // optional with message: Deleted!
}
return Response::back(); // optional message: Comment not found!
}
You can use Sentry in this case to get the logged in user and check for user id. I think you should let user delete their own comments always but if you need special roles (Admins for example) to be able to delete any comment, or special permission comments.delete (For some Managers) - you can use Sentry as well:
public function destroy($id) {
$user = Sentry::getUser();
$comment = Comment::find($id);
if($comment) {
if($comment->user_id != $user->id && !$user->hasRole('Admin') && !$user->hasPermission('comments.delete'))) {
return Response::back(); // optional message: Permission denied!
}
$comment->delete();
return Response::back(); // optional with message: Deleted!
}
return Response::back(); // optional message: Comment not found!
}
A nicer way of making the DELETE thru a Form request check this:
Laravel RESTfull deleting