I have this code snippet in my blade:
#foreach($products as $product)
<tr>
<td></td>
<td>{{ $product->name }}</td>
<td>{{$product->tags}}</td>
<td>{{$product->created_at}}</td>
<td>
// some other code and buttons
</td>
</tr>
#endforeach
In $product->tags ( tags is the name of my relation ) are the tags I need and some other things, but I only want the tags.
I tried to reach them with $product->tags->tag but this hasn't worked for me. Can anybody tell me how I can access only the tags?
Try this:
#foreach($product->tags as $tag)
<td>{{ $tag->tag }}</td>
#endforeach
$product->tags returns an array of Tag objects.
If you have a relationship set between your Products and it's Tags (https://laravel.com/docs/5.1/eloquent-relationships)
Products Model
//namespace and use statements
class Products extends Model
{
/**
* Get all of the tags for the product.
*/
public function tags()
{
return $this->hasMany('App\Tags');
}
}
Tags Model
(assuming tags can be used for multiple products)
//namespace and use statements
class Tags extends Model
{
/**
* The tags that belong to the product.
*/
public function products()
{
return $this->belongsToMany('App\Products');
}
}
Then you can query in you controller for the products with their tags (https://laravel.com/docs/5.1/eloquent-relationships#querying-relations)
$products = App\Products::with('tags')->get();
Then you can simply access them in your view with your current code but using
#foreach($products as $product)
<tr>
<td></td>
<td>{{ $product->name }}</td>
#foreach($product->tags as $tag)
<td>{{ $tag->name }}</td>
#endforeach
<td>{{ $product->created_at }}</td>
<td>
// some other code and buttons
</td>
</tr>
#endforeach
Related
I am trying to read data from a local collections from my mongodb database. Name of database Intuit name of collection post.
Code inside PostController:
<?php
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller{
public function home() {
$posts = Post::with(['user'])->get();
return view('home', ['posts' => $posts->toArray()]);
}}
Code inside Post:
class Post extends Model{
use HasFactory;
protected $fillable = [
'STATUS',
'DDC_CODE',
'TRADE_NAME',
'SCIENTIFIC_CODE',
'SCIENTIFIC_NAME',
'INGREDIENT_STRENGTH',
'DOSAGE_FORM_PACKAGE',
'ROUTE_OF_ADMIN',
'PACKAGE_PRICE',
'GRANULAR_UNIT',
'MANUFACTURER',
'REGISTERED_OWNER',
'UPDATED_DATE',
'SOURCE'
];}
All of the above are the titles of the columns in the table.
Next home.blade.php:
<!DOCTPE html>
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border = "1">
<tr>
<td>Id</td>
<td>STATUS</td>
<td>DDC_CODE</td>
<td>TRADE_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>SCIENTIFIC_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>INGREDIENT_STRENGTH</td>
<td>DOSAGE_FORM_PACKAGE</td>
<td>ROUTE_OF_ADMIN</td>
<td>PACKAGE_PRICE</td>
<td>GRANULAR_UNIT</td>
<td>MANUFACTURER</td>
<td>UPDATED_DATE</td>
<td>SOURCE</td>
</tr>
#foreach ($posts as $post)
<tr>
<td>{{ $post->_id }}</td>
<td>{{ $post->STATUS }}</td>
<td>{{ $post->DDC_CODE }}</td>
<td>{{ $post->TRADE_NAME }}</td>
<td>{{ $post->SCIENTIFIC_CODE }}</td>
<td>{{ $post->SCIENTIFIC_NAME }}</td>
<td>{{ $post->INGREDIENT_STRENGTH }}</td>
<td>{{ $post->DOSAGE_FORM_PACKAGE }}</td>
<td>{{ $post->ROUTE_OF_ADMIN }}</td>
<td>{{ $post->PACKAGE_PRICE }}</td>
<td>{{ $post->GRANULAR_UNIT }}</td>
<td>{{ $post->MANUFACTURER }}</td>
<td>{{ $post->REGISTERED_OWNER }}</td>
<td>{{ $post->UPDATED_DATE }}</td>
<td>{{ $post->SOURCE }}</td>
</tr>
#endforeach
</table>
</body>
</html>
The route handler is as follows
Route::get('view','App\Http\Controllers\PostController#home');
all of this seems to be fine but when I load up the application I see nothing but the column titles not sure what I am missing, I believe it is not working because the query in PostController is incorrect and need to change it as it is taken from another projec. Any and all help and suggestions are welcome.
Instead of
Post::with(['user'])->get();
use
Post::with(['user'])->all();
subject to user relationship is properly defined in your Post model
You are sending data as array, But you are trying to get them as object in your blade view file.
Instead of getting data like this:
$post->STATUS
Try using like this:
$post['STATUS']
Don't use ->toArray()
Pass data as below & variables will get displayed
return view('home', ['posts' => $posts)]);
I'm trying to make a show view with Laravel 8 but i can't show the detail, this is the code from the controller:
public function show($id)
{
$accesorios=DB::table('accesorio as acc')
->join('detalle_aparato as da','acc.idAccesorio','=','da.idAccesorio')
->select('acc.Nombre')
->where('da.idAparato','=',$id);
return view("almacen.aparato.show",["accesorio"=>Accesorio::findOrFail($id)]);
}
And this is the code from the view:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
Error message
When I use:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc}}</td>
</tr>
#endforeach
It prints: 1 for each record
Hope you can help me
In you're controller you're using DB::Table to set the $accesorios variable, but never using it.
You then are setting accesorio in your view to Accesorio::findOrFail($id) which will only return one instance of the object.
Either pass $accessorios into your view
return view("almacen.aparato.show",["accesorios"=>$accessorios]);
then loop through it
#foreach ($accesorios as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
or since you're just sending one instance of the object to the view, remove the loop and you can render it like this.
<tr>
<td>{{ $accesorio->Nombre }}</td>
</tr>
i cant access my admin_table i know i have and problem on my route on the admin_table please help me on my route Property [id] does not exist on this collection instance.
my admin_table.blade
#foreach ($users as $positions)
#foreach ($positions->admins as $position )
<tbody>
<tr>
<td>{{ $position->id}}</td>
<td>{{ $position->first_name}}</td>
<td>{{ $position->last_name}}</td>
<td>{{ $position->contact}}</td>
<td>{{ $position->departments->department}}</td>
<td>{{ $position->schoolpositions->school_position}}</td>
<td>{{ $position->email}}</td>
<th> Edit
</th>
</tr>
</tbody>
#endforeach
</tr>
</tbody>
#endforeach
#endforeach
</table>
this is my route
Route::get('/admin_table', 'DashboardController#index');
Route::put('/admin_table/{id}', 'DashboardController#store');
Route::get('/admin_table', 'DashboardController#show');
Route::get('/teacher_editform/{id}', 'DashboardController#edit');
Route::put('/teacher_editform/{id}', 'DashboardController#update')->name('teacheradminpage.teacher_tableform.teacher_editform');
this is my controller
public function edit($id)
{
$departments = Department::find($id);
$users = Schoolposition::find($id);
$students = Student::find($id);
$admins = Admin::find($id);
return view('teacheradminpage.teacher_tableform.teacher_editform', compact('departments','users','students','admins', 'id', 'id', 'id', 'id'));
}
Your issue is that you put an extra $position before the array. Your route should be
Edit
You've got several issues in this code that probably need attention. The main issue with the position error, seems to be that you've got a circular path to get that $position in your anchor tab within the loop.
This section is likely not creating what you want:
#foreach ($users as $positions)
#foreach ($positions->admins as $position )
This is roughly saying - take my collection of users and make a new collection of positions that are directly attached to each user object. From here, take that collection of positions and try and find a collection of admins on that are attached to that collection and then find a $position. (I think this is close to what it is trying to do...)
When you get to the inside of the loop and your anchor / route, you are asking the anchor to find an id on something that is likely either non-existent, or a collection again.
You have already passed in admins to this view. It would make sense to use that as your top level loop item, no?
To fix, use admins as the only loop item:
#foreach ($admins as $position )
Edit Your Blade With
#foreach ($users as $positions)
#if(!empty($positions->admins)
#foreach ($positions->admins as $position )
<tbody>
<tr>
<td>{{ $position->id}}</td>
<td>{{ $position->first_name}}</td>
<td>{{ $position->last_name}}</td>
<td>{{ $position->contact}}</td>
<td>{{ $position->departments->department}}</td>
<td>{{ $position->schoolpositions->school_position}}</td>
<td>{{ $position->email}}</td>
<th> Edit
</th>
</tr>
</tbody>
#endforeach
</tr>
</tbody>
#endforeach
#endif
#endforeach
</table>
I want to display data from an eloquent relationship in my view but I seem to be doing something wrong. Here's my model relationship, controller and view
Product
class Product extends Model
{
protected $guarded = ['id'];
public function prices()
{
return $this->hasMany(Price::class, 'product_id');
}
}
Price
class Price extends Model
{
protected $guarded = ['id'];
public function product()
{
$this->belongsTo(Product::class);
}
}
HomeController
public function index()
{
$products = Product::with('prices')->get();
return view('home', compact('products'));
}
View
#foreach ($products as $product)
<tr>
<td>{{ $product->prices->date }}</td>
<td>{{ ucwords($product->name) }}</td>
<td>{{ $product->prices->cost }}</td>
</tr>
#endforeach
My view returns the error
Property [date] does not exist on this collection instance
How do i fix this error?
In hasMany relations you have a Collection as result, so you should have another foreach to access each Price. Try something like this:
#foreach ($products as $product)
#foreach ($product->prices as $price)
<tr>
<td>{{ $price->date }}</td>
<td>{{ ucwords($product->name) }}</td>
<td>{{ $price->cost }}</td>
</tr>
#endforeach
#endforeach
For a blog site, I have all the posts & lists of categories displaying on a page though now I need to display the posts for each category on it's own separate page.
Let's say I had a categorie that was 'Javascript' and I wanted only the posts with the category of javascript displayed on a page.
What's the correct code to do this? here's an example, the bold 'javascript' is what needs to be replaced with the correct code.
-- categoriesController.php ---
public function show($id)
{
$post->withCategories($Categories)->$id->($id as **javascript)**
}
--- javascript.blade.php --- ( corresponding view )
<tbody>
#foreach ($categories as $category->$id>**javascript**)
<tr>
<th>{{ $category->id }}</th>
<td>{{ $category->name }}</td>
</tr>
#endforeach
</tbody>
</table>
</div> <!-- end of .col
For example:
post.php model
class Post extends Model
{
protected $primaryKey = 'id';
function withCategories() {
return $this->hasOne('App\Categories', 'id', 'category_id');
}
public function show($id){
Post::with('withCategories')->where('category_id', $id)->get(); //the output of articles of the category
}
}
$id is a parameter of url: site.com/posts/javascript
in posts.blade.php
<table>
<tbody>
#foreach ($posts as $post)
<tr>
<th>{{ $post->id }}</th>
<td>{{ $post->name }}</td>
<td>{{ $post->withCategories->name }}</td> <!-- Category name-->
</tr>
#endforeach
</tbody>
</table>
</div>