cant use the variable that i send to a blade component page - php

hi sorry for my bad engilsh.
im using blade component page in laravel to include a blade page in another blade page
i use component blade page.
i send A VARIABLE that its name is "post" like this
#foreach($posts->skip(1) as $post)
<x-post-card :post="$post"/>
#endforeach
i use #props(['post']) on that page to get the variable
#props(['post'])
and when put 'post'
on #ddd its fine and ok
but when i wonna use its property like 'title' the laravel send me a error that Undefined variable: post
this the way i use property
#props(['post'])
#dd($post)
<article class="transition-colors duration-300 hover:bg-gray-100 border border-black border-opacity-0 hover:border-opacity-5 rounded-xl">
<div class="py-6 px-5 lg:flex">
<div class="flex-1 lg:mr-8">
<img src="/images/illustration-1.png" alt="Blog Post illustration" class="rounded-xl">
</div>
<div class="flex-1 flex flex-col justify-between">
<header class="mt-8 lg:mt-0">
<div class="space-x-2">
{{$post->category->name}}
</div>
<div class="mt-4">
<h1 class="text-3xl .bold">
{{$post->title}}
</h1>
<span class="mt-2 block text-gray-400 text-xs">
Published <time>{{$post->created_at->diffForHumans()}}</time>
</span>
</div>
</header>
<div class="text-sm mt-2">
<p>
{{$post->excerpt}}
</p>
</div>
<footer class="flex justify-between items-center mt-8">
<div class="flex items-center text-sm">
<img src="/images/lary-avatar.svg" alt="Lary avatar">
<div class="ml-3">
<h5 class="font-bold">{{$post->user->name}}</h5>
<h6>Mascot at Laracasts</h6>
</div>
</div>
<div class="hidden lg:block">
Read More
</div>
</footer>
</div>
</div>
</article>

Related

Invalid argument supplied for foreach() - the rest is working fine

I'm developing a website like instagram in laravel
everything was going fine until i implemented the foreach command
this is my code:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-3 p-5">
<img src="/images/simbolomax.jpg" class="rounded-circle">
</div>
<div class="col-9 pt-5">
<div class="d-flex justify-content-between align-items-baseline">
<h1 class="p-2">{{ $user->username }}</h1>
Aiciona nova receita
</div>
<div class="d-flex">
<div class="pe-5"><strong>153</strong> receitas</div>
<div class="pe-5"><strong>23k</strong> seguidores</div>
<div class="pe-5"><strong>212</strong> seguindo</div>
</div>
<div class="pt-4">{{ $user->profile->description }}</div>
</div>
</div>
<div class="row pt-5">
#foreach($user->posts as $post)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
#endforeach
</div>
</div>
#endsection
and the error is: Invalid argument supplied for foreach() (View: C:\projetos\easymeal\resources\views\profiles\index.blade.php)
I can't figure out, send help!
If there is a user with no posts... There goes ERROR. So.... Check user for posts before #foreach.
#if(count($user->posts)>0).... #foreach
Try this
#if ($user->posts->isNotEmpty())
#foreach ($user->posts as $item)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
#endforeach
#endif

Change default pagination style in laravel

#extends('layouts.app')
#section('content')
<h4 style="text-align:center;">SHOES</h4>
#foreach($shoes as $list)
<div style="margin-top: 40px">
<div class="row d-flex justify-content-md-center">
<div class="card" style="width: 22rem;">
<img class="card-img-top justify-content-center" src="{{ asset("images/$list->shoe_image") }}" alt="" style="width:350px; height:350px;">
{{-- bg-card-pink p-2 m-2 --}}
<div class="card-body">
<h1>{{ $list->shoe_name }}</h1><br>
<h6>{{ $list->shoe_description }}</h6><br>
${{ $list->shoe_price }}<br>
</div>
<a class="btn btn-primary btn-dark" href="/detail/{{ $list->id }}">
SEE DETAILS
</a>
</div>
</div>
</div>
#endforeach
<div class="row d-flex justify-content-md-center mt-4">{{ $shoes->links() }}</div>
#endsection
Anybody had any idea changing the color/style of the pagination button on the blade?
I use default pagination by laravel which is $shoes->links()
You can customize default pagination by exporting pagination files from vendor folder to resources.
To export run in terminal: php artisan vendor:publish --tag=laravel-pagination
See Laravel Docs

How to make several rows with foreach?

I am using Laravel 5.8 and in my controller I am showing 4 elements with paginate():
public function index()
{
$proyects = Proyect::latest()->paginate(4);
return view('proyect.index', compact('proyects'));
}
In the view the 4 elements are in a single row but I would like to show 2 elements for each row.
<div class="row">
<div class="col-10 col-lg-12 col-md-6">
<div class="card-deck">
#forelse($proyects as $proyect)
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">
{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as an
overlay for the body</p>
<a href="{{ route('proyect.show', $proyect) }}"
class="card-link">Ver mas</a>
</div>
<div class="card-footer">
<small class="text-muted">
{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
#empty
<li> Empty </li>
#endforelse
</div>
</div>
How to show 2 items per row instead of all items in a row?
public function index()
{
$proyects = Proyect::latest()->paginate(10);
return view('proyect.index', compact('proyects'));
}
array_chunk is a php function ca splice your array.
#forelse(array_chunk($proyects->all(),2) as $rows)
<div class="row">
#foreach($rows as $proyect)
<div class="col-10 col-lg-12 col-md-6">
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as overlay for the body</p>
Ver mas
</div>
<div class="card-footer">
<small class="text-muted">{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
</div>
</div>
#endforeach
</div>
#empty
<div>Empty ...</div>
#endforelse
May be this is your solution:
<div class="row">
#forelse($proyects as $proyect)
<div class="col-6 col-lg-6 col-md-6">
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">
{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as an
overlay for the body</p>
<a href="{{ route('proyect.show', $proyect) }}"
class="card-link">Ver mas</a>
</div>
<div class="card-footer">
<small class="text-muted">
{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
#empty
<li> Empty </li>
</div>
#endforelse
</div>

laravel foreach broke column

Sorry for title i couldn't think better title!
anyway, this is my blog page code:
#extends('layouts.frontend_index')
#section('title', 'Blog')
#section('content')
<div class="container">
<div class="row" style="margin-bottom:30px;">
<div class="col-md-8">
<div class="row">
#forelse($posts as $post)
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12" style="margin-bottom:15px;">
<div class="card card2">
<div class="cardimg">
<img src="{{ url('images/') }}/{{ $post->image }}" class="card-img-top" style="width:100%; height:auto; max-height:500px;" alt="{{ $post->title }}">
</div>
<div class="card-block">
<h4 class="card-title">{{$post->title}}</h4>
<p class="card-text">{!! str_limit($post->body, 10) !!}</p>
<p><a class="btn btn-block btn-link" href="{{ route('frontshow', $post->slug ) }}">Read More <i class="fa fa-arrow-right"></i></a></p>
</div>
</div>
</div>
#empty
<p>No Post Available yet!</p>
<div class="text-center">
<a class="btn btn-error" href="{{url('/')}}">Back to Main Page</a>
</div>
#endforelse
<div class="col-md-12 text-center">
{{$posts->links()}}
</div>
</div><!--end row-->
</div><!--end col-md-8-->
<div class="col-md-4">
<h3>Get Latest Updates...</h3>
<a target="_blank" href="https://t.me/studiotjd"><div id="telegram"></div></a>
<a target="_blank" href="https://www.facebook.com/studiotjd"><div id="facebook"></div></a>
<a target="_blank" href="https://www.instagram.com/studiotjd/"><div id="instagram"></div></a>
</div><!--end col-md-4-->
</div><!--end row-->
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-4 span3 wow rollIn" style="animation-delay: 1s;"><div id="Library"></div></div><!--end col-md-4-->
<div class="col-md-4 span3 wow bounceInDown center" style="animation-delay: 1s;"><div id="Library2"></div></div><!--end col-md-4-->
<div class="col-md-4 span3 wow bounceInRight" style="animation-delay: 1s;"><div id="Library3"></div></div><!--end col-md-4-->
</div><!--end row-->
</div><!--end col-md-12-->
</div><!--end row-->
</div>
#endsection
here is how it looks like in view:
I tried almost everything to fix this issue but nothing works!
As you can see in my inspect all col-md-4 goes under first col-md-4 and i don't know why
Is there anyone knows how to fix this issue?
try this
{{ strip_tags(str_limit($post->body, 10)) }}
I think you have html element stored in your database, and you are just using str_limit so the tags in your body is not closed.
problem is a height of div, check it on the inspector code. 1 px or more is

Laravel foreach database issue

I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?
I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{id}', 'ThemesController#show')->name('showtheme');
ThemesController.php
I only show the show method because I believe that's the only one necessary here
public function show($id)
{
$topics = Topic::all($);
return view('themes/topics')->with('topics', $topics);
}
topics.blade.php
#extends('layouts.default')
#section('content')
<div class="container main-content">
<div class="row first-row">
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Laatste reply</h6>
<p class="center-align">Naam</p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
</div>
</div>
#endsection
I believe I'm doing something obviously wrong but I can't see what.
Help will be appreciated. Thanks in advance
If I miss some code, Please inform me.
How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?
****edit****
public function show($id)
{
$topics = Theme::find($id)->topics;
return view('themes/topics')->with('topics', $topics);
}

Categories