I'm new to Laravel and I'm trying to figure out how to send confirmation emails to customers after they place an order.
I've read a few articles about it but couldn't find the answer I needed.
The following are the things I have done with the order process:
I have and Order controller which collects the informations I need when a customer pay for his products and save them to the database:
public function index()
{
$order = Order::all();
return response()->json(
[
'results' => $order,
'success' => true
]
);}
public function create(Request $request)
{
$data = $request->all();
$order = new Order();
$order->fill($data);
$order->save();
return response()->json([
'order' => 'New order created'
]);}
This is the Model:
class Order extends Model
{protected $fillable = ['customer_name','customer_address', 'customer_telephone','total','customer_email','user_id','cart'];
public function dishes() {
return $this->belongsToMany('App\Dish');
}}
And this is my web.php file:
Auth::routes();
Route::middleware('auth')
->namespace('Admin')
->name('admin.')
->prefix('admin')
->group(function () {
Route::get('/', 'HomeController#index')->name('home');
Route::resource('orders', 'OrderController');
Route::resource('restaurants', 'RestaurantController');
});
Route::get('{any?}', function() {
return view('guests.home');})->where('any', '.*');
And my api.php:
Route::get('/restaurants', 'Api\RestaurantController#index');
Route::get('/restaurant/{slug}','Api\RestaurantController#show');
Route::get('/users', 'Api\UserController#index');
Route::get('/categories', 'Api\CategoryController#index');
Route::post('/orders', 'Api\OrderController#store');
Can someone guide me step-by-step on what to do to collect the current email address that a customer has used and then send them a confirmation email?
Any help (also links to tutorials dealing with this situation) will be really appreciated
Related
Newbie to all the tech I'm using here.
Trying to query a table using Axios in Laravel with Vue.js. Here's what I got:
from my component's <script>
this.axios
.get('http://localhost:8000/api/tasks', {params: {vid: this.properties[0].vid}})
.then(response => {
this.tasks = response.data;
console.log(response)
})
Regardless of what vid is in the params, the response contains ALL of the table's rows.
I do know that this may have something to do with api.php, and the Controller associated with the request. I'll post those to be verbose.
routes/api.php
Route::middleware('api')->group(function () {
Route::resource('vehicles', VehicleController::class);
Route::resource('tasks', TaskController::class);
Route::get('tasks?={vid}', [TaskControler::class, 'search']);
});
Controllers/TaskController.php
class TaskController extends Controller
{
//
public function index() {
$tasks = Task::all()->toArray();
return $tasks;
}
public function store(Request $request){
$task = new Task([
'make' => $request->input('make'),
'model' => $request->input('model'),
'year' => $request->input('year'),
'mileage' => $request->input('mileage'),
'type' => $request->input('type'),
'VIN' => $request->input('VIN')
]);
$task->save();
}
public function show($tid){
$task = Task::find($tid);
return response()->json($task);
}
public function update($tid, Request $request){
$task = Task::find($tid);
$task->update($request->all());
return response()->json('Task Updated!');
}
public function destroy($tid){
$task = Task::find($tid);
$task->delete();
return response()->json('Task Deleted!');
}
}
I tried for a while to mess around with api and the controller, but to no avail. Most of the questions asked here give "just use params" as an answer, but despite my best efforts I don't seem to be able to get away with "just" params (willing to be proven wrong.)
I have a working function in my controller but I want to add a permissions gate in order to use the function.
Basically, right before the collect line in the first section, I want to add Gate::allows('can-fix', 'testFix') ?: parent::denyAccess(); but I'm testing it and even though I don't have the 'testFix' ability, it still lets me through
I don't want to add a gate at the route level, I want it to actually access the controller method but kick me out if I'm not allowed to do anything in the method itself.
Am I declaring the gate incorrectly here?
public function editCategorySort(categorySur $category, Request $request, $categoryGroup)
{
Gate::allows('can-fix', 'testFix') ?: parent::denyAccess();
collect($request->input('sur'))
->each(function ($asset) use ($categoryGroup, $category) {
$data = [
'category_sur' => $asset['category_sur'],
'type' => $asset['type'],
];
$category_sur_id = $asset['id'];
$category->updateCategoryGroup($categoryGroup, $category_sur_id, $data);
});
}
I am not sure how you defined your gate, but the code below works for me.
controller
Route::get('/test', function () {
Gate::allows('can-fix') ?: dd('you do not have the access');
dd('yes you can reach here');
});
app\Providers\AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Gate::define('can-fix', fn () => true);
}
so if fn () => true the result is "yes you can reach here"
if fn () => false the result is "you do not have the access"
Hope this helps!
In Laravel 5.8 app using tymon/jwt-auth 1.0.0
I have users_groups table and I need for logged user for some controller to make check if inside of some group.
For this in routes/api.php I have :
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'manager', 'as' => 'manager.'], function ($router) {
Route::get('users_of_event_selection/{event_id}', 'API\ManagerController#users_of_event_selection');
Route::post('add_user_to_event', 'API\ManagerController#add_user_to_event');
...
I app/Http/Controllers/API/ManagerController.php I added checks:
public function __construct()
{
$this->middleware('jwt.auth', ['except' => []]);
$request = request();
$this->requestData = $request->all();
$loggedUser= Auth::guard('api')->user();
$userGroupsCount = UsersGroups
::getByUserId($loggedUser->id)
->getByGroupId([ACCESS_ROLE_ADMIN,ACCESS_ROLE_MANAGER])
->count();
if($userGroupsCount == 0) {
return response()->json(['error' => 'Unauthorized'], 401);
}
}
But
$userGroupsCount == 0
the code above does not work as I expected and my control's method returns valid data. I suppose I can make
small function and to call it in top on any control's method, but if that ig good way? If jwt-auth has any way to extend
additive checks ?
Thanks!
I am getting this error (Route [companies.show] not defined.) and I don't know what to do.
Actually I am updating the data in CompaniesController and data is updating but the route is not working
Here is the code for that:
public function update(Request $request, Company $company){
$companyUpdate = Company::where('id', $company->id)->update(['name'=> $request->input('name'),'description'=> $request->input('description')]);
if($companyUpdate){
return redirect()->route('companies.show', ['company'=> $company->id])
->with('success' , 'Company updated successfully');
}
return back()->withInput();
And My web.php file is as follow `
Route::get('/', function () {
return view('welcome');});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/company','CompaniesController');
Thanks in advance for helping me
change companies.show to
return redirect()->route('company.show', ['company'=> $company->id])
->with('success' , 'Company updated successfully');
}
companies.show is undefined because you didn't give your route a name.
Route::get('/companies/{id}', 'CompaniesController#showCompanyForID')->name('companies.show');
Create a function called showCompanyForID in your CompaniesController and return the company which has the id requested for in your Request.
use Illuminate\Http\Request;
public function showCompanyForID(Request $request)
{
$id = isset($request->id) ? $request->id : 0;
if ($id) {
// do work here
}
return view('companies.company')->with(compact('var1', 'var2'));
}
You can now redirect to that route:
return redirect()
->route('companies.show')
->with(['company'=> $company->id, 'success' => 'Company updated successfully']);
To see all routes, cd to your project in cmd / Terminal and type: php artisan route:list
Laravel 5 has it's default register function which is in
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
I know that I can copy this code and paste it in my AuthController but there's a change that I need to make which I don't know where to start and find. What I want is change the code for the insertion of data in my users table. I want to change this because I add another column in my users table which is company_name and I have a table which is named companies so basically when the user enter a company_name for registration it will check the companies table if it is existing then return error message if it is. So think there is something like:
$rules = array(
'company_name' => 'unqiue:companies',
);
But i don't know where to put this thing in my registration code. Thanks
You can use a custom validation, in this case. Make sure, you a are calling $this->validate(), and not $this->validator. This validate will automatically redirect back with errors if it fails, so you can skip the check statement.
public function postRegister(Request $request)
{
$this->validate($request->all(), [
'company_name' => 'unique:companies',
// And the other rules, like email unqiue, etc..
]);
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}