Laravel 5.7: Class 'App\Http\Controllers\MailableClass' not found - php

I created a Mailable called Class UserRequest
I'm trying to call it from inside a controller buy this is the error I get:
Class 'App\Http\Controllers\UserRequest' not found
I also tried ->send(new \UserRequest($msgdata)); but it still doesn't work.
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request)
{
$msgdata = array('subject'=>$request->subject,'email'=>$request->email, 'name'=>$request->name,'body'=>$request->body);
try
{
Mail::to('dddddddd#dddsdsf.com')
->send(new UserRequest($msgdata));
}
catch(Exception $e)
{
}
}
}

Include your class at the top like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\UserRequest; // including your class
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request){
$msgdata = array('subject'=>$request->subject,'email'=>$request->email,
'name'=>$request->name,'body'=>$request->body);
try {
Mail::to('dddddddd#dddsdsf.com')->send(new UserRequest($msgdata));
}catch(Exception $e){
// Log your exception
}
}
}

add 'App\Http\Controllers\UserRequest' to the head

use App\Http\Controllers\UserRequest;
at the top.

You will need to add the right path to the top as stated by other.
Also check the namespace in the UserRequest class

Related

Calling Laravel function dynamically

I just started learning Laravel.
I get an error message of "Error: Call to undefined function addPost()". What seems to be the problem in my code?
$command == "addPost";
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class PostsController extends Controller
{
function checkPostsCommand(Request $request) {
$command = $request->btn;
$data = $this->$command($request->all());
return response()->json($data);
}
private function addPost() {
return 'wew';
}
}
use it like this :
$this->$command();
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class PostsController extends Controller
{
function checkPostsCommand(Request $request)
{
$command = $request->btn;
return $this->addPost();
}
private function addPost() {
echo 'wew';
}
}

Class App\Http\Controllers\Panel does not exist

I'm following the Laracasts series and have run into an issue on the episode Laravel 5.4 From Scratch: Route Model Binding.
Laravel version:
Laravel Framework 5.6.13
The error:
Class App\Http\Controllers\Panel does not exist
This shows on both the /panel and /panel/1 pages
App\Http\Controllers\PanelController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
routes/web.php
// Main panel view
Route::get('/panel', 'PanelController#index');
// Individual panel view
Route::get('/panel/{panel}', 'PanelController#show');
App/Panel.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Panel extends Model
{
public static function activePanels()
{
return static::where('status', 1)->get();
}
}
Add this line in panel controller before the class
use App\Panel;
You need to add use App\Panel; to top of class
Or call it by full namespace $panels = App\Panel::all();
You don't included your model to class.
Add App\Panel to main include section:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panel;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
or load model in your class method manually:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PanelController extends Controller
{
public function index()
{
$panels = App\Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}

Laravel return variable in blade template

I have a route like below.
Route::get('profile/{username}', 'ProfileController#index');
ProfileController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
function index() {
view('profile.index');
}
}
profile/index.blade.php
{username}
But it doesn't echo the username when I go to /profile/salep, what's missing here?
If I change my ProfileController to this (below), it works but PhpStorm says "Unreachable stamement" for the view.
class ProfileController extends Controller
{
function index($username) {
return $username;
view('profile.index');
}
}
I didn't use the structure below (took it from the documentation) because I need to pass my variable to a view rather than returning, so I need to both return and pass it to a view, I guess.
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
You were nearly there with the second attempt.
Try this:
class ProfileController extends Controller
{
function index($username) {
return view('profile.index')->with('username', $username);
}
}
I solved it.
routes.php
Route::get('profile/{username}', 'ProfileController#index');
ProfileController.php
class ProfileController extends Controller
{
function index($username) {
return view('profile.index')->with('username', $username);
}
}

Laravel 5 fails to pass variable to all views

I have a problem with passing variable (data) to all Views. I created BaseController that extends default Laravel controller and "global" variables are defined there. When I extend other controller with BaseController i got error that variable is not defined. Does someone knows where's the problem?
Here is code:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class BaseController extends Controller {
public function __construct() {
$obavijesti="Other data";
$izbornici="Some data";
View::share ( 'izbornici', $izbornici );
View::share ( 'obavjesti', $obavjesti );
}
}
class AdminController extends BaseController {
.
.
.
echo '<pre>';var_dump($izbornici);echo '</pre>';//Error pop ups here
.
.
.
}
You are doing something wrong here. view::share() is used for sharing a piece of data across all views not controller.
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
//If you wish to get these variables in your other controllers you do this:
class BaseController extends Controller {
public $obavijesti="Other data";
public $izbornici="Some data";
public function __construct() {
View::share ( 'izbornici', $this->izbornici );
View::share ( 'obavjesti', $this->obavjesti );
}
}
class AdminController extends BaseController {
//if you have a constructor in other controllers you need call constructor of parent controller (i.e. BaseController) like so:
public function __construct(){
parent::__construct();
}
public function Index(){
echo $this->obavijesti;
}
}
You can also use a composer to share variables to views
//1. Create a composer file at app\Composers\AdminComposer.php
//NB: create "app\Composers" if does not exists
//2. Inside AdminComposer.php add this.
<?php namespace App\Composers;
class AdminComposer
{
public function __construct()
{
}
public function compose($view)
{
//Add your variables
$view->with('izbornici', 'Other data')
->with('obavjesti', 'Some other data');
}
}
//3. In you controller do this:
<?php namespace App\Http\Controllers;
//NB: I removed your BaseController because I believe the issue is coming from //there
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class AdminController extends Controller{
public function __construct(){
//Lets use AdminComposer to share variables to adminpage.blade.php view
View::composers([
'App\Composers\AdminComposer' => array('adminpage')
]);
}
public function Index(){
return view('adminpage');
}
}
Ideally you're going to want a combination of what you have, and the other answer posted here.
<?php
class BaseController extends Controller {
protected $obavijesti = 'Other data';
protected $izbornici = 'Some data';
public function __construct() {
View::share('obavjesti', $this->obavjesti);
View::share('izbornici', $this->izbornici);
}
}
Then in all of your views, you have access to the variables $obavjesti and $izbornici. Now in your other controllers, anything that extends BaseController can do the following:
class AdminController extends BaseController {
public function index() {
echo $this->ixbornici;
echo $this->obavjesti;
}
}

Laravel 5 - Where to define functions and call them in views & controllers

I have following function and want to call it from view. Basically i want to put all common functions in one file. I am not sure where to create that file and how to call it inside controller and view.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
So far i have created CommonController.php in app/Http/Controllers and put above function in it.
Then in other controller i have tried to call it following way:
use App\Http\Controllers\Common;
class SongsController extends Controller {
public function index($id)
{
echo Common::BytesToMB('7012187');
}
}
But i am getting error:
Class 'App\Http\Controllers\Common' not found
Ok, new try. You missed to use the complete class name and add the static keyword:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public static function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends Controller {
public function index($id)
{
echo CommonController::BytesToMB('7012187');
}
}
Another and more OOP solution is to use the function from the parent class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
protected function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends CommonController {
public function index($id)
{
echo $this->bytesToMB('7012187');
}
}

Categories