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;
Related
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(Auth::Attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Change the Auth namespace to:
use Illuminate\Support\Facades\Auth;
You may use the auth helper instead and then no worries about any class name
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(auth()->attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Try this :
\Auth::attempt([...]);
I think this is helpfull..
Use the Auth namespace to:
use Illuminate\Support\Facades\Auth;
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}}
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);
route in routes.php
Route::get('korisnici', array('uses'=>'MojPrviKontroler#prvaAkcija'));
//
my controler in Controllers
<?php
use App\User;
use App\Http\Controllers\Controller;
class MojPrviKontrolerController extends Controller
{
public $restful = true;
public function get_prvaAkcija()
{
return View::make('prviViewovi.PrviView.php');
}
}
Can somebody tell my why my controller isn't found?
Seems like a wrong namespace problem.
Change to this:
<?php
namespace App\Http\Controllers;
use App\User;
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)