Laravel 5 Method [ALL] does not exist - php

I'm trying to make a CRUD application in Laravel 5 to better understand how a php platform work ( first time using one ), but I keep getting the "Method [all] does not exist." error and I can't understand what i'm doing wrong. I have a table named 'products' in Mysql and what I'm trying to do, is to list all the entry's from within the table. I also have an index.blade.php that I will post if needed.
ProductsController.php
class ProductsController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$products = ProductsController::all();
return View::make('products.index')->with('products', $products);
}
Products.php
class Products extends Eloquent
{
}
routes.php
Route::resource('/', 'ProductsController#index');

Eloquent will assume the User model stores records in the users table. You may specify a custom table by defining a $table property on your model. [Ref]
So, you should rename your Products.php to Product.php (or define the $table property on your model).
Then you can retrieve all products:
$products = Product::all();
Product.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
//
}
ProductsController.php
<?php namespace App\Http\Controllers;
use App\Product;
class ProductsController extends Controller {
public function index()
{
$products = Product::all();
return View::make('products.index')->with('products', $products);
}
}

Why you write ProductsController to access all() function. To access all() function you must call Product model.
ProductsController::all();
Example
Product Model
class Product extends Model
{
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'products';
}
Product Controller
class ProductController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$products = Product::all();
return View::make('products.index')->with('products', $products);
}

Related

Using Eloquent to retrieve the parent category name?

i have a simple application for recursive hasMany relationship with unlimited subcategories
the code is running without a problem but it's not retrieving the parent category name or title.
when i tried to loop through the data coming from the DB it have the parent id but not the parent category name
i need to get the parent category name
here is the code for both controller and model
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$parentCategories = Category::where('parent_id',1)->get();
return view('welcome', compact('parentCategories'));
}
}
?>
Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $guarded = [];
public function subcategory(){
return $this->hasMany('App\Category', 'parent_id');
}
}
?>
You should create another relation in same Category model.
public function parent() {
return $this->belongsTo('App\Category', 'parent_id');
}
And retrieving parent model will be like that:
$subcategory->parent->title;

laravel getting error in one to manay relationship

I am trying to get record(one to many) from database. But it only provides me product table data not both
table structure
my model
namespace App;
use Illuminate\Database\Eloquent\Model;
class brand extends Model
{
protected $table="brand";
protected $primaryKey = 'brand_id';
public function product(){
return $this->belongsTo('App\product','brand_id','brand_id');
}
}
my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\brand;
class BrandController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$val=brand::with('product')->find(5)->product;
foreach($val as $va){
echo print_r($val);
}
}
It provides me the record from product table but not from both the tables
You are fetching only product, remove the product part to get the both:
$val=brand::with('product')->find(5);
Then to get brand_name-
$brand_name = $val->brand_name;
foreach($val->product as $product){
$prod_fault = $product->prod_fault;
}
Don't forget as #alexey mezenin said, make your hasMany() relation in your Brand model like-
public function product(){
return $this->hasMany('App\product','brand_id','brand_id');
}
Since brand has many products and product belongs to brand, the relationship must be hasMany() and not belongsTo():
return $this->hasMany('App\product', 'brand_id', 'brand_id');
Product Model
public function brands()
{
return $this->hasMany(Brand::class);
}
Brand Model
public function product()
{
return $this->belongsTo(Product::class);
}
BrandController index method
public function index()
{
$brand = Brand::with('product')->find(1);
echo ($brand);
}
Out Put:
{"id":1,"name":"Toyota","product_id":1,"created_at":"2018-02-05 00:00:00","updated_at":"2018-02-05 00:00:00","product":{"id":1,"name":"Car","created_at":"2018-02-05 00:00:00","updated_at":"2018-02-05 00:00:00"}}
For further review
Guide for using Eloquent ORM with laravel by scotch.io
Another Link
Laravel Eloquent Relationships from laravel documentation

Issue adding attribute values to Laravel model

I'm trying to create an additional attribute method in a model which appends a ratings label column to an existing rating model. I have the exact same code working for another model which adds click data. I'm getting an Undefined index: rating_label error - can't figure out why? Any help much appreciated.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Rating extends Model
{
protected $table = 'rating';
protected $primaryKey = 'rating_id';
protected $appends = array('rating_label');
/**
* Define a 1-many inverse relationship in eloquent
*/
public function Article()
{
return $this->belongsTo(Article::class, 'article_id');// (Model, primary_key)
}
/**
* Add additional column to Rating model for use in ClassifyingService class
*/
public function getRatingLabelAttribute()//accessor getter/setter
{
return $this->attributes['rating_label'];
}
}

Get model with hasMany is not working in Laravel

I try to print on my view all the atleti associated with a squadra x but the method $squadra->atleti() always returns me ant empty array.
Have you got some ideas to get this working?
Down below you can find my code and my db structure.
Squadra is team in english. One team can be associated to many atleti (it stands for players).
I think there is some problems with the foreign key.
Thank you for your help.
<?php
//Squadra Model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Squadra extends Model
{
protected $connection = 'custom_mysql';
protected $table = 'squadra';
/**
* Get the Allenatore for the Squadra.
*/
public function allenatore()
{
return $this->belongsTo('App\Models\Allenatore');
}
/**
* Get the Atleta for the Squadra.
*/
public function atleti()
{
return $this->hasMany('App\Models\Atleta');
}
}
<?php
//Atleta Model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Atleta extends Model
{
protected $table = 'atleta';
protected $connection = 'custom_mysql';
/**
* Get the Atleta for the Squadra.
*/
public function squadra() {
return $this->belongsTo( 'App\Models\Squadra' );
}
}
<?php
//Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class AtletaController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function getSquadra($id)
{
$squadra = Squadra::find($id);
return view('custom.squadra.dettaglio', compact('squadra'));
}
//View
#foreach( $squadra->atleti() as $atleta )
<tr class="success">
<td>{{ $atleta->id }}</td>
<td>{{ $atleta->nome}}</td>
<td>{{ $atleta->cognome}}</td>
</tr>
#endforeach
Here it is the output of dd($squadra)
Your are sending the atleti to the view, instead of the squadra. Try to change your controller function to this:
public function index()
{
$squadra = Squadra::firstOrFail(); // You need a paremeter in a route that defines which squadra to show. This will show the first one.
return view('custom.atleta.index', compact('squadra'));
}

how to join two models in laravel 5.2

I have two models as Project and Collaborator this is My tables
Project column
id
project_name
project_note
user_id
Collaborator Model
id
project_id
collaborator_id
I need join this models to get project_name instead project_id of Collaborator Model.
how can I do this. I read www.laravel.com documents but difficult to understand. help me in code...
project Model
<?php
namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
/*
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['project_name', 'project_notes', 'project_status', 'due_date'];
public function scopePersonal($query)
{
return $query->where('user_id', Auth::user()->id);
}
//
}
collaborator Model
<?php
namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;
class Collaboration extends Model
{
protected $table = 'project_collaborator';
/*
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['project_id', 'collaborator_id'];
/*
* Get the user that is a collaborator on another project
* #return collection
*/
public function user()
{
return $this->belongsTo(User::class, 'collaborator_id');
}
/*
* Query scope to return information about the current project
* #param $query
* #param int $id
* #return query
*/
public function scopeProject($query, $id)
{
return $query->where('project_id', $id);
}
public function scopeColabo($query)
{
return $query->where('collaborator_id',Auth::user()->id);
}
}
Based on your comment, you need to add a relationship hasOne to your collaboration model:
class Collaboration extends Model
{
.....
public function project()
{
return $this->hasOne('App\Project');
}
.....
}
The method project() will define your relationship with your project model. And then you'll be able to get the collaboration project name like this:
$collaborations = Collaboration::with('project')->get();
foreach ( $collaborations as $collaboration ) {
echo $collaboration->project->project_name;
}
You can read more in the documentation.
In your Collaboration class add the followoing relation:
public function project()
{
return $this->belongsTo('App\Project');
}
And in your User class define a relation as:
public function collaborations()
{
return $this->hasMany('App\Collaboration', 'collaborator_id');
}
Then you can get all the collaborations of logged in user by:
$collaborations = auth()->user()->collaborations()->with('project')->get()
or
To get all collaborations you can so as:
$collaborations = Collaboration::with('project')->get();
In your view file:
#foreach ($collaborations as $collaboration)
{{ $collaboration->project->project_name }}
#endforeach

Categories