I have a controller in Laravel 5.0 like this-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CustomersController extends CustomerLayoutController
{
public function getDashboard()
{
return $this->view('layouts.customer.dashboard', []);
}
public function getTest()
{
return $this->view('layouts.admin.webinar', ['qustions' => DB::table('qustions')->get()]);
}
}
So, I want to pass DB::table('qustions')->get() as a parameter to my views, but I m getting error.
What I am doing wrong?
You're getting an error because Laravel is searching the class is the wrong namespace (it's "appending" the class to the current class's namespace, if you note).
You either import the DB class with the use keywords:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
or let it know that DB resides in the "global" application namespace, so call it with a backslash:
return $this->view('layouts.admin.webinar', ['questions' => \DB::table('qustions')->get()]);
Related
I'm having some problems with my Laravel APP.
I'm using laravel 8 and every time when I try to visit home page it gives me like:
Function () does not exist
This is how my routes looks like:
Route::get('/{any}', [VueController::class])->where('any', '.*');
And here how VueController looks like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function __invoke()
{
return view('application');
}
}
VueController::class second parameter is missing it should be
Route::get('/{any}', [VueController::class,'index'])->where('any', '.*');`
then in controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function index()
{
return view('application');
}
}
Route::get() get req second array must have function name like [VueController::class,'index']
if you don't want to mention index then in resouncse it is possible but it generate some url with some function it will not work in __invoke()
ref link https://laravel.com/docs/8.x/routing
Here is my code in controller, am trying to sort columns in view file by just clicking on corresponding column name
<?php
namespace App\Http\Controllers;
use App\Http\Controllers;
use Illuminate\Http\Request;
use App\Service;
use DB;
use JsValidator;
use Validator;
use Session;
use Redirect;
use Carbon\Carbon;
use App\Libraries\GlobalHelpers;
use App\Libraries\ImageHelpers;
use Auth;
use Response;
class ServicesController extends Controller
{
protected $serviceValidationRules = [
'service_name' => 'required'
];
public function index()
{
$services = Service::all()->sortable()->get();
return view('services.index')->with('services', $services);
}
?>
Am getting following error
Method Illuminate\Database\Eloquent\Collection::sortable does not exist.
Kindly help me to resolve
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class testing extends Model
{
}
should i write my all queries in this model class,even if i just want to use query builder no eloquent?
what are the good practices to write raw query or using query builder?
You can write your queries or say eloquent in your model differenciating from other logical code
Say for example you have AlbumController
namespace App\Http\Controllers;
use App\Album;
use App\Http\Controllers\Controller;
class AlbumController extends Controller
{
public function index()
{
$albums = Album::get_albums();
// other logical code
}
}
And in Album.php which is model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
public static function get_albums() {
$albums = Album::get();
return $albums;
}
}
And in your route file
Route::post('album', 'AlbumController#index');
Hope you get idea!
No that is not only the method to communicate with your database it is one possible solution only. You can use DB for query like
<?php
Namespace App\Http\Controllers;
use DB;
Class AbcController extends Controller{
Public function functionName(){
$data=DB::table(‘tableName’)->get();
return view(‘desiredPage’)->with(‘data’, $data);
}
}
go to the link for more laravel database query information
Hello i am trying to use a trait from controller in my register controller but it can't seem to find it
the error message:
Trait 'MailVerification' not found
The class in which i want to use the trait
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
use MailVerification;
Here i call the function
protected function create(array $data)
{
$mail = $data['email'];
$this->sendVerification($mail);
Here is the trait in the class i am trying to import it from
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
trait MailVerification
{
public function sendVerification($mail)
{
$verification_code = str_random(30);
Mail::send('mail.verify', ['verification_code' => $verification_code, 'mail' => $mail], function ($message) use ($mail)
{
$message->from('test#laravel.com');
$message->to($mail);
});
Session::flash('message', "Please check you're email to verify your account");
return redirect('/');
}
}
class MailController extends Controller
{
I have the trait outside of my class i don't know if this is correct but it was giving me an error while it was inside the class.
The namespace of your controller RegisterController and your trait MailVerification is different...
So, you'll have to add this line to your RegisterController
use App\Http\Controllers\MailVerification;
Also, I suggest you to put all your traits inside App\Traits folder instead of your controller. Try following a simpler way if possible :)
Edit --
This is how you register controller should look like
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MailVerification;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers, MailVerification;
//Your code here....
}
I'm having a trouble using a model in a controller in Laravel 5.0. I created the model in a folder model under App. The code of the model id described as:
<?php namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Observacion extends Model{
protected $table = 'obs_usuarios';
protected $fillable = ['observaciones', 'usuario_id','autor_id','tipo'];
}
Part of the code of the controller where I pretend to use it, is:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\Contrato;
use app\models\Observacion;
use App\models\Perfil;
use Illuminate\Http\Request;
use App\models\Configuracion;
use App\models\Usuario;
use App\models\Categ_profesores;
use Carbon\Carbon;
use Illuminate\Support\Facades\Input;
use Auth;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class SecretariaController extends Controller {
public function observacion($msg,$user_id,$autor_id,$type){
$observacion = new Observacion();
$observacion->observaciones=$msg;
$observacion->usuario_id=$user_id;
$observacion->autor_id=$autor_id;
$observacion->tipo=$type;
$observacion->save();
}
I even made dump-autoload but I get this error:
FatalErrorException in SecretariaController.php line 155:
Class 'app\models\Observacion' not found
The line 155 is where I do: $observacion = new Observacion();
So, I don't know what else to check.
Change:
use app\models\Observacion;
to:
use App\models\Observacion;
The first letter is a capital. In PHP names are case-sensitive.