I try to get my categories base on their status and i have scope below in my category model:
public function scopeOfStatus($query, $status)
{
return $query->where('status_id', $status);
}
And my controller is like:
public function finder() {
$findercategories = Category::OfStatus('Active')->get();
return response()->json($findercategories);
}
My route is like:
Route::get('/finder','frontend\SearchController#finder');
But i get blank page as result. Any idea?
update
if i use $findercategories = Category::ofStatus(1)->get(); i get the result that i want but it's static not dynamic :\
Get the Id's from the statuses table with respect to the searched value and use the Id's to get the Category
public function scopeOfStatus($query, $status)
{
$statuses = Status::where('title', $status)->pluck('id'); // Or relevant column name
return $query->whereIn('status_id', $statuses);
}
and in your Controller you could do so
public function finder() {
$findercategories = Category::ofStatus('Active')->get();
return response()->json($findercategories);
}
Related
How do you display all list of admin that have column with the is_admin=1? with this query? because we are not passing any data in showListAdmin.
public function Admin()
{
$users = User::all();
return view('admin.admins')->with('users',$users);
}
simply use where ....
public function showListAdmin()
{
$users = User::where('is_admin',true)->get()->all();
return view('admin.admins')->with('users',$users);
}
I am trying to retrieve the data on my wishlist table, for a particular user, so far it only retrieves the first data on the table, just returning one array instead of the three in the table with same user id
public function getWishlistByUserId($id){
$wishlists = Wishlist::where('userId', $id)->get();
foreach($wishlists as $wishlist){
$products = Product::where('id', $wishlist->productId)->get();
return $products;
}
}
It happens because the foreach loop returns a value during the first iteration. Place your return statement outside the loop. Also you could improve your performence by making use of relationships.
An example could be:
// Product.php
public function wishlists()
{
return $this->hasMany(Wishlist::class);
}
// Your method
public function getWishlistByUserId($id)
{
return Product::whereHas('wishlists', function ($query) use ($id) {
$query->where('userId', $id);
});
}
Ideally this is n+1 situation
So i will suggest to use laravel relationship like:
in your whishlist model
public function product(){
return $this->hasMany(Product::class,'productId','id');
}
get data with relationship
public function getWishlistByUserId($id){
$wishlists = Wishlist::with('product')->where('userId', $id)->get();
}
I was finally able to get it working this way, i just pushed the result into an array, and then returned it outside the loop, thanks everyone for your help
public function getWishlistByUserId($id){
$wishlists = Wishlist::where('userId', $id)->get();
$wishlist = [];
foreach($wishlists as $wish){
$product = Product::where('id', $wish->productId)->get();
array_push($wishlist, $product);
}
return $wishlist;
}
I'm have got a problem about showing data with multi relation table,
I just want show data from jadwal table with specified value from kelas.nama_kelas and sort it by jam.waktu_mulai,
for example : Showing data from jadwal where has nama_kelas=3C
this my model.php
public function dosen(){
return $this->hasMany('App\Dosen','id');
}
public function jurusan(){
return $this->hasMany('App\Jurusan','id');
}
public function ruang(){
return $this->hasMany('App\Ruang','id');
}
public function kelas(){
return $this->hasMany('App\Kelas','id');
}
public function makul(){
return $this->hasMany('App\Matakuliah','id');
}
public function hari(){
return $this->hasMany('App\Hari','id');
}
public function jam(){
return $this->hasMany('App\Jam','id');
}
This my controller.php
public function show($id)
{
$dec = $id;
$sort = \App\Kelas::find($dec);
$jadwals = Jadwal::join('dosen','dosen.nip','=','jadwal.nip')
->join('jurusan','jurusan.kode_jurusan','=','jadwal.kode_jurusan')
->join('ruang','ruang.kode_ruang','=','jadwal.kode_ruang')
->join('kelas','kelas.kode_kelas','=','jadwal.kode_kelas')
->join('mata_kuliah','mata_kuliah.kode_mk','=','jadwal.kode_mk')
->join('hari','hari.kode_hari','=','jadwal.kode_hari')
->join('jam','jam.kode_jam','=','jadwal.kode_jam')
->select('*','jadwal.id as jadwal_id','kelas.id as kelas_id')
->where('kelas.nama_kelas','LIKE','%'.$sort->nama_kelas.'%')
->orderBy('jam.waktu_mulai','Asc')
->get();
return view('Akademik.Jadwal.jadwalDetail',compact('jadwals'));
}
And this my database entiti reltion diagram :
sorry for my bad english
EDIT 1:
I have been test and do a research it works but when i try to print $jadwal->kelas_id it return nul...but not for $jadwal->jadwal_id,,,
before i post this I've searched a lot for an answer but no result please forgive me my English not that good
i have Laravel app that contains :
User Model
public function Follow()
{
return $this->belongsToMany(Page::class,"follows","user_id","page_id");
}
Page Model
public function Posts()
{
return $this->hasMany(Post::class);
}
Post Model
public function Page()
{
return $this->belongsTo(Page::class);
}
In the homeController I've index method that should return the posts of the user's followed pages
$user->follow()->get();
this returned only the Pages collection, I couldn't get the Posts or access any
I need to access the posts and their properties, also if possible i need to count the post's likes.
thank you very much.
I have found an answer after trillion attempts
public function index()
{
$user = Auth::User();
$posts = [];
foreach ($user->follow as $key => $page)
{
foreach ($page->posts as $k => $post)
{
array_push($posts, $post);
}
}
//return $posts;
return view('home', compact('posts'));
}
I'm not able to access the likes count yet but i'll try the with('') method
I'll Update for any further Info
You're using the function follow() that returns a page!! Check your return statement
should be:
public function Follow()
{
return $this->belongsToMany(Follow::class,"follows","user_id","page_id");
}
I am trying to get my head around Laravel's relationships but having difficulty with the last part of what I'm trying to achieve.
My tables are like so:
pagetemplates
id
pages
id
pagetemplate_id
pagetemplate_blocks
id
pagetemplate_id
pagetemplateblocks_content
id
page_id
page_template_block_id
So when loading a page out of the DB I need to get it with pagetemplates, with it's blocks and with the content.
Here's my code so far:
Page.php
public function pageTemplate()
{
return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
}
public function pagetemplateblockcontent()
{
return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')->withTimestamps();
}
public function pagecontent()
{
return $this->hasMany('PageTemplateBlockContent', 'page_id')->with('pagetemplateblock');
}
PageTemplate.php
public function page()
{
return $this->hasMany('Page', 'pagetemplate_id');
}
public function blocks() {
return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
}
PageTemplateBlock.php
public function pagetemplate() {
return $this->belongsTo('PageTemplate', 'pagetemplate_id');
}
public function blockcontent() {
return return $this->hasOne('PageTemplateBlockContent');
}
PageTemplateBlockContent.php
public function pagetemplateblock()
{
return $this->belongsTo('PageTemplateBlock', 'page_template_block_id');
}
However, the issue is with the content, if I try this it returns one instance of PageTemplateBlockContent, which is the same for all of the pages. Although there should be a different PageTemplateBlockContent for each Page. I'm not sure how to get around this, so any ideas would be greatly appreciated.
UPDATED
My Controller calls
$this->pageRepo->where('id', $id)->with('pageTemplate');
"pageTemplate" returns the following:
return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
"blocks" returns the following:
return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
"blockcontent" returns the following:
return $this->hasOne('PageTemplateBlockContent');
So this means it's never hitting the "pagetemplateblockcontent" which Joost suggested creating.
Change
public function pagetemplateblockcontent()
{
return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')->withTimestamps();
}
to
public function pagetemplateblockcontent()
{
return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblockscontent_page', 'page_id', 'page_template_block_id')->withTimestamps();
}
and create an table page_pagetemplateblockcontent with two primary keys like so.
$table->integer("page_id")->unsigned();
$table->integer("page_template_block_id")->unsigned();
$table->primary(['page_id', 'page_template_block_id']);