I have created new controller in the project but I am getting Not Found (#404) error when I try to access it.
I am using the following URL
http://localhost/basic/web/index.php?r=users/index
here is the controller:
<?php
namespace app\Controllers;
use yii\web\Controller;
use app\models\Users;
class UsersController extends Controller
{
public function actionIndex()
{
$users= Users::find()->all();
return $this->render('index',['users'=>$users]);
}
}
?>
and here is the model:
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Users extends ActiveRecord{
}
?>
and here is the view:
<?php
foreach($users as $user){
echo $user->username."<br/>";
}
?>
solved, the error accorded because "Controller" shouldn't start with capital letter in the namespace.
Try to use http://localhost/basic/web/index.php?r=users/indexe , because i see that your indec function in controller is public function actionIndexe()
In Case of Advance, The namespace app must be change to frontend in addition to the Basel.shoban comment. like this frontend\controllers.
Related
have been trying to get the output of these code from my vew but its giving me issues, Please i woild really be bappy to get help.
In my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
$data = App\allLeave::find(1)->full_name;
return view('leave/allLeave',["data"=>$data]);
}
}
in my employee model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class Personnel
* #package App
*/
class Employee extends Audit
{
public function leave()
{
return $this->belongsTo('App\allLeave');
}
}
In allLeaveModel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class allLeave extends Model
{
public function empolyee()
{
return $this->hasMany('App\Employee');
}
}
in the blade
{{$data->employee->full_name}}
You already assign full name to data in controller. You only need {{ $data }} in blade
If i have to do the same i will do it simply like
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
/* No need to use App\allLeave because you already have that used in
top of the project */
$data=allLeave::findorFail(1);
return view('leave.allLeave')->with('data', $data);
}
}
in front end just use
{{$data->first_name}} //same column as in database table
Note: Make sure are using laravel eloquent model relationships
In your controller should be like this:
$data = App\allLeave::find(1)->empolyee();
And your blade:
{{$data->full_name}}
When i execute my code that returns:
Class 'app\Nota' not found
I try use app\Notas::all(); instead app\Nota::all();, in the controller, but didn't work. I try too use app\Notas; instead app\Nota; but didn't worked for me.
My model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Nota extends Model
{
//
}
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Nota;
class productoController extends Controller
{
public function show($id){
$notas = app\Nota::all();
print_r($notas);
die;
}
}
What can be the problem?
Replace your controller code like -
<?php
namespace App\Http\Controllers;
use App\Nota;
use Illuminate\Http\Request;
class productoController extends Controller
{
public function show($id){
$notas = Nota::all();
dd($notas);
}
}
Try using the following:
$notas = Nota::all();
and replace use app\Nota; by use App\Nota;
Here is the Model which simply inserts data into the table:
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserHandle extends Model {
public $timestamps=false;
}
Here is the Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\UserHandle;
class UserHandleCntrl extends Controller {
public function index(){
$data = new UserHandle;
$data->name='Laravel';
$data->email='dbtcbd#gmail.com';
$data->username='bdlaravel';
$data->password='laravel12345';
$data->save();
echo 'Data Inserted';
}
}
Here is the route :
Route::get('/user', 'UserHandleCntrl#index');
But when I am trying to insert data into the database I am getting error that model class not found. Why I am getting this and what is the solution. Please help. Bellow is the error screenshot.
Keep the a of App capital while you are including that model.
use App\UserHandle;
including model use App instead of app in your controller file.
use App\UserHanle;
I want to use Model functions in view
My controller function code:
$model = Tickets::find(1);
View::make('view')->withModel($model);
return view('index.search', ['tickets' => $result]);
My model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tickets extends Model
{
public function someFunction() {
echo 'hello world!';
}
}
My view code:
{{ $model->someFunction() }}
You need to import your model like this:
use App\Tickets;
right after line with namespace so it should look something like this:
<?php
namespace App\Http\Controllers;
use App\Tickets;
This should fix the issue:
use App\Models\Ticket;
For laravel 7:
use App\Tickets;
For laravel 8:
use App\Models\Ticket;
Your model should be
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tickets extends Model
{
public function someFunction() {
echo 'hello world!';
}
}
And controller function should be
$model = Tickets::find(1);
View::make('view')->withModel($model);
return view('index.search', ['tickets' => $result]);
To get this to work you will either have to use the full namespace:
$model = \App\Tickets::find(1);
Or add a use statement to the top of the controller:
use App\Tickets;
and load the model with:
$model = Tickets::find(1);
So, in L5 I created folder like app/Models/Blog where is file Posts.php which looks like:
<?php namespace App\Models\Blog;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model {
protected $table = 'posts';
}
After it I executed composer dump and then in my controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
It throws me an error:
FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found
Try changing this:
use Models\Blog\Posts as Posts;
To this:
use App\Models\Blog\Posts;
In Laravel 5.2 it's just:
use App\Blog;
or
use App\Blog\Posts;
Change the following
class Posts extends Model {
to
class Posts extends \Eloquent {
You need to check two points :
the namespace have to be in first
the using must be use App\Models\Blog in your case
Like this :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Blog;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
(tested with Laravel 5.4)