Laravel5.1 query relationships(Eloquent ORM) - php

My model is Patient->Sample->Ready_Sample ,relationship all is oneToMany ,
My question is I query Ready_Sample need to know patient.name
Patient_Model
class Patient_Model extends Base_Model {
protected $table = 'patients';
public function samples(){
return $this->hasMany('App\Models\Sample_Model','patient_id','code');
}
}
Sample_Model
class Sample_Model extends Base_Model{
protected $table = 'samples';
public function patient(){
return $this->belongsTo('App\Models\Patient_Model','patient_id','code');
}
public function ready_samples(){
return $this->hasMany('App\Models\Ready_Sample_Model','sample_id','code');
}
}
Ready_Sample_Model
class Ready_Sample_Model extends Model{
protected $table = 'ready_samples';
public function sample(){
return $this->belongsTo('App\Models\Sample_Model','sample_id','code');
}
}
In Sample_Controller
class Sample_Controller extends Controller{
public function query(Request $request){
$result = Sample_Model::with(['patient']);
->orderBy("updated_at","desc")
->Paginate(15)
->toJson();
return $result;
}
In Sample I know to get patient.name ,but Ready_Sample how to get Patien.name?

You can get the patient.name using the below code:
$readySample = Ready_Sample_Model::first(); // fetch the first record
echo $readySample->sample->patient->name;
Hope it helps!

Related

How can I pass a parameter from the controller index function to the model's functions? LARAVEL

//------------------------------//
//below is the controller index function code //
//------------------------------//
public function index($from, $to,$id)
{
$data=Attendance::where('attendance_date','>=',$from)
->where('attendance_date','<=',$to)
->with('userAttendance')//--> **I need to pass the $id to this function in the model**
->with('admin')
->get()
//---------------------------------//
//below is the model code //
//----------------------------------//
class Attendance extends Model
{
protected $table = "attendances";
protected $fillable=[
'attendance_date',
'admin_id',
];
//---> **i need the $id passed to here**
public function userAttendance()
{
return $this->belongsToMany('App\User', 'user_attendances','attendance_id','user_id')
->withPivot(
'present_absent',
'excuse',
'attendance_key_amount',
'verified_date',
'comment',
);
}
}
just try this
Controller:
public function index()
{
$id = 50;
Product::getId(50);
}
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public static function getId($id){
dd($id);
}
}

How Pass variable from Controllers to Model

I want to pass $id from my Controllers to Model, please help me
this my Controllers
class OrderController extends Controller
{
public function show($id,Request $request) {
$data= order_list::select('*')
->where('t_order_list.id_order', $id)
->get();
}
}
how to pass $id to my model
class order_list extends Model
{
protected $table = "t_order_list";
protected $fillable = ['id_order','id_product','id_variant','qty_order'];
protected $with = ['variant_list'];
public function variant_list($id){
return $this->hasMany('App\Model\variant','id','id_variant')
->join('t_color','t_variant.id_pcolor','=','t_color.id')
->join('t_size','t_variant.id_psize','=','t_size.id')
->join('t_subcategory','t_variant.id_pcategory_sub','=','t_subcategory.id')
->join('t_order_list as d', 't_variant.id', '=', 'd.id_variant')
->select(['t_variant.*','t_color.code_pcolor','t_size.code_psize','t_color.hex_pcolor'
,'t_color.hex_pcolor_2','t_color.hex_pcolor_3',
't_subcategory.name_pcategory_sub','d.qty_order'])
->where('d.id_order', $id);
}
this my error
Call from Controller like OrderList::variant_list($id) and define function like
public static function variant_list($id){
return $this->hasMany('App\Model\variant','id','id_variant')
->join('t_color','t_variant.id_color','=','t_color.id')
->join('t_size','t_variant.id_size','=','t_size.id')
->join('t_subcategory','t_variant.id_subcategory','=','t_subcategory.id')
->join('t_order_list as d', 't_variant.id', '=', 'd.id_variant')
->select(['t_variant.*','t_color.code_color','t_size.code_size','t_color.hex_color'
,'t_color.hex_color_2','t_color.hex_color_3',
't_subcategory.name_subcategory','d.qty_order']);
}
You can pass simply the variable from controller to models.
For Normal method:-
Controller:-UserController
public function test(){
$id = "A";
$user = new User; //create object
$user->passValue($id);
}
Model:- User
public function passValue($id){
echo $id;
}
For Static method:-
public function test(){
$id = "A";
User::passValue($id);
}
Model:- User
public static function passValue($id){
echo $id;
}

laravel 4 hasOne/ hasMany / belongsTo doesn't work

I have some models belong to Activity Model.
in my Activity.php I had
<?php
class Activity extends \Eloquent {
protected $fillable = [];
public function activity_car_nums()
{
return $this->hasMany('ActivityCarNum');
}
public function newables()
{
return $this->hasMany('Newable');
}
public function serial_codes()
{
return $this->hasMany('SerialCode');
}
public function applys()
{
return $this->hasMany('Apply');
}
}
and in SerialCode.php, I had
<?php
class SerialCode extends \Eloquent {
protected $fillable = ['code'];
public function activity()
{
return $this->belongsTo('Activity');
}
}
and in my controller, when I wrote
$serial_codes = [];
while(count($serial_codes) < $serial_code_total)
{
$code = substr(md5(uniqid(rand(), true)),0,5);
$serial_code = new SerialCode(['code' => $code]);
if(!in_array($serial_code, $serial_codes))
{
$serial_codes[] = $serial_code;
}
}
$activity->serial_codes()->saveMany($serial_codes);
it works.
But when it turns to
//this can get activity object
$activity = Activity::find($id);
//this can get the serial codes of the object above.
$serial_codes = SerialCode::whereActivityId($id)->get();
//this don't work, it returns null
$serial_codes = $activity->serial_codes;
for I really don't know why...
Can anybody help me please, and sorry for my poor English. Thank You.
(If you need any code else please tell me.)
my model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
class Product extends Model
{
public $table="s_products";
protected $primaryKey = 'product_id';
public function reviews()
{
return $this->hasMany('App\ProductReview');
}
public function attributes()
{
return $this->hasMany('App\AttributeMapping');
}
public function images()
{
return $this->hasMany('App\Image');
}
}
I found the reason that why my model query cannot work, because the "_" in function name.
just change
public function serial_codes()
to
public function serialcodes()
then everything will go fine.
Thank to everybody.

Chain of hasMany or hasManyThrough through N models

How can I get all the submissions belongs to a particular user?
Trying this:
$user=User::find(1);
dd($user->submissions);
Throwing error:
Call to undefined method Illuminate\Database\Query\Builder::hasMany()
Will I have to loop through the models?
Here are the models:
class User extends Eloquent implements ConfideUserInterface, BillableInterface
{
public function categories()
{
return $this->hasMany('Category');
}
public function forms()
{
return $this->hasManyThrough('App\Models\Form', 'Category');
}
public function submissions()
{
return $this->hasMany('Category')->hasMany('Form')->hasMany('Submission');
}
}
class Category extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function forms()
{
return $this->hasMany('App\Models\Form');
}
public function submissions()
{
return $this->hasManyThrough('Submission', 'App\Models\Form', 'id', 'form_id');
}
}
namespace App\Models;
class Form extends \Eloquent {
public function user()
{
return $this->category->user();
}
public function category()
{
return $this->belongsTo('Category');
}
public function submissions()
{
return $this->hasMany('Submission');
}
}
class Submission extends \Eloquent {
public function user()
{
return $this->form->category->user();
}
public function category()
{
return $this->form->category();
}
public function form()
{
return $this->belongsTo('App\Models\Form');
}
}
That doesn't really work that way with chaining relations...
What you can do, is this:
$submissions = Submission::whereHas('form.category.user', function($q){
$q->where('id', 1);
})->get();
Note that whereHas with nested relationships has only been added in the latest Laravel 4 release. Make sure to composer update.
If i'm not getting your question wrongly, you need to define relationship correctly.
According to laravel 4.2 you can use below kind of code to define relations:
class User extends \Eloquent implements ConfideUserInterface, BillableInterface{
//...
public function submissions(){
return $this->hasMany('Submission');
}
}
//...
class Submission extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
//...
}
To further reading take a look at: http://laravel.com/docs/4.2/eloquent#relationships

Laravel model object chaining

For some reason, I cannot chain model objects. I'm trying to eager load 'Location' for an 'Order' and would prefer the logic to be contained in the models themselves. But past one chain, it does not work.
class Order extends Eloquent {
protected $table = 'orders';
public function customer() {
return $this->belongsTo('Customer');
public function location() {
return $this->customer()->location(); // this does not work
}
}
class Customer extends Eloquent {
protected $table = 'customers';
public function user() {
return $this->belongsTo('User');
}
public function orders() {
return $this->hasMany('Order');
}
public function location() {
return $this->user()->location();
// return $this->user(); // WORKS!!
}
}
class User extends Eloquent {
protected $table = 'users';
public function locations() {
return $this->hasMany('Location');
}
public function location() {
return $this->locations()->first();
}
}
I eventually want to do this:
class ChefController extends BaseController {
public function get_orders() {
$chef = $this->get_user_chef(); // this already works
return $chef->orders()->with('location')->get(); // does not work
}
}
Try to reference relation (user table) by adding user_id as second argument, like this:
public function user() {
return $this->belongsTo('User',"user_id");
}
Maybe you called that id field different, but you know what I mean.

Categories