I need your help with this simplePaginate();
controller:
foreach($lesson->products as $product)
{
$p[] = $product->simplePaginate(1);
}
view:
#foreach($p as $data)
<img src="{{ asset('storage/'.$data->image) }}" alt="">
#endforeach
{{ $p->links('layouts.paginate') }}
what is wrong that I got this error:
Undefined property: Illuminate\Pagination\Paginator::$image
Why dont you remove that foreach to generate $p and loop $lesson->products in the blade directly
#foreach($lesson->products as $product)
<img src="{{ asset('storage/'.$product->image) }}" alt="">
#endforeach
If you need the simplePaginate() for reasons not posted in your question, then you can loop it like this
#foreach($p as $productSimplePaginate)
#foreach($productSimplePaginate as $product)
<img src="{{ asset('storage/'.$product->image) }}" alt="">
#endforeach
#endforeach
Related
I'm new to laravel. How can i put if condition inside the foreach loop in blade template
my code is:
enter code here#foreach ($data as $data)
question: {{ $data->qid }}
Your answer: {{ $data->answer }}
Correct answer: {{ $data->anskey }}
#endforeach
#foreach($data as $data)
Question: {{ $data->qid }}
Your answer: {{ $data->answer }}
#if($data->anskey === $data->answer)
You correct
#else
You incorrect
#endif
#endforeach
####Ols
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
####new syntix for and if
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse
I want to show last database data in my website page but I have this error "Trying to get property 'header_image' of non-object"(I have data in database) what is the problem?
controller:
public function show()
{
$edu = Classes::get()->first();
return view('UI.classes.educationcourses' , compact('edu'));
}
blade:
#foreach( $edu as $class)
<section id="home">
<div id="home-slider">
<div class="slide-item">
<img src="{{ $class->header_image }}" alt="">
</div>
</div>
Try this -
In controller:
public function show()
{
$edu = Classes::all();
return view('UI.classes.educationcourses',compact('edu'));
}
And in blade file:
#foreach( $edu as $class)
<section id="home">
<div id="home-slider">
<div class="slide-item">
#if($class->header_image)
<img src="{{ asset('imagepath/'.$class->header_image) }}" alt="">
#else
<img src="{{ asset('imagepath/defaultimage.ext') }}" alt="">
#endif
<div class="slide-wrap">
Please make sure your image exists in the folder.
Update:
If you want a single row and the last one, then you don't need to go through with foreach loop in blade file.
Then you should try this:
public function show()
{
$edu = Classes::latest()->first();
return view('UI.classes.educationcourses',compact('edu'));
}
And in blade file:
<section id="home">
<div id="home-slider">
<div class="slide-item">
#if($edu->header_image)
<img src="{{ asset('imagepath/'.$edu->header_image) }}" alt="">
#else
<img src="{{ asset('imagepath/defaultimage.ext') }}" alt="">
#endif
<div class="slide-wrap">
You can get the last one by using the entry time in that table.
$edu = Classes::where('any_condition',true)->order_by('upload_time', 'desc')->first();
And then in the blade, you can simply use $edu without any #foreach loop. Also, where clause is not necessary, just added it there for now.
help or assist as it does not turn out to make sorting on categories, I can not understand as to solve this problem.
I recently study laravel.
Controller: class ArticlesController
public function showAll ($category_id)
{
$categories = Category::where('name', $category_id)->get();
return view('categories')->with(compact('categories', 'articles'));
}
This view from which I want to go into categories. site.blade.php
#foreach($categories as $category)
<li><a class="nav-link text-white" href={{route('articlesShow', ['id'=>$category->id]) }}">{{ $category->name }}</a></li>
#endforeach
Route:
Route::get('/categories/{category_id}', 'ArticlesController#showAll')->name('articlesShow');
Model: Category
public function articles()
{
return $this->hasMany(Article::class, 'category_id');
}
Representation in which does not get to get: categories.blade.php
#foreach($categories->articles as $article)
<div class="col-6 col-lg-4"> <!--Post -->
<img src="{{ asset('upload/image/1.jpg') }}" width="255" alt="..." class="rounded img_size">
<h4> {{ $article->title }}</h4>
<h6>Category: {{ $article->category->name }}</h6>
<h6>Author: {{ $article->author }}</h6>
<p>{{ $article->text }} </p>
<p><a class="btn btn-secondary" href="{{ route('articleShow', ['id' =>$article->id]) }}"> More »</a></p>
</div><!--EndPost-->
#endforeach
$categories is an array that contains categories, and the articles property belongs to a category, therefore you have to loop the categories before you can access the articles.
try this:
#foreach($categories as $category)
#foreach($category->articles as $article)
<div class="col-6 col-lg-4"> <!--Post -->
<img src="{{ asset('upload/image/1.jpg') }}" width="255" alt="..." class="rounded img_size">
<h4> {{ $article->title }}</h4>
<h6>Category: {{ $article->category->name }}</h6>
<h6>Author: {{ $article->author }}</h6>
<p>{{ $article->text }} </p>
<p><a class="btn btn-secondary" href="{{ route('articleShow', ['id' =>$article->id]) }}"> More »</a></p>
</div><!--EndPost-->
#endforeach
#endforeach
First of all: use eager loading.
Assuming, your ArticlesController#showAll method should display all articles for a given category id, your query should look like this
public function showAll ($category_id)
{
$categoryArticles = Category::with('articles')->where('id', $category_id)->get();
return view('categories')->with('categoryArticles', $categoryArticles);
}
Then to display all articles for the given category (assuming an article has only one category), this could be one way:
#foreach($categoryArticles->articles as $article)
<div class="col-6 col-lg-4"> <!--Post -->
<img src="{{ asset('upload/image/1.jpg') }}" width="255" alt="..." class="rounded img_size">
<h4> {{ $article->title }}</h4>
<h6>Category: {{ $categoryArticles->name }}</h6>
<h6>Author: {{ $article->author }}</h6>
<p>{{ $article->text }} </p>
<p><a class="btn btn-secondary" href="{{ route('articleShow', ['id' =>$article->id]) }}"> More »</a></p>
</div><!--EndPost-->
#endforeach
I'm having issues displaying a collection in a blade template.
$comments = Comment::all();
return view('comments/index')->with(compact('comments'));
The code for the blade is:
#isset($comments)
#foreach($comments as $comment)
<div>
<p>
<{{ $comment->commentor }}
</p>
</div>
<hr>
#endforeach
#endisset
#empty($comments)
<div>
<p>There were no comments available.</p>
{{ $comments }}
</div>
#endempty
But not sure how to get the data to render in the template. It just renders a blankpage.
Use this instead :
$comments = Comment::all();
return view('comments.index')->with(compact('comments'));
Use dot notation to reference the view folder structure, view('comments.index'). This represents the file resources/views/comments/index.blade.php. Use this.
#forelse ($comments as $comment)
<div>
<p>
{{ $comment->commentor }}
</p>
</div>
<hr/>
#empty
<div>
<p>There were no comments available.</p>
</div>
#endforelse
I have one question, I'm developing a multi-lang site and struggling of displaying data for each lang. Can I write a IF statement where it detects current locale, for example:
#if('locale' = 'lv')
#foreach($slides_lv as $slide)
<img src="{{ $slide->path }}">
#endforeach
#endif
#if('locale' = 'en')
#foreach($slides_en as $slide)
<img src="{{ $slide->path }}">
#endforeach
#endif
#if('locale' = 'ru')
#foreach($slides_ru as $slide)
<img src="{{ $slide->path }}">
#endforeach
#endif
Or there is some better way to do it in Laravel ?
You can do it this way:
<?php
$varName = 'slides_' . App::getLocale();
?>
#foreach ($$varName as $slide)
<img src="{{ $slide->path }}">
#endforeach
App::getLocale() will return the locale (in your case 'lv', 'en' etc).
So you build a dynamic variable name based on it and the use it for your foreach loop.
Take care