How to show data in blade by id - laravel

I have to make a website for student hostels in my country I have a special page for every hostel that shows its all photos and property but I don't know how can I do this with the hostel id when I have a hostel table if someone can help me, please!
thank's for all
This is my hostel controller code:
class HostelDetailsController extends Controller
{
public function index(Request $request,$detail_id)
{
// $id = $request->query($detail_id);
$file_details = HostelDetails::all();
$files = Attachment::get();
return view('khabgah_details', compact(['file_details', 'files']));
}
}
This is my hostel page blade code:
#foreach($file_details as $file)
<div class="card-title text-center">
<div class="mt-4"></div>
<i class="fa fa-home mt-4 text-center" style="font-size:45px;"></i>
<h5 class="mt-3 text-center"> لیلیه نرگس </h5>
<h6 class="fa fa-phone"> شماره تماس: {{ $file->phone_number }} </h6>
</div>
#endforeach
It doesn't show any error message but when I load the page it shows all my records in the table

//You can get the id from the index page of the hostel
public function show($id)
{
$details = Detail::where('hostel_id', '=', $id)->get();
return view('HostelDetailPage',compact('details'));
}

Related

Show category posts in Laravel is not working

I want to show posts that are in a category by clicking on them, but when I click on a category,$articles in category.blade.php returns null.
$articles should return articles that have a specific category
this is my index.blade.php:
<div class="tab-pane fade" id="show-categories" role="tabpanel">
<h6 class="sidebar-title">Categories</h6>
<div class="row link-color-default fs-14 lh-24">
#foreach($categories as $category)
<div class="col-6">
<a href="{{ route('cms.category', $category->id) }}">
{{ $category->name }}
</a>
</div>
#endforeach
</div>
</div>
category.blade.php:
#extends('layouts.app')
#section('content')
#forelse ($articles as $article)
<h1>{{$article->title}}</h1>
#empty
<span>array is empty</span>
#endforelse
#endsection
router:
Route::get('cms/categories/{category}', [articlesController::class, 'category'])->name('cms.category');
and articlesController:
public function category(Category $category)
{
return view('cms.category')
->with('category', $category)
->with('articles', $category->articles()->searched()->simplePaginate(3))
->with('categories', Category::all())
->with('tags', Tag::all());
}
Please, don't forget to define the relationship from Category to Article on your model. Like: one category has many articles. For example:
On your Category.php model:
public function articles()
{
return $this->hasMany(Article::class);
}
Then on your, controller, you may call the relationship like this way:
public function category(Category $category)
{
$articles = Category::with("articles")->where("id", $category->id)->simplePaginate(3);
return view('cms.category')
->with('category', $category)
->with('articles', $articles)
->with('categories', Category::all())
->with('tags', Tag::all());
}
Please share the model file and second I prefer to write the category method in a different way.
public function category($category_id)
{
try {
$articles= Article::where('id', $category_id)->get();
return view('cms.category', $articles);
} catch (\Throwable $th) {
Log::error($th->getMessage());
}
}
Let me know if you still face any issue. Try to use dd $articles ..

Get brand name, instead of ID - Laravel 5.8

Good someone can help me, I am not doing it well. I have a table called products, in it a field called brand_id, then I have a table called brands, in which I list it by its ID field, this table also contains a column called nombre.
Model producto
class Producto extends Model
{
protected $table = 'productos';
public function marcas()
{
return $this->belongsTo(Marca::class , 'id');
}
}
Model Marca
class Marca extends Model
{
protected $table = 'marcas';
public function producto()
{
return $this->hasMany(Producto::class , 'id_brand');
}
}
Controller
public $categorias, $productos, $ofertados;
public function __construct()
{
$this->productos = Producto::with('marcas')
->latest('id')
->limit(6)
->whereEstado(1)
->get();
}
View
#foreach ($productos as $pro)
<div class="col-4 catProduct">
<div class="col-12 bordeCajon">
#if ($pro->novedad == 1)
<div class="nEntrada">NOVEDAD</div>
#endif
#if ($pro->pvpAntes == null)
#else
<div class="oferta">OFERTA!</div>
#endif
<div class="minHei">
<img src="{{ asset("{$pro->imagen}") }}" alt="">
</div>
<div class="paddingProducto">
#php
/** echo "<pre>"; print_r($pro->marcas);*/
#endphp
<h2> {{ $pro->marcas->mombre }} </h2>
<h3>{{ $pro->producto }}</h3>
<div class="dividerProd"></div>
<h4>{{ $pro->pvpAhora }} <span>€</span></h4>
#if ($pro->pvpAntes == null)
#else
<div class="pvpAntes">{{ $pro->pvpAntes }} <span>€</span></div>
#endif
VER PRODUCTO
</div>
</div>
</div>
#endforeach
Error
Trying to get property 'nombre' of non-object (View: /Applications/MAMP/htdocs/camasLiteras/resources/views/index.blade.php
For example, in the marks table I have 4 marks. In the beginning I have 6 products each with a brand. Well it does what we have done is take the product and put the name of the brand in the same order of the brand table, that is, it does not put the name that it touches. And since there are 6 products but only 4 brands, that's why it says the 'name' of no-object '. If I deactivate two products it doesn't give me an error but it shows in each product a different brand, putting the 4 there are.

Laravel Select the First Row From HasMany Relation

I have 2 table where 1 products have many prodphotos
I can retrieve all prodphotos from products with the same id, but my case is listing all the products but only take 1 photo from prodphotos.
Controller :
public function daftarproduk()
{
$produks = Product::orderBy('created_at', 'desc')->get();
$select = Product::with('prodphotos')->firstorfail();
$photo = $select->prodphotos->pluck('photo');
$kategori = Category::orderBy('created_at', 'asc')->get();
return view('guest.daftarproduk')
->with('produks',$produks)
->with('kategori',$kategori)
->with('select',$select)
->with('photo',$photo);
}
View :
#foreach($produks as $value)
<div class="col-xs-6 col-md-4 col-lg-3 box-product-outer">
<div class="box-product">
<div class="img-wrapper">
<a href="detail/{{$value->id}}/{{str_slug($value->name)}}">
<img alt="Product" src="images/gambarproduk/thumb_{{ i dont know what i must throw here to only get first picture }}">
</a>
</div>
<h6>{{$value->name}}</h6>
</div>
</div>
#endforeach
I dont know what function I must use to get the first photo name from $select or $photo from controller. or my foreach logic is wrong? Please help me.
Add a featured photo relation with hasOne type to your Product model.
Product model
public function featuredPhoto() {
return $this->hasOne(PhotosModel);
}
In your controller
public function daftarproduk()
{
// Get the products with featured image
$produks = Product::with('featuredPhoto')->orderBy('created_at', 'desc')->get();
$kategori = Category::orderBy('created_at', 'asc')->get();
return view('guest.daftarproduk')
->with('produks',$produks)
->with('kategori',$kategori);
}
View
#foreach($produks as $value)
<div class="col-xs-6 col-md-4 col-lg-3 box-product-outer">
<div class="box-product">
<div class="img-wrapper">
<a href="detail/{{$value->id}}/{{str_slug($value->name)}}">
<img alt="Product" src="images/gambarproduk/thumb_{{ $value->featuredPhoto->photo }}">
</a>
</div>
<h6>{{$value->name}}</h6>
</div>
</div>
#endforeach

Call to a member function tasks() on null on laravel 5

Let me explain with a couple of word my problem.
On my controller i have this line:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
This works for existing users, but when i try to create new ones i receive
Call to a member function tasks() on null
Can i with something like this or what do you suggest ?
if(!is_null($tasks))
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
}
This i my show function on controller
public function show(){
$user = Auth::user();
if(!$user)
return redirect('/')->withErrors(config('constants.NA'));
$countries = Country::all()->lists('name', 'id')->toArray();
$profile = $user->profile;
$student = $profile->student;
// Tasks showed on students profile
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// Classmates
if ($student->group) {
$classmates = $student->group->students()
->where('id', '!=', $student->id)
->get();
}
// Activated books
$books = Auth::user()->books()->orderBy('grade_id', 'asc')->get();
if(!is_null($student->group))
$iTasks = $student->group->tasks->count();
else {
$iTasks = 0;
}
$iTodos = $user->todos->count();
return view('student.profile.show',compact('user','profile', 'countries', 'iTasks', 'iTodos', 'tasks', 'classmates', 'books'));
}
This is my show view, for the tasks
<div class="tab-pane <?php if(isset($tab) && $tab == 'timeline'): ?> active <?php endif; ?>" id="timeline">
#if($tasks->count())
<div class="timeline">
#foreach($tasks as $task)
<!-- TIMELINE ITEM -->
<div class="timeline-item">
<div class="timeline-badge">
<div class="timeline-icon">
<i class="mdi mdi-clipboard-text font-red-intense"></i>
</div>
</div>
<div class="timeline-body">
<div class="timeline-body-arrow"> </div>
<div class="timeline-body-head">
<div class="timeline-body-head-caption">
<span class="timeline-body-title font-blue-madison">
{{ $task->professor->profile->user->name }}
</span>
<span class="timeline-body-time font-grey-cascade">ju ca caktuar një detyrë të re.</span>
</div>
</div>
<div class="timeline-body-content">
<span class="font-grey-cascade">
{{ $task->pivot->comment }}
</span>
</div>
<hr>
Lenda: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->subject->name }}</span>
<div class="pull-right">
Krijuar më: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->created_at->format(Config::get('klasaime.date_format')) }}</span>
</div>
</div>
</div>
<!-- END TIMELINE ITEM -->
#endforeach
</div>
#else
<div class="alert">
Ju nuk keni asnje detyrë të caktuar!
</div>
#endif
</div>
Looks to me like you haven't added the student to a group yet, or that somehow hasn't been persisted. If you have added the student to a group after creating and saving the student, try this:
$student->load('group')
Before running:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
I'll need to see more of your code to give a more accurate answer. But the error you're getting is related to the ->group, of your student, not your student itself.
Do a simple check to see if the group exists and then carry on with whatever action you need to perform.
// controller
$tasks = null;
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// view
#if($tasks)
{{ $tasks->id }}
#else
No tasks found
#endif
Edit : In your controller add $tasks = null; and $classmates = null; at top. And in your view change #if($tasks->count()) to #if($tasks). I'm not sure where your use the classmates variable, but add a check to see if it's null before you use it.

get category name in a post record

I have a list containing article posts from all categories. I want to display the category tag in each post on the list and I tried this :
<article class="post bg z-depth-1">
<?php foreach ($article_list as $article): ?>
<div class="post-img">
<a href="post.html">
<img class="retina" src="{{ asset('image/' . $article->image) }}" width="820" height="500" alt="Post Image">
</a>
</div>
<div class="post-content">
<div class="post-header center-align">
<div class="tags">
{{ $article->category->title }}</div>
<h2 class="post-title">{{ $article->title }}</h2>
<div class="date">Posted on {{ Carbon\Carbon::parse($article->created_at)->format('F j, Y') }}</div>
</div>
<div class="post-entry">
<p>sss</p>
</div>
<div class="post-footer">
<div class="post-footer-item">
<span class="text">Read More</span> <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
</div><!-- .post-content -->
<?php endforeach ?>
</article><!-- .post -->
and it gave me error Undefined property: stdClass::$category and I don't know why, I already define the relationship in each model Article and Category. Thanks for the help!
In Article model :
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
In Category model :
public function article() {
return $this->hasMany('App\Article', 'category_id');
}
Controller :
public function index() {
$article_list = Article::all();
return view('article/index', compact('article_list', $article_list));
}
What you need to do is eager load those relationships in your controller.
When you pull the records do it like this:
$article_list = Article::with('category')->get();
https://laravel.com/docs/5.3/eloquent-relationships#eager-loading
Change this: $article->category->title
To this: $article->category()->title
When you define the relationship in your model you are also creating a function not a property called category.
public function category()
{
return $this->belongsTo('App\Category');
}
In Category model :
public function article() {
return $this->hasMany('App\Article');
}
Controller :
public function index() {
$article_list = Article::all();
return view('article/index', compact('article_list'));
}
This should work for you now like this.

Resources