Laravel : Eloquent Model::all() returns nothing - php

Books model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Books extends Model{
//
public $table = 'books';
}
My controller function.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Books;
class HomeController extends Controller
{
public function addbooks()
{
$book = Books::all();
print_r ($book);
}
}
So the function addbooks, doesn't return anything.
I am using sqlite DB.
Attached is a screenshot of my SQL result.
php artisan tinker result.

Laravel's convention is plural table names by default. But when you write a class name (for Model) it should be singular. Try 'Book' as your class name.

Related

Laravel Spatie translation package not working for Raw Queries and join operations

Used this Spatie/laravel-translatable
package i achived my localization with ORM queries like (Article::all();). But when i use raw query or join operations(DB queries), it shows the raw json from database.
Article.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class Article extends Model
{
use HasTranslations;
public $translatable = ['name'];
}
?>
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
use App\Article;
use App\User;
class TestController extends Controller
{
public function index()
{
return $result = DB::table('articles')->get();
}
}
?>
I had the same problem and my solution (the only way I found) was define an accessor to get the attribute translated:
public function getNameAttribute($value)
{
return json_decode($value)->{\App::getLocale()};
}
You are getting them trough the DB facade, therefor you get raw data from the database via Query Builder.
To get the translations you should query the articles table through the Article model making use of Eloquent ORM instead.
public function index()
{
return Article::get();
}
If you want to join some data by hand, you can:
Article::query()
->join('table', 'table.article_id', 'articles.id')
->get();

Where should I write raw-query or query using query builder instead of writing it in route file in Laravel?

<?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

FatalErrorException in productController.php line 16: Class 'App\Product' not found Laravel

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

Accessing model methods from controller class Laravel

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

How to call models in Laravel 5?

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)

Categories