Fetch pivot table data in laravel 4.2 - laravel-4

I have created a pivot table post_tag and inserting data into it using sync method.
Now I want to fetch respective tags of a post to show in the view like following
{{ $post->tags }}
How can I do this? Thanks in advance.

You have to convert your object to array. In laravel, you can use like this.
{{ $post->tags->toArray() }}
For more detail in documentation.
Edit
In your Post model.
public function tags()
{
return $this->hasMany('Tag'); // One to Many
return $this->belongsToMany('Tag'); // Many to many
}
You can get like this.
$tags = Post::find($postId)->tags;
Or
$post = Post::find($postId);
$tags = $post->tags;
Hope it will be work for you.

Related

Variable inside laravel query builder is always null

Im trying to find the users that has commented the product this way:
public function getUserToRate(Product $product)
{
$userIds = DB::table('product_ratings')->where('product_id', $product->id)->pluck('user_id');
return $userIds;
}
But the thing is that $product->id is null. This is what comes out if I do:
DB::table('product_ratings')->where('product_id', $product->id)->toSql():
select * from `product_ratings` where `product_id` = ?
Why is my $product->id null? I have checked and the model exists, but I cant seem to use it inside the query builder.
Can anyone lend me a hand? Thanks
I don't think you need a product model. So ignore Route Model binding like Product $product. Instead you can use your $oroductId. And query the UserIds like this:
public function getUserToRate( $productId)
{
$userIds = ProductRating::where('product_id', $productId)->pluck('user_id');
return $userIds;
}

I am trying to return users and items datas from invoice

this is my function in Invoice Controller
public function edit(invoice $invoice, $id)
{
$invoice = Invoice::with('users', 'items.products')->findOrFail($id);
return view(compact('invoice'));
}
in my View I did
{{ $invoice->user_id->email }}
The relationship you are loading is named users so that is the name of the dynamic property for that relationship. user_id is the User's id, not the relationship.
{{ $invoice->users->email }}
This is also assuming there is always a User for the invoice.
Laravel 6.x Docs - Eloquent - Relationships - Relationship Methods vs Dynamic Properties
Side Note:
The naming could use some work since there should only be one user for an invoice I would assume, so singular. Use plural when something can have many, singular when it has one or none. The relationship should be named user.
You are using the wrong property, $invoice->user_id is probably is the raw integer value coming from the database. You need to call the relation like so:
{{ $invoice->user->email }}
$invoice->user will return the user object that is related to the $invoice->user_id id.
you forgot view name to return
public function edit(invoice $invoice, $id)
{
$invoice = Invoice::with('users', 'items.products')->findOrFail($id);
return view('invoice.edit',compact('invoice'));
}
and access with relationship name
{{ $invoice->users->email }}

Laravel Relationships - Select rows with same column value in blade

Let's say I have a post with many comments and I properly defined $post->comments relation in my post model. Comments have a column named confirmed with the value of 0 or 1. How can I select confirmed (rows which have confirmed value of 1) rows in my blade template?
This can help you
In your Post model
public function comments()
{
return $this->hasMany(Comment:class);
}
public function confirmedComments()
{
return $this->comments()->whereConfirmed(1);
}
And from your controller
$comments = $post->confirmedComments;
If in blade, you want select confirmed comments, it's so easy to do it with
#if($comment->confirmed)
//confirmed comment
#else
//
#endif
Hope it'll be helpful !
There are many ways to do this.
If you already have the comments eager loaded, then you can use the where() method on the comments collection:
$confirmedComments = $post->comments->where('confirmed', 1);
This will go through all the comments in the collection and only return those that are confirmed.
If you don't have the existing collection, you can add the where clause to the relationship query. This way, you're only getting confirmed comments from the database, and you're not wasting resources retrieving and building models for comments you're not using
$confirmedComments = $post->comments()->where('confirmed', 1)->get();
And, another option, would be to create a new relationship for just confirmed comments, and that way you can eager load the new relationship:
public function comments()
{
return $this->hasMany(Comments::class);
}
public function confirmedComments()
{
return $this->comments()->where('confirmed', 1);
}
$confirmedComments = $post->confirmedComments;

laravel passing data to view blade

//model
return $this->hasMany('App\Model\Order', 'customer_id')->select('name');
//controller
$customer = Model\customer::find($id);
return view('customer', array('data' => $customer));
//view (blade)
{{ $data }} //{"id":1,"name":"Tony"}
{{ $data->orders }} //[{"name":"T-shirt"},{"name":"Macbook"}]
i'm new in laravel
I have a question about passing data to view.blade
I used hasMany to join 2 table together and pass $data to view
when I try to output $data, I could not see order object inside of data object
but when I did $data->orders, it shows object
can anyone tell me how it works?
The behavior you are seeing is because Laravel lazy loads relations when they are accessed. If the relation has not been loaded, Laravel will send another query and add it to your $data object behind the scenes. That's why when you dump the $data variable, you are not seeing the orders.
To demonstrate, run the following snippet.
{{ $data }} //{"id":1,"name":"Tony"}
// Laravel will lazy load the orders relation
{{ $data->orders }} //[{"name":"T-shirt"},{"name":"Macbook"}]
// Now the $data object has the orders property.
{{ $data }} //{"id":1,"name":"Tony", "orders": [{"name":"T-shirt"},{"name":"Macbook"}]}
Solution
You have a couple options here. Here are 2.
1. Eager load relation when querying model.
$customer = Model\customer::with('orders')->find($id);
This is the preferred method as it prevents n+1 querying.
2. Load relation after model has been queried.
$customer = Model\customer::find($id);
$customer->load('orders');
Now when you dump the model, you will see the orders property.
In your model,
public function orders(){
return $this->hasMany('App\Model\Order', 'customer_id');
}
In view
{{ $data->orders->order_name }}
I hope it will works
You have defined hasMany relationship in your model. So, for a particular customer there can be multiple orders. So, it shows in array. You have to loop through:
foreach($data->orders as $order)
{
$order->name
}
to get the orders.

laravel virtual field/column in query?

I have a problem, I have 2 tables (posts, comments) and I want to select all posts + the number of their comments.
I could create a column "comments" in posts and write the counter in there, but I think that's a bad solution, so I am looking for something like virtual fields/columns in mysql to get the number of comments in the posts query.
Can I do this in Laravel?
In your Comment Model:
public function post(){
return $this->belongsTo('Post', 'post_id');
}
In your Post Model:
public function comments(){
return $this->hasMany('Comment', 'post_id');
}
In your controller, get a post with all its comments.
$post = Post::with('comments')->find(1);
In your view, display the post and the number of comments it has. Assuming your post has a title field.
Post Title: {{ $post->title }}
The post has {{ $post->comments->count() }} comments

Resources