I have a simple code, and now I want to select data from 2 tables. Then I want to show in my view. Here's my code
model:
Tikpro.php
class Tikpro extends Model {
public $table = "TIKPRO";
public $primaryKey = "ID_TIKPRO";
public function permintaan() {
return $this->hasMany('Permintaan', 'TIKPRO_ID', 'ID_TIKPRO');
}
model:
Permintaan.php
class Permintaan extends Model {
public $table = "PERMINTAAN";
public $fillable = array(
'NOMOR_TICKET',
'TGL_PERMINTAAN',
'NAMA_REQUESTER',
'BARANG_PERMINTAAN',
'NO_FPBJ',
'TGL_INPUT_FPBJ',
'TARGET_SELESAI',
'KETERANGAN',
'TINDAK_LANJUT_AKHIR',
'STATUS',
'FPB',
'RFQ',
'DO',
'BAST',
'TGL_DEADLINE',
'titik_proses',
'TIKPRO_ID',
);
public $primaryKey = "ID_PERMINTAAN";
public function tikpro() {
return $this->belongsTo('Tikpro','TIKPRO_ID','ID_TIKPRO');
}
Controller:
PermintaanController.php
public function details($ID_PERMINTAAN) {
$query = DB::table('PERMINTAAN')->select('TIKPRO.NAMA_TIKPRO')->join('TIKPRO','TIKPRO.ID_TIKPRO','=','PERMINTAAN.TIKPRO_ID')->get('all');
$jebret = Permintaan::find($ID_PERMINTAAN);
return view('permintaan.details', compact('jebret'))->with($query);
}
And how can I show it? In my view I try {{ $jebret->$query }} . But still can't show the data.
How should I write code? Thanks
I think you can try this
public function details($ID_PERMINTAAN) {
$query = DB::table('PERMINTAAN')->select('TIKPRO.NAMA_TIKPRO')->join('TIKPRO','TIKPRO.ID_TIKPRO','=','PERMINTAAN.TIKPRO_ID')->where('PERMINTAAN.ID_PERMINTAAN',$ID_PERMINTAAN)->get();
return view('permintaan.details', compact('query'));
}
Hope this work for you
There's 2 ways here. The first and easiest (if you've set it up correctly) is the relationship. https://laravel.com/docs/5.4/eloquent-relationships. To use the one in your example you want to do this:
$permintaans = Permintaan::query()->get()->all();
return view('permintaan.details', [
'permintaans' => $permintaans,
]);
//In the view
#foreach($permintaans as $permintaan)
<tr>{{$permintaan->tikpro->attribute}}</tr>
#endforeach
Another way like you've attempted is the join. In the select though I would leave it blank so you get all columns!
$permintaans = Permintaan::query()
->join('TIKPRO','TIKPRO.ID_TIKPRO','=','PERMINTAAN.TIKPRO_ID')
->get()
->all();
Related
there are the following models:
class Page extends Model {
protected $with = ['blocks'];
public function blocks() {
return $this->belongsToMany('\App\Block');
}
}
class Block extends Model {
protected $with = ['fields'];
public function fields() {
return $this->hasMany('\App\Fields');
}
}
class Field extends Model {
protected $with = ['data'];
public function data() {
return $this->hasOne('\App\Data');
}
}
And there is a controller in which I get the data:
public function get() {
$page_id = 1;
$data = Page::find($page_id);
}
The result is the following structure:
Page
- Block 1
- field 1
- Some data
- field 2
- More data
- Block 2
- Field 1
- Other data
The block may belong to multiple pages.
The field belongs to the block.
But the data stored in this field belong to both the field and the page at the same time.
So, I need to pass the variable $page_id to the data() function of the Field model. To get something like this:
public function data() {
return $this->hasOne('\App\Data')->where('page_id', '=', $page_id);
}
Tell me, please, how to do this?
I solved this problem by removing $with from models and selecting the data with this code:
Page::with(['blocks.fields.data'=> function($query) use ($page_id){
$query->where('page_id', $page_id);
}])->find($page_id);
But it's not beautiful. I just want to write Page::find($page_id);
Thanks!
I like force loading relation but in your case maybe this what you looking for :
https://laravel.com/docs/master/eloquent-resources#conditional-relationships
you can do this as the following :
Page::whereHas('blocks.fields.data', function($query) use ($page_id){
$query->where('page_id', $page_id);
})->find($page_id);
I am new to Laravel. I am trying to use Eloquent Model to access data in DB.
I have tables that shares similarities such as table name.
So I want to use one Model to access several tables in DB like below but without luck.
Is there any way to set table name dynamically?
Any suggestion or advice would be appreciated. Thank you in advance.
Model:
class ProductLog extends Model
{
public $timestamps = false;
public function __construct($type = null) {
parent::__construct();
$this->setTable($type);
}
}
Controller:
public function index($type, $id) {
$productLog = new ProductLog($type);
$contents = $productLog::all();
return response($contents, 200);
}
Solution For those who suffer from same problem:
I was able to change table name by the way #Mahdi Younesi suggested.
And I was able to add where conditions by like below
$productLog = new ProductLog;
$productLog->setTable('LogEmail');
$logInstance = $productLog->where('origin_id', $carrier_id)
->where('origin_type', 2);
The following trait allows for passing on the table name during hydration.
trait BindsDynamically
{
protected $connection = null;
protected $table = null;
public function bind(string $connection, string $table)
{
$this->setConnection($connection);
$this->setTable($table);
}
public function newInstance($attributes = [], $exists = false)
{
// Overridden in order to allow for late table binding.
$model = parent::newInstance($attributes, $exists);
$model->setTable($this->table);
return $model;
}
}
Here is how to use it:
class ProductLog extends Model
{
use BindsDynamically;
}
Call the method on instance like this:
public function index()
{
$productLog = new ProductLog;
$productLog->setTable('anotherTableName');
$productLog->get(); // select * from anotherTableName
$productLog->myTestProp = 'test';
$productLog->save(); // now saves into anotherTableName
}
I created a package for this: Laravel Dynamic Model
Feel free to use it:
https://github.com/laracraft-tech/laravel-dynamic-model
This basically allows you to do something like this:
$foo = App::make(DynamicModel::class, ['table_name' => 'foo']);
$foo->create([
'col1' => 'asdf',
'col2' => 123
]);
$faz = App::make(DynamicModel::class, ['table_name' => 'faz']);
$faz->create([...]);
I want to get the data from pdf_Files table where it will only show the name of files based on it user_id. I don't really know how to explain it but maybe if you see my codes, you would understand. I have already finish making the relationship between this 2 tables already
I want it to be something like:
DB::table('pdf_files')->where(table:personal_infos->id = pdf_files->user_id)->get(); --> I want it to be something like that but I do not know how to get it
Here is my code:
Controller:
public function downfunc(Request $request){
$downloads=DB::table('pdf_files')->get();
return view('download.viewfile',compact('downloads'));
}
pdfFile model:
protected $fillable = array('file_name','file_size','user_id');
public function personal_infos() {
return $this->belongsTo('App\personal_info', 'user_id', 'id');
}
personal_info model:
class personal_info extends Eloquent
{
protected $fillable = array('Email', 'Name');
protected $table = 'personal_infos';
protected $primaryKey = 'id';
public function pdfFiles() {
return $this->hasMany('App\pdfFile','user_id');
}
}
Are you going to search multiple user ids at once or are you getting the id from the request?
If you are getting the info from the request, you would want to do something like this:
use App\pdfFile; //Put this above the class
pdfFile::where('user_id', $request->user_id)->get(); //If you are checking to see if something equals something else in eloquent, you do not have to use equals
May be is something very easy but as I'm really new in Laravel and MVC frameworks can't figured it out.
Currently the source which I have showing products on the home page. Now I want to put WHERE clause in the query ... but I can't find the query.. What I have in HomeController.php is
public function index()
{
$products = Product::paginate(15);
return View::make('site.index', [
'products' => $products
]);
}
In the models/Product.php this
class Product extends Eloquent {
protected $table = 'products';
protected $primaryKey = 'product_id';
public $timestamps = false;
}
Where exactly I should put WHERE cat_id = 1...
You need to write,
$products = Product::where('cat_id','=','1')->paginate(15);
Please refer this link also.
Hope it will help you :-)
I'm trying to make a mini CMS-type component and I have three models:
Page.php
class Page extends Eloquent {
protected $table = 'pages';
public function content() {
return $this->hasManyThrough('Content', 'PageGroup');
}
public function groups() {
return $this->hasMany('PageGroup');
}
}
PageGroup.php
class PageGroup extends Eloquent {
protected $table = 'page_groups'
public function group() {
return $this->hasMany("Content");
}
}
Content.php
class Content extends Eloquent {
protected $table = 'content';
public function content() {
return $this->hasMany("Content");
}
}
Edit:
I've set up my relationship models with the correct relationships.
$page = Page::find(1);
$page_group = $page->groups()->where('id', '=', 1)->get();
Returns the PageGroup collection belonging to the Page. I guess my new question is if it's possible to get a collection from that collection... Something like:
$content = $page_group->content;
Because that returns:
Undefined property: Illuminate\Database\Eloquent\Collection::$content
Does this make any sense? I apologize for the newbie question; I'm just getting into Laravel!
I feel like an idiot.
The method chain $page->groups()->where("id", "=", 1)->get()->content; will not work because $page->groups()->where("id", "=", 1); returns a list of rows!
$page->groups()->where("id", "=", 1)->first()->content will work.