Problem of Class not found on Laravel for a form - php

I tried to follow this tutorial to make a work on Laravel : https://vegibit.com/how-to-set-up-form-submission-in-laravel/ but I got a strange bug and I can't found a way to fix it, I wanted to make a form to insert something in my database and show it in another page (the showing is right, it's just the form page that get this problem) here is a screenshot :
Code :
namespace Doreas\Http\Controllers;
use Illuminate\Http\Request;
use App\Demand;
class DemandeController extends Controller
{
public function index()
{
$demande = Demand::all();
return view('demande.index', ['demande' => $demande]);
}
public function show($id)
{
$demande = Demande::find($id);
return view('demande.show', ['demande' => $demande]);
}
public function create()
{
return 'it works';
}
}
I don't understand where the problem of Demand come from
<?php
namespace App\Doreas;
use Illuminate\Database\Eloquent\Model;
class Demand extends Model
{
public function scopeTest($query)
{
return $query->where('title', '=', 'test');
}
}
Where I declare the class

The problem is that you have imported the class Demand using use App\Demand;
However its namespace is actually App\Doreas so you should do use App\Doreas\Demand;

Related

using traits inside laravel controller

I have some repetitive codes inside my laravel(7.23.0)controller
use App\ModelA;
use App\ModelB;
use App\ModelC;
use App\Traits\DbTrait;
class DarsController extends Controller
{
use DbTrait;
public function A($id) {
return ModelA::where('column', $id)->get(*);
}
public function B($id) {
return ModelB::where('column', $id)->get(*);
}
public function C($id){
return ModelC::where('column', $id)->get(*);
}
//the only difference in these codes is model, all codes are the same
}
I had created a folder named Traits and inside that I had defined a trait DbTrait.php
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName , $id){
return $ModelName::where('column', $id)->get('*');
}
}
so I modified my controller's functions to this
public function A($id) {
// return ModelA::where('column', $id)->get(*); works fine
$this->getAllz('ModelA', $id);// throws an error
}
it throws an error message: "Class 'ModelA' not found"
thank you
update:
i should apologize, i am really sorry, 3 of the answer worked, and i see the data inside network tab,i am using vue to display data,
and i think using trait made a complex array
this is my simple vue
axios.get('/api/emla/' + id).then(response =>{
this.data = JSON.parse(JSON.stringify(response.data));
}
Import the classes which will be used in your trait as you have done in the controller class.
use App\ModelA;
use App\ModelB;
use App\ModelC;
the reason you're getting Class 'ModelA' not found is because it's looking for that class in your traits directory, which it won't find. so you need this instead:
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName , $id) {
return app( "\App\\" . $ModelName )::where('column', $id)->get('*');
}
}
assuming that you've defined your models under \App namespace
you need to modify your trait
ModelName::where it try to load the class in trait so use $ModelName->where here $ModelName is already instance of that class so you can call function via -> operator
<?php
namespace App\Traits;
trait DbTrait
{
public function getAllz($ModelName ,$columnName ,$id){
return $ModelName->where($columnName, $id)->get('*');
}
}
and to call this function
$this->getAllz(new ModelA, $id);
this way you don't need to import class inside trait
your final code will be like this
use App\ModelA;
use App\ModelB;
use App\ModelC;
use App\Traits\DbTrait;
class DarsController extends Controller
{
use DbTrait;
public function A($id)
{
return $this->getAllz(new ModelA,'column', $id);
}
public function B($id)
{
return $this->getAllz(new ModelB,'column', $id);
}
public function C($id)
{
return $this->getAllz(new ModelC,'column', $id);
}
}
100% should work if you follow this
public function A($id) {
// return ModelA::where('column', $id)->get(*); works fine
$model = new ModelName(); // new ModelName;
$this->getAllz($model, $id);// throws an error
}
public function getAllz($ModelName , $id){
return $ModelName->where('column', $id)->get('*');
}

Laravel, is there a way to include codes from resource/view path into controller?

My goal is to include codes from another sources which is located in resources/views. I have tried using resource_path('views/myfiles.php') but it does nothing.
Controller
class MyController extends Controller
{
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$theFilesLocation = "resources.views" . $request->input('name');
#include($theFilesLocation) //something like this
}
}
}
myfiles.php
<?php
dump("if this shows up, then the code works")
?>
Try bellow code but I think it is not a good way.
class MyController extends Controller
{
require_one(resource_path('views/myfile');
}
Or with Laravel File facade
class MyController extends Controller
{
\File::requireOnce(resource_path('views/myfile');
}
You should create a class and put your code there then call it from the controller is a better solution.
What you are looking for is a trait. This allows the easy sharing of code and functionality without having to inherit from a specific base class causing an inheritance hell.
namespace MyCode\Traits;
trait SharedCodeForThing {
public function blaTheBla() {
dump("if this shows up, then the code works");
}
}
and then in your controller
use MyCode\Traits\SharedCodeForThing ;
class MyController extends Controller
{
use SharedCodeForThing;
}
Now if you wish to just render the contents of the view which it seems you're after:
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$view = view('resources.views' . $request->input('name'));
return $view->render();//or echo $view->render(); whatever you like
}
}

How to call a model function inside the controller in Laravel 5

I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function

issue with php namespace in "Laravel"

I use subfolder in the Controller 'folder',which works fine..
but when I write the blow code ..php return the error said "Auth is not found ,and the Input'
<?php
namespace website;
use Auth;
use Input;
use View;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('wcsite.index');
}
public function saveHome()
{
$uid = Auth::user()->id;
$websiteData = Input::get('data');
return $uid;
}
}
but when I add 'use Auth,use Input',everything works fine...so ,anyone who can tell me ...is there any way to to this ,which "need not to use Auth,use Input in my subfolder Controllers' Thank you a lot!
and my route is
Route::post('/wcsite',array('uses' => 'website\HomeController#saveHome'))->before('auth');
Your question is a bit confusing. You're saying that the code above is not working because PHP can't find the Auth and Input global class references but your code clearly shows you're importing them correctly.
PHP can't use the global Auth and Input class references without importing them first (which you're doing in the above code). It's going to assume they're located under the website namespace by default.
If you don't want to import hem with use statements you could always reference the global namespace by using a backslash before the class name like the code below:
<?php
namespace website;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return \View::make('wcsite.index');
}
public function saveHome()
{
$uid = \Auth::user()->id;
$websiteData = \Input::get('data');
return $uid;
}
}
That being said, I prefer importing the classes first instead of using backslashes everywhere. It'll provide for much cleaner code.

Class Comment not found Laravel 4

Im trying to add a comment system to my laravel app.
But I can't seem to get it working.
I have two models
class Post extends \Eloquent {
protected $table = 'posts';
public function comments()
{
return $this->hasMany('Comment','postId');
}
}
and my Comment model
class Comment extends \Eloquent {
protected $table = 'comments';
public function post()
{
return $this->belongsTo('Post');
}
}
in my DashBoardController I'm trying to get the output from the models
use App\Models\Post;
use App\Models\Comment;
use Input, Redirect, Sentry, Str, View, Notification;
class DashboardController extends \BaseController {
public function index()
{
$post = Post::find(3)->comments()->comment;
print_r($post);die;
}
}
I think my database is properly linked, but now I'm getting the error
'Class Comment not found'.
Any advice on this one?
First try this: composer dump-auto (as commented by user1669496)
if this didn't helped then change your model...
Change this:
return $this->belongsTo('Post');
to smth like this:
$this->belongsTo('App\Models\Post');
Do the similar for Post model.
Just change App\Models\XXXX to your namespace where you have Post model saved.
I had similar problem and this helped me, hope it will help you.

Categories