Can i use eloquent ORM and Query Builder in one place (one model)? - laravel-5

I want using Eloquent ORM and Query Builder in one place (one model), Thanks
<?php
namespace App\Inventory_material;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class InventoryMaterialModel extends Model
{
// Eloquent ORM
protected $table = 'table_name';
// Query Builder
public function getter($field, $value) {
$query = DB::table('inventory_material')->where('id', $value)->value($field);
return $query;
}

You don't need the getter method if you're using Eloquent, it is all taken care of under the hood.
class InventoryMaterialModel extends Model
{
// Eloquent ORM
protected $table = 'table_name';
}
class InventoryController
{
public function show($id)
{
return App\Inventory_material\InventoryMaterialModel::findOrFail($id);
}
}

Related

insert an array of data into database with API using laravel 8

I'm new to laravel and just started creation of apis to connect to the database. I'm able to send a single entry at a time to the database, now what i want to do is to pass an array of data to be inserted into the database but don't seem to find my way around
Controller
public function newSaveUser(Request $request) {
$newFood = new Food;
$newFood->name = $request->name;
$newFood->save();
}
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Food extends Model {
//
public $timestamps = false;
}
Try this here:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Food extends Model
{
//
public $timestamps = false;
protected $fillable = ['name'];
}
and then to insert data:
Food::create([
'name' => $request->get('name')
]);
firstly check the db connection is works or not.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Food extends Model
{
//
public $timestamps = false;
protected $table = "write_table_name_here";
protected $guarded = [];
}
extend model in controller;
$newFood = new Food;
$newFood->name = $request->name;
$newFood->save();

Relationship Between Mongodb and Mysql databases

I am Trying to get data from mysql and mongodb in laravel framework.I am using HybridRelations to fetch data realated data but I stuck.please help me.
I got Error like that.
Call to undefined relationship [Country] on model [App\Models\State].
Here is my code.
country model;
namespace App;
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Country extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'countries';
protected $fillable = [ 'country' ];
public function country()
{
return $this->hasMany(State::class,'country_id');
}
}
my state model:
<?php
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Illuminate\Database\Eloquent\Model;
class State extends Model
{
use HybridRelations;
protected $connection = 'mysql';
protected $table = 'state';
protected $primaryKey = 'state_id';
protected $fillable = [ 'name','code','status' ];
public function state()
{
return $this->belongsTo(Country::class, '_id');
}
}

One to Many to One relationship in laravel eloquent

I have company, employee and motorCycle table.
One Company has many employee. One employee has One motorCycle
Company.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
protected $table = 'company';
protected $primaryKey = '_id';
public function employee()
{
return $this->hasMany(Employee::class);
}
}
?>
Employee.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
protected $table = 'employee';
protected $primaryKey = '_id';
public function motorCycle()
{
return $this->hasOne(MotorCycle::class, 'motorCycle', 'id');
}
}
?>
MotorCycle.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class MotorCycle extends Model
{
protected $table = 'motorCycle';
protected $primaryKey = 'id';
public function employee()
{
return $this->belongsTo('MotorCycle::class');
}
}
?>
I would like to fetch result in controller like below
public function show(Company $company)
{
return $company->employee()->offset(0)->limit(20)->motorCycle()->get();
}
I am trying to browse this URL http://127.0.0.1:8000/api/comapanys/1
My route is like below
Route::apiResource('comapanys', 'ComapanyController');
what kind of result do you want to display? you want to have a list of employees and his/her motorcycle in a certain company?
may you can return a query like this.
public function show(Company $company)
{
return $company->employee()->with('motorCycle')->offset(0)->limit(20)->get();
}
There is to many problem in your code.
First, for example you write class company extends Model but in controller you use Company $company. Also for class employee class employee extends Model but in motoCycle class return $this->belongsTo('App\Model\Employee');. Use naming convention like first letter uppercase for model name .
Second, im not sure but I dont think you can chain eloquent methods like this return $company->employee()->offset(0)->limit(20)->motorCycle()->get();. Offset and limit should be on the end of chain.
Also use (Employee::class) instead of ('App\Model\Employee')

BadMethodCallException in Macroable.php line 81: Method with does not exist. Laravel ORM

I'm new to laravel, currently using ORM I am trying to retrieve data from parentCategory table by using foreign key present in category table.
following is the code in my category model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class category extends Model
{
protected $table='category';
public function parentCategory(){
return $this->belongsTo('App\parentCategory','mCategoryId');
}
}
following is my parentCategory model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class parentCategory extends Model
{
protected $table='maincategory';
public function categories(){
return $this->hasMany('App\category');
}
}
following is the code of my categoryController by which i am using to retrieve data:
class categoryController extends Controller
{
function index(){
$category=category::all()->with('parentCategory');
$parentCategory=parentCategory::all();
return view('admin.category',['categories'=>$category,'parentCategories'=>$parentCategory]);
}
function add(Request $request){
$category= new category();
$category->categoryName=$request["name"];
$category->mCategoryId=$request["parentCategory"];
$category->save();
return redirect()->route('category');
}
}
the error further states:
at Collection->__call('with', array('parentCategory')) in categoryController.php line 17
In your index() function, you can try
$category = category::with('parentCategory')->get();
This should solve the problem. Follow the Eager Loading documentation.

Laravel Eloquent With() With()

How do I go about using multiple Eloquent With()'s?
PortalPlaylistElement Model
class PortalPlaylistElement extends Model
{
public $primaryKey = 'code';
public $incrementing = false;
public $timestamps = false;
public function AirtimePlaylists()
{
return $this->hasOne('App\AirtimePlaylist','id','playlist_id');
}
}
AirtimePlaylistContent Model
class AirtimePlaylistContent extends Model
{
protected $table = 'cc_playlistcontents';
}
AirtimePlaylistModel
class AirtimePlaylist extends Model
{
protected $table = 'cc_playlist';
public function PortalPlaylistElements()
{
return $this->belongsTo('App\PortalPlaylistElement','playlist_id');
}
public function AirtimePlaylistContents()
{
return $this->hasMany('App\AirtimePlaylistContent','playlist_id');
}
}
I have no problems with:
AirtimePlaylist::with('AirtimePlaylistContents')->get());
or
PortalPlaylistElement::with('AirtimePlaylists')->get();
But I'd like to get all AirtimePlaylistContents, in an AirtimePlaylist that belongs to a PortalPlaylistElement.
In essence, (Pseudo code)
PortalPlaylistElement::with('AirtimePlaylists')::with('AirtimePlaylistContents')->get();
You need Nested Eager Looading
PortalPlaylistElement::with('AirtimePlaylists.AirtimePlaylistContents')->get();
nested relations
with('relation1.relation2.relation3')->get(); // relation1 has relation2 relation2 has relation 3
not nested relations
with('relation1','relation2','relation3')->get(); // you model has all relations
I would like to add this if someone needed it
with(['relation1.relation2-1','relation1.relation2-2'])->get(); // relation1 has relation2-1 and relation1 also has relation2-2

Resources