Comparing to tables in Laravel. I have this code in my class (model):
public function table1() {
return $this->hasOne(table1Class::class, 'type', 'bms_id');
}
public function table2() {
return $this->belongsTo(table2Class::class, 'bms_id', 'type');
}
The controller, has something like this:
$type =
DB::table('table2')
->join('table1', 'table1.col_id','table1.col_name')
->select('table2.*', 'table1.*')
->get();
$dataMerge = $dataMerge->merge($type);
In the view, I have:
#foreach($data as $item)
{{$item->type==120}} //this works, no problem
{{$item->col_name}} //this produces nothing!
#endforeach
As you can see, the code stops working when referencing the col_name, any suggestions?
Thanks in advance!
Related
I actually managed to get data from HasManyThrough relations, but when I want to reach the relational data, it throws the error:
Property [publishings] does not exist on this collection instance.
(View: C:\Xampp\htdocs\wave\resources\views\pages\category.blade.php)
As can be seen in the picture down below, I received "publishings" data.
Where I got the error from is this Blade file:
category.blade.php
#foreach ($data->publishings as $item)
<div></div>
#endforeach
Since I have the data, the codes down below can be unnecessary for the solution, but they are there, just in case.
CategoryController.php
public function index($category)
{
$data = Category::where('slug', $category)
->with(['publishings' => function ($query) {
$query->where('slug', '!=', 'placeholder')->latest()->paginate(10);
}])->get();
return view('pages.category')->with('data', $data);
}
Category.php
public function assets()
{
return $this->hasMany('App\Models\Asset');
}
public function publishings()
{
return $this->hasManyThrough('App\Models\Publishing', 'App\Models\Asset');
}
Asset.php
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function publishings()
{
return $this->hasMany('App\Models\Publishing');
}
Publishing.php
public function asset()
{
return $this->belongsTo('App\Models\Asset');
}
Thanks for your help.
You are passing an Eloquent Collection to the view. If you want to query only the first result, you can use the first() method of the Query Builder instead of get():
public function index($category)
{
$data = Category::where('slug', $category)
->with(['publishings' => function ($query) {
$query->where('slug', '!=', 'placeholder')->latest()->paginate(10);
}])->first();
return view('pages.category')->with('data', $data);
}
If anyone has the same problem and comes across this message, the solution, in my case was actually quite easy. The 3rd line in the picture says:
0 => App\Models\Category
And I was trying to reach the relational data as if the $data is an object. That's why
$data->publishings
thrown the error. Solution is
$data[0]->publishings
Or return only that data from the controller:
return view('pages.category')->with('data', $data[0]);
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 have set up a many to many relationship in Laravel and have the database table populated with data. The relationship setup looks like this...
users.php
---------
public function houses()
{
return $this->belongsToMany('App\House')
->withTimestamps();
}
house.php
---------
public function users()
{
return $this->belongsToMany('App\User')
->withTimestamps();
}
In my /house/show.blade.php I am trying to display the saved connections like this...
$houses = House::with('App\User')->all();
foreach ($houses as $house) {
echo 'Found House';
}
It is giving me an error saying that $houses can not be found. Where am I going wrong?
You should indicate the relationship in the with method like this :
$houses = House::with('users')->get();
And one more thing it's better to get houses in the controller and pass them to the view :
$houses = House::with('users')->get();
return view('someView')->withHouses($houses);
And in the view do it like this :
#foreach ($houses as $house)
{{ $house->addres }}
#endforeach
To get only the houses taht has the users try this :
$houses = House::has('users')->get();
And to add some conditions on the users you can do it like this :
$houses = House::whereHas('users', function ($query) {
$query->where('name', 'some user name'); // to add some conditions on the user :)
})->get();
You should try this:
$houses = House::with('users')->get();
foreach ($houses as $house) {
echo 'Found House';
}
OR
In controller:
use House;
$houses = House::with('users')->get();
return view('someView',compact('houses'));
In Blade file:
#foreach ($houses as $house)
{{ $house->name }}
#endforeach
I have trouble with showing related variable in laravel blade
public function GetAll()
{
$news=DB::table('news')->get();
return View('index',['news'=>$news]);
}
In View:
#foreach($news as $new)
...
{{$new->comments()->count()}} Comments
...
#endforeach
Its also doesnt work for any variables of object but working good for first item:
public function Count()
{
$news=News::find(1);
echo $news->comments()->count();
}
public function GetAll()
{
$news = News::with('comments')->get();
return View('index',['news'=>$news]);
}
Use ORM instead of DB.
I am using laravel 5.1 for my project.
My issue was to dispaly the data as per category_id which foreign key of table category.
This code only get DATA having category_id 1 and this loop only run at $i=1 and after that they can not be iterate.
Please help me to solve out this issue.
My controller code was:-
public function category()
{
$category2=Category::all();
for($i=1;$i<=count($category2);$i++)
{
$category=HelpCenter::where('category_id','=',$i)->get();
return view('folder/category',compact('category'));
}
}
My view code was:-
#foreach($category as $category)
<li> {{$category->questions}}</li>
#endforeach
You cannot use the same variable for iterating as the array you are iterating ($category as $category). You MUST be getting some kind of error because of that.
That being said, you cannot return multiple views with a controller. You will just return the first result that you get.
What you should be doing is something like this:
public function category()
{
$category2=Category::all();
$categories = [];
for($i=1;$i<=count($category2);$i++)
{
$categories[]=HelpCenter::where('category_id','=',$i)->get();
}
return view('folder/category',compact('categories'));
}
And then in your view:
#foreach($categories as $category)
<li> {{$category->questions}}</li>
#endforeach
You are returning from the loop when it runs for one category. Just place your return line after closing loop braces. Like this..
public function category()
{
$category2=Category::all();
for($i=1;$i<=count($category2);$i++)
{
$category[]=HelpCenter::where('category_id','=',$i)->get();
}
return view('folder/category',compact('category'));
}
Hope this will solve your problem.
It is good to use relationship between Category and HelpCenter . For more info Check Here
But if you want to do this way then I think this is better way :
public function category()
{
$category2=Category::all();
$category = [];
foreach($category2 as $cat ){
$category[] = HelpCenter::where('category_id','=',$cat->id)->get();
}
return view('folder/category',compact('category'));
}