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)
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;
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);
I am new to laravel just displaying all the data of a single table using model named 'Product.php' inside my 'app' folder. Unfortunatly i'm getting above error while doing so. Following is the controller code:
<?php
namespace App\Http\Controllers;
use App;
use App\Product;
use Illuminate\Http\Request;
use App\Http\Requests;
class productController extends Controller
{
function show(){
return Product::all();
}
}
and model code is following:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class products extends Model
{
protected $table='product';
}
Two mistakes:
1) remove letter "s".
2) Add first Upper case
...
class Product extends Model
{
protected $table='product';
}
To create a Model always use
php artisan make:model your_model_name
And your mistake is in your spelling
class products extends Model
It should be
class Product extends Model
I am new to Laravel, I am trying things around while going through a tutorial. This is where I am facing an unexpected behaviour.
I have a model tweet and a controller named tweetsController; when I call tweet::find() or any similar method I found this:
FatalErrorException in tweetsController.php line 13:
Class 'App\Http\Controllers\tweet' not found
I have also tried App\tweet::find().
Everything seems fine through tinker.
Please explain.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class tweetsController extends Controller
{
public function show(){
$data = tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
public function delete($id){
return "here we dele the tweet ".$id;
}
public function add(){
return "i add your tweet to database then show you all the tweets";
}
}
tweet.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tweet extends Model
{
protected $fillable = array(
'tweetHead',
'tweetBody'
);
}
?>
A few options the maybe generating this error:
The model/controller namespace is incorrect;
The name of the file and the class name for the model needs to be "Tweet" with the first letter in uppercase;
If you set the right namespace on the model "Tweet.php" and import that on your "TweetController.php"
I hope that helps :)
UPDATE:
In the TweetController.php add this
use App\Tweet;
Before the class declaration, like this
use App\Tweet;
class tweetsController extends Controller
{
And remember to change the controller name in the class declaration like this
class TweetsController extends Controller
{
And the controller filename will become "TweetsController.php"
The Model also has to be named "Tweet" and not "tweet" in the class declaration and the filename
class tweet extends Model
will become
class Tweet extends Model
and the file will be named "Tweet.php"
and everytime you need to call the model you will do this
public function show(){
$data = App\Tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
Add use Add\User below namespace in your controller