I have 'Menus' table and this website is for a restaurant. When I add a new record through Laravel Backpack Crud, it gives me an error
BadMethodCallException Call to undefined method
App\Models\Menu::menu()
however, it adds the record to DB. When I want to update some record, it gives me this error but does not update the DB. Where can be the problem?
Here is my model:
<?php
namespace App\Models;
use Backpack\CRUD\CrudTrait;
use Illuminate\Database\Eloquent\Model;
class Menu extends Model
{
use CrudTrait;
protected $table = 'menus';
protected $fillable = ['image', 'name', 'description', 'price', 'category_id', 'popular'];
public function category()
{
return $this->belongsTo(Category::class);
}
Category.php:
<?php
namespace App\Models;
use Backpack\CRUD\CrudTrait;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use CrudTrait;
protected $table = 'categories';
public function menu()
{
return $this->hasMany(Menu::class);
}
MenuCrudController:
/*
|--------------------------------------------------------------------------
| CrudPanel Basic Information
|--------------------------------------------------------------------------
*/
$this->crud->setModel('App\Models\Menu');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/menu');
$this->crud->setEntityNameStrings('menu', 'menus');
Related
there is a (card model) bleongsToMany contacts, and there is a (contact model) is bleongsToMany cards, I want when (card) get updated his children in the middle table in the relation became removed.
I'm trying to do this by using (the observer), I did all steps correctly but the issue is:
when (card model) updated his children still exist in the middle table of the relationship, I want to remove them when any update on the card.
Route:
Route::get('/test',function(){
$card = \App\Models\Card::find(1);
$card-> update([
'name' => 'firstCard1',
'user_name' =>1,
]);
return true;
});
Observer:
<?php
namespace App\Observers;
use App\Models\Card;
class CardObserver
{
public function updated(Card $card)
{
$card -> contact()-> delete();
}
}
Card Model:
<?php
namespace App\Models;
use App\Observers\CardObserver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
use HasFactory;
protected $fillable = [
'name',
'user_id ',
];
public $timestamps = true;
protected $hidden = ['pivot'];
public function contact()
{
return $this->belongsToMany(Contact::class, 'card_contacts', 'card_id');
}
public function connection()
{
return $this->belongsTo(Connection::class, '');
}
protected static function boot()
{
parent::boot();
Card::observe(CardObserver::class);
}
}
Contact Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
use HasFactory;
protected $fillable = [
'provider_id',
'user_id',
'contact_string'
];
public $timestamps = true;
protected $hidden = ['pivot'];
public function card(){
return $this->belongsToMany(Card::class,'card_contacts','contact_id');
}
public function user(){
return $this->belongsTo(User::class,'user_id');
}
public function provider(){
return $this->belongsTo(Provider::class,'provider_id');
}
}
Card_contact Model (the pivot table):
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CardContact extends Model
{
use HasFactory;
protected $fillable = [
'card_id',
'contact_id',
];
public $timestamps = true;
}
any help please?
I have 5 tables
Products
-- Categories
--- SubCategories
---- Marks
----- Suppliers
I want to know how to make relationship
I have Product Model structure with :
namespace App\Models\Products;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [.... ];
public function categorie(){
return $this->belongTo(categories::class);
}
public function sub_categorie(){
return $this->belongTo(sub_categorie::class);
}
public function marks(){
return $this->belongTo(marks::class);
}
public function suppliers(){
return $this->belongTo(suppliers::class);
}
}
i want to know if it's correct ?
Another Question how would be looks other Model relationships such Suppliers, Categories, SubCategories ...
It depends on the cardinality and the foreign_keys that are involved.
Products
-- Categories
--- SubCategories
---- Marks
----- Suppliers
The context that you gave me, I'm guessing that each table depends on the previous one as showed in the context that you gave. If so, then you can do the following:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Suppliers extends Model
{
protected $fillable = [.... ];
public function mark()
{
return $this->belongsTo(Mark::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Marks extends Model
{
protected $fillable = [.... ];
public function subCategorie()
{
return $this->belongsTo(SubCategorie::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SubCategory extends Model
{
protected $fillable = [.... ];
public function categorie()
{
return $this->belongsTo(Categorie::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Categorie extends Model
{
protected $fillable = [.... ];
public function categorie()
{
return $this->belongsTo(Product::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [.... ];
}
For more in depht inormation, you can consult the section about relationshipts on laravel docs: https://laravel.com/docs/8.x/eloquent-relationships.
Please use the correct nomeclature of relationship's, instead of "categorie" write "category" in the function name, with that laravel will automaticaly recognise the foreign key.
Example:
public function category(){
return $this->belongTo(categories::class);
}
public function sub_category(){
return $this->belongTo(SubCategory::class);
}
public function mark(){
return $this->belongTo(Mark::class);
}
public function supplier(){
return $this->belongTo(Supplier::class);
}
Show me your models folder please if you have any problem .
My query is like this :
<?php
public function getListReviews()
{
$reviews = Review::where('user_id', auth()->user()->id)
->get();
return $reviews;
}
From the query, it can get all review data by id
I want get user photo, store photo and product photo
I want get it use Eloquent: Relationships
How can I get it with Eloquent: Relationships?
My review model is like this :
<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Review extends Eloquent
{
use HybridRelations;
protected $connection = 'mongodb';
protected $fillable = ['user_id', 'user_name', 'product_id', 'product_name', 'store_id', 'store_name', 'invoice_number', 'rating', 'comments', 'created_at', 'updated_at'];
public function user()
{
return $this->belongsTo(User::class);
}
}
My user model is like this :
<?php
namespace App;
...
class User extends Authenticatable
{
...
protected $fillable = ['name', 'email', 'password', 'birth_date', 'mobile_number', 'photo'];
public function store()
{
return $this->hasOne(Store::class);
}
}
My store model is like this :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Store extends Model
{
protected $fillable = ['user_id', 'name', 'address', 'phones', 'total_product', 'photo'];
public function products()
{
return $this->hasMany(Product::class);
}
}
My product model is like this :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['store_id','category_id','name', 'photo','description'];
public function store()
{
return $this->belongsTo(Store::class);
}
}
If you want to find out product and store from review model then add two more methods to Review model as below.
Edit App\Review.php
//You already have key to to find store i.e. store_id
public function store()
{
return $this->belongsTo(Store::class);
}
//You already have key to to find product i.e. product_id
public function product()
{
return $this->belongsTo(Product::class);
}
then execute your query as below
$reviews = Review::where('user_id', auth()->user()->id)
->with('store')
->with('product')
->with('user')
->get();
and you can access them as below,
Iterate through $reviews object as $review
$review->store->photo;
$review->product->photo;
$review->user->photo;
I'm trying to build my first Laravel application by following a few guides on the internet and I'm feeling I'm missing something obvious. Here is the code.
Error
BadMethodCallException in Builder.php line 2450: Call to undefined
method Illuminate\Database\Query\Builder::addresses()
User-Model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sentinel;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'email', 'password',
];
protected $hidden = [
'password',
'remember_token'
];
public function addresses()
{
return $this->hasMany('App\CustomerAddress');
}
}
CustomerAddress-model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CustomerAddress extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
CustomerAddress-controller
<?php
namespace App\Http\Controllers;
use App\CustomerAddress;
use Illuminate\Http\Request;
class CustomerAddressController extends Controller
{
public function create(Request $request)
{
$address = new CustomerAddress();
$address->address = $request['address'];
$request->user()->addresses()->save($address);
}
}
Error appears after this piece of code:
$request->user()->addresses()->save($address);
Any ideas? Thanks and cheers
In .config/cartalyst.sentinel.php, change 'model' => 'Cartalyst\Sentinel\Users\EloquentUser' to 'model' => 'App\User' to use your user model with the addresses relation defined
In ./app/User change User extends Authenticatable to User extends \Cartalyst\Sentinel\Users\EloquentUser to extend sentinel's user to your app's User model
Finally, your controller code should now be
`$address = new CustomerAddress();
$address->address = $request->input('address');
$request->user()->addresses()->save($address);`
and everything should be peachy
I have two tables
products and users
Both of this objects has images associated with it in a table
images
The schema for the images table is
id | image_id | resource_id | flag
1 | 567575 | 1 | user
2 | 423423 | 1 | product
Based on this flag i am identifying whether its a users image or whether its a products image.
If I need to eager load a users image how do it do it?
User model
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'users';
protected $primaryKey = 'users_id';
public function images()
{
return $this->hasMany('App\Entities\Image','resource_id');
}
}
Product model
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'products';
protected $primaryKey = 'products_id';
public function images()
{
return $this->hasMany('App\Entities\Image','resource_id');
}
}
images model
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class Image extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'images';
protected $primaryKey = 'images_id';
public function products()
{
return $this->hasOne('App\Entities\Product','products_id');
}
public function users()
{
return $this->hasOne('App\Entities\User','users_id');
}
}
Is there a way I can pass a flag in the relationship function of images() so that it will fetch the record based on the flags?
Please help out.
You can try this adding a conditional inside your images() method:
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'users';
protected $primaryKey = 'users_id';
public function images($filtered=false)
{
if ($filtered) {
return $this->hasMany('App\Entities\Image','resource_id')->where('flag','user');
}
return $this->hasMany('App\Entities\Image','resource_id');
}
}
and try the same logic to your Product model
Change your Image model to:
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class Image extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'images';
protected $primaryKey = 'images_id';
public function product()
{
return $this->belongsTo('App\Entities\Product', 'products_id', 'resource_id');
}
public function user()
{
return $this->belongsTo('App\Entities\User', 'users_id', 'resource_id');
}
}
try with this way :
$user_id = 1;
$my_image= User::find($user_id)->images()->where('flag', '=', 'user')->first();