When I am trying to send mail, everytime a new member is added to the user table, so that they can get a setup password link. I have been trying to get this to work but seem not to be.
public function store(AddUser $request)
{
$user = $request->all();
$user['activate'] = $this->active();
$user['guid'] = $this->guid();
$user['accountno'] = $this->generateAndValidateAccountno();
$check = User::find($user['phone']);
if(!$check) {
$id = User::create($user);
$this->sendEmail($user['accountno']);
}
return redirect('employee');
}
public function sendEmail(Request $request, $id)
{
$user = User::find($id);
Beautymail::send('emails.welcome', [], function($message)
{
$message
->to('$id->email', '$id->fname')
->subject('Welcome!');
});
}
}
Not sure what am doing wrong
Just use the same request class in the controller and the model. In your user model, add use Illuminate\Http\Request at the top of the class to tell it which Request class to use.
Just change:
public function sendEmail(Request $request, $id){...}
to
public function sendEmail($id){...}
Related
Is this possible to be less boilerplate in controllers when current user must be known?
class FooController extends Controller
{
function index(Request $request) {
$user = Auth::user(); // <------
return Foo::where('user_id', $user->id)->get()->toArray();
}
}
Is this possible to receive $user directly from a dependency injection?
You can do so from the Request class
$user = $request->user();
OR - using helper functions
$user = auth()->user();
in Controller.php you can add protected property
protected $user;
public function __construct()
{
$this->user = auth()->user ?? null;
}
then in all contoller you can do $this->user
I'm having a controller with some functions. In every function I get user data by sharing it from the Contoller.php
In controller.php
public function share_user_data() {
$user = Auth::user();
$this->checkValidation($user);
$this->user = $user;
View::share('user', $user);
}
public function checkValidation($user){
if($user->email_activated == 0){
var_dump($user->email_activated); // I get: int(0)
return redirect('/verifyEmail');
}
}
In the other controller
public function viewCategory(Category $category){
$this->share_user_data(); // here's the check
$this->get_site_lang();
$products = $category->products;
return view('category', compact('category','products'));
}
But I get the category view and not redirected to the verifyEmail route. How to fix this and why it's happening?
The controller function called by the route is the one responsible for the response. I guess it is viewCategory() in your example?
Your viewCategory() function is always returning view(). It must return redirect() instead. I think the main function should be responsible for picking the output of the request.
private function checkValidation($user) {
return $user->email_activated == 0;
}
public function viewCategory(Category $category) {
$user = Auth::user();
/* ... call to share_user_data() or whatever ... */
if ($this->checkValidation($user)) {
return redirect('/verifyEmail');
}
return view('category', compact('category','products'));
}
I am generating a pdf based on user input. I can call the databse and get ALL contract info using all(). However, I would only like to generate a pdf for one of the values. Each contract has a auto incrememnt id which i could use.
Whats the best way to communicate between controllers so only the contract I am referencing is used to generate a pdf?
PdfGenerateController:
public function pdfview(Request $request)
{
$users = DB::table("users")->get();
$contract = Contract::all();
view()->share('users',$users);
if($request->has('download')){
// Set extra option
PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
$users = DB::table("users")->get();
// pass view file
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML
($contract);
return $pdf->stream();
}
return view('sell.contract');
}
Contract Controller (user input)
public function store(Request $request)
{
$contract = new Contract;
$contract->buyer_first_name = $request->input('buyer_first_name');
$contract->listing_id = $request->input('listing_id');
$contract->save();
return redirect()->route('generate-pdf')->with('contracts',$contract);
}
Storing the contract id in the session is probably your best bet. If you need to use it only on the next request, you could just flash it (in fact, that's exactly what the redirect(..)->withInput(sessionKey, val) does).
Something like that:
Contract Controller
public function store(Request $request)
{
// ...
$contract->save();
session(['contract_id' => $contract->id]);
return redirect()->route('generate-pdf');
}
Or just flashing:
public function store(Request $request)
{
// ...
$contract->save();
return redirect()->route('generate-pdf')->with('contract_id', $contract->id);
}
PdfGeneratteController
public function pdfview(Request $request)
{
$contract = Contract::findOrFail(session('contract_id'));
// ...
}
I would probably create a PDF model class with a static method to generate the PDF.
class PDF
{
public static function generatePDF($id, $isDownload)
{
// ...
}
}
Then you could simply call the static method from the Contact controller and pass the required data as parameters.
public function store(Request $request)
{
// ...
\PDF::generatePDF($contract->id, $request->has('download'));
// ...
}
I have a controller like this
use App\Model\User;
class UserController extends BaseController
{
protected $model;
public function __construct(User $user)
{
$this->model = $user;
}
public function updatePhone(Request $request)
{
//user id
$id = $request->id;
//new phone number
$phone = $request->phone;
}
}
The function updatePhone is used to update the phone number of the user.So I can code like this
$res = $this->model->where('id',$id)->update('phone',$phone)
In this way, I do nothing in the user model.But I get used to like this
$res = $this->model->updatePhone($id, $phone);
And I must create function updatePhone in user model
public function updatePhone($id,$phone)
{
return $this->where('id',$id)->update('phone',$phone);
}
who is the better way to update a single model? Any answers or suggestions are excepted.
i created a UserController.php
use App\User;
//
class UsersController extends Controller
{
//
public function edit($id, User $user){
$user = $user->find($id);
return view('admin.user.edit' , compact('user'));
}
//
}
And edit views
but when i want to access to this url
url('/users/'. $user->id .'edit')
i got this error
Error edit() must be an instance of App\User, none given
!?
Make sure your route is like:
Route::get(/users/{id}/edit', 'UsersController#edit');
Then use it as:
url('/users/'. $user->id .'/edit');
or
url("/users/{$user->id}/edit");
And then your controller should be as:
public function edit($id) {
$user = User::find($id);
return view('admin.user.edit' , compact('user'));
}
The error says that the edit() function expects $user_id and User instance but you're only providing $user_id. If that's the case your code should look like this:
public function edit($id){
$user = User::find($id);
return view('admin.user.edit' , compact('user'));
}