I'm a beginner in Laravel
public function allorders($param1){
$customerss=Customer::where('mobile_number',$param1)->first();
$customer_id1=$customerss->id;
$orderss= Order::where('customer_id',$customer_id1);
return view('admin.allorders')->with('orderss', $orderss);
}
and i have the view admin.allorders
#foreach ($orderss as $tag)
<span class="label label-primary">{{ $tag['customer_id']}}</span>
#endforeach
I'm sure the $orderss is having data but it's not shown in the view.
You need to add get() to execute the query:
$orderss = Order::where('customer_id', $customer_id1)->get();
Also, you could use relationships instead of this:
$customerss=Customer::where('mobile_number',$param1)->first();
$customer_id1=$customerss->id;
$orderss= Order::where('customer_id',$customer_id1);
You could do the same with just one query:
$orderss = Order::whereHas('customer', function($q) use($param1) {
$q->where('mobile_number', $param1);
})->get();
To make it work, define this relationship in the Order model:
public function customer()
{
return $this->belongsTo(Customer::class);
}
Related
When I am trying to query with relation in following way, I am getting error Trying to get property 'name' of non-object
$billing = ServiceProviderBilling::with(['user' => function ($query) {
$query->where('parent_id', auth()->id());
}])->get();
There is no error if I use
$billing = ServiceProviderBilling::with(['user'])->get();
In my view
#foreach($billing as $bill)
{{ $bill->user->name }}
#endforeach
Relations
public function serviceProviderBillings()
{
return $this->hasMany(ServiceProviderBilling::class);
}
and
public function user()
{
return $this->belongsTo(User::class);
}
How can I solve it?
It means $bill->user return null. some bill has no user, If you want show the bills without user, you can use whereHas method:
ServiceProviderBilling::with('user')
->whereHas('user', function (Builder $query) {
$query->where('parent_id', auth()->id());
})->get()
I'm creating a functionality which allows the user to add product in wishlist, but I'm getting an error Trying to get property of non-object when I click the (whishlist blade), the error comes from this line <h4>USD {{$wishlist->product->price }}</h4> if I remove the $product it displays no price how do i fix this?
Wishlist Controller
public function index()
{
$user = Auth::user();
$wishlists = Wishlist::where("user_id", "=", $user->id)->orderby('id', 'desc')->paginate(10);
return view('wishlist', compact('user', 'wishlists'));
}
Blade
#if (Auth::user()->wishlist->count() )
#foreach($wishlists as $wishlist)
<h2>USD {{$wishlist->product->price }}</h2>
<h4>USD {{$wishlist->product->name }}</h4>
#endforeach
#endif
Wishlist.php
class Wishlist extends Model
{
protected $table = "wishlist";
protected $fillable=['product_id','user_id'];
public function user(){
return $this->belongsTo(User::class);
}
public function product(){
return $this->belongsTo(Product::class);
}
}
User.php
public function wishlist(){
return $this->hasMany(Wishlist::class);
}
Product.php
public function wishlist(){
return $this->hasMany(Wishlist::class);
}
You should first change how you check the count of wishlist, since it runs a heavy query that recovers all wishlists then count them. And also remove the $ in $product as #lucasArbex suggested
#if ($wishlists->count() )
#foreach($wishlists as $wishlist)
<h2>USD {{$wishlist->product->price }}</h2>
<h4>USD {{$wishlist->product->name }}</h4>
#endforeach
#endif
Also change your controller and use the relation on your user
public function index()
{
$user = Auth::user();
$wishlists = $user->wishlist()->with('product')
->orderby('id', 'desc')
->paginate(10);
return view('wishlist', compact('user', 'wishlists'));
}
Firstly, you should access the product relation like so (removing $):
$wishlist->product->price
Secondly, you should eager load the wishlist's product using the ::with() query builder:
public function index()
{
$user = Auth::user();
$wishlists = Wishlist::with('product')
->where('user_id', $user->id)
->orderby('id', 'desc')
->paginate(10);
return view('wishlist', compact('user', 'wishlists'));
}
Also, if I am correct, your product relation is wrong.
Your wishlist should have many products (rather than the other way around).
In your frontend, you will need to loop through all of the wishlist's products:
#foreach($wishlist->products as $product)
{{ $product->price }}
#endforeach
Change the relation in your Wishlist class to hasMany:
public function products()
{
return $this->hasMany(Product::class);
}
I have a blog and want to include the Users Name when shown to the public.
When creating the blog I make sure to include the user_id in the blogs table
In my Blog model I have the following:
public function users()
{
return $this->belongsTo(User::class);
}
In my Users model I have:
public function blogs()
{
return $this->hasMany(Blog::class);
}
In my Blog Controller I have:
public function index(User $user)
{
$users = User::get();
$blogs= DB::table('blogs')->where('user_id', '=', $users->id)->orderBy('id', 'DESC')->paginate(6);
return view('blogs.index',compact('blogs'));
}
Then in my view:
#foreach($blogs as $blog)
<h1>{{$blog->title}}</h1>
Source:{{$blog->users->first_name}} // This does not work
Source:{{$blog->first_name}} // This does not work either
#endforeach
I thought I could do something like this to show the names:
{{ $blogs->users->first_name }} {{ $blogs->users->last_name }}
But this isn't working either...
Try this:
#foreach($blogs as $blog)
<h1>{{$blog->title}}</h1>
{{$blog->user->first_name}}
#endforeach
And on your Blog Model
public function user()
{
return $this->belongsTo(User::class);
}
In your Blog controller the variable $blog needs to be $blogs. You also have extra characters (right parenthesis) in your Blade. It should be:
#foreach($blogs as $blog)
Source: {{ $blog->user->first_name }}
...
#endforeach
Blog Model
This function replaces the old "users" function, as only one user is returned (belongsTo is a singular relationship).
class Blog extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
User Model
public function blogs()
{
return $this->hasMany('App\Blog');
}
Controller Function
And, as such, you can cut down your controller code, including removing the redundant elements.
public function index(User $user)
{
$blogs = Blog::where('user_id', '=', $user->id)->orderBy('created_at','desc')->paginate(6);
return view('blogs.index', compact('blogs'));
}
The way you did is called Query Builder
$blogs= DB::table('blogs')->where('user_id', '=', $users->id)->orderBy('id', 'DESC')->paginate(6);
Query Builder does not support lazy loading, cause lazy loading is only supported for the Eloquent method
$blog->users->first_name
For Eloquent way you can try this instead:
$blogs = Blog::where('user_id', $user->id)->get()
foreach($blogs as $blog){
dd($blog->user); // you will get the user detail here
}
For lazy loading have a performance issue when come to load heavy data so to prevent lazy loading can use this
$blogs = Blog::with('user')->where('user_id', $user->id)->get()
For more information can look at Eloquent Relationship Documentation
For query builder, the only way to link your user is use join, which will be something like this
$blogs = DB::table('blogs')
->join('users', 'users.id', '=', 'blogs.user_id')
->get();
foreach($blogs as $blog){
dd($blog->first_name) // user first name
}
For more information can look at Query Builder Join
BlogController.php
public function index(){
$blogs = Blog::with('user')->get();
return view('blogs.index')->with('blogs',$blogs);
}
Blog.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function blogs()
{
return $this->hasMany('App\Blog');
}
I want to sort/order my data by its release_date but I get an error I don't understand:
In my Controller I have:
public function index() {
$releases = Release::orderBy('release_date', 'desc');
return view('pages.index')->with('releases', $releases);
}
and in my view file I have this:
#if (count($releases) > 0)
#foreach ($releases as $release)
<div>
<p>{{$release->artist}} - {{$release->album_title}} <span>ReleaseDate: {{$release->release_date}}</span></p>
</div>
#endforeach
#endif
This returns the error:
count(): Parameter must be an array or an object that implements
Countable
What does this mean? Can someone help me out?
Try this code:
public function index()
{
$releases = Release::orderBy('release_date', 'desc')->get();
return view('pages.index')->with('releases', $releases);
}
The reason you code wasn't working before is because orderBy won't perform the query, it will simply add to it. In this case you need to chain on get() at the end in order to perform the query and get the results otherwise you will just get an instance of the query builder.
Change Controller
public function index() {
$releases = Release::orderBy('release_date', 'desc')->get();
return view('pages.index',compact('releases');
}
change View
#if ($releases->count() > 0)
#foreach ($releases as $release)
<div>
<p>{{$release->artist}} - {{$release->album_title}} <span>ReleaseDate: {{$release->release_date}}</span></p>
</div>
#endforeach
#endif
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