I have to table
This is classroom table
And this is joinclass table
Now i want to view classroom data which class_code match form joinclass table.
Here is my classroom model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class classroom extends Model
{
public function user()
{
return $this->belongsToMany('App\User');
}
}
And this is my join class model
<?php
namespace App\Eloquent;
use Illuminate\Database\Eloquent\Model;
class joinclass extends Model
{
public $timestamps= true;
protected $table='joinclass';
protected $guarded=['id'];
public function user()
{
return $this->belongsToMany('App\User');
}
}
So now what should to write in my controller?
Use following simple join query first, and then integrate it with your tables
$arr_tags = DB::table('tags')
->join('users','users.id','=','tags.user_id')
->select('tags.id','tags.status','tags.tag_name','users.first_name','users.last_name','tags.created_at','tags.updated_at')
->get();
Related
I have forums table, topics table and posts table. the relationship between the posts and topics to forums in one to many. I have implemented the relationship but when I try to access the topics count by doing {{count($forum->topics)}} I get this error: count(): Parameter must be an array or an object that implements Countable However, when I try accessing the posts count in the same way:{{count($forum->posts)}}, it works correctly. Please help
Forum model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Forum extends Model
{
use HasFactory;
protected $guarded = [];
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function topics()
{
return $this->hasMany('App\Models\Topic');
}
public function posts()
{
return $this->hasMany('App\Models\Post');
}
}
Topic model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Topic extends Model
{
use HasFactory;
public function forum()
{
return $this->belongsTo('App\Models\Forum');
}
}
Post Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
public function forum()
{
return $this->belongsTo('App\Models\Forum');
}
}
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);
}
}
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')
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.
I have three tables and i want to make multiple relationship between then
orders,order_details,book
I want to make a common relation between then to take values in a single array .I am using following Code
Order.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
public $timestamps = false;
protected $table='orders';
public function orderDetails(){
return $this->hasMany('App\OrderDetail','order_id');
}
}
OrderDetail.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderDetail extends Model
{
//
public $timestamps = false;
protected $table='order_details';
public function orders(){
return $this->belongsToMany('App\Order');
}
public function book(){
return $this->hasMany('App\Book');
}
}
and Book.php
public function orderDetail(){
return $this->hasManyThrough('App\OrderDetail','App\Book','id','item');
}
where item is foreign key and primary key in book table
in OrderController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Order,App\OrderDetail,App\Book,App\User;
use Auth;
use App\Http\Requests;
use Response;
use App\Http\Controllers\Controller;
class OrderController extends Controller
{
public function userOrders(){OrderDetail::find($orderId)->where('order_id',$orderId)->where('item','book.id')->get(); }