How to cut the content in laravel5? - php

I need to cut the characters that show description with the same quantity of letter in all foreach.
Could anyone helps me?
#foreach ($subastas as $subasta)
#if(($subasta->data_inici <= $data)&&($subasta->data_final>$data))
<a href="item/{{$subasta->id}}"><div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<div class="nombre">{{$subasta->nombre}}</div>
#foreach ($subasta->images as $word => $meaning)
#if($word==0)
<img src="{{ asset($meaning->ruta) }}" style="width="242px" height="164px""/>
#endif
#endforeach
<h2><span class="clock" data-countdown="{{ $subasta->data_final}}"></span></h2>
<p>{{$subasta->descripcion}}</p>
<p>{{$subasta->cant_actual}}</p>
<a class="pujar btn btn-warning" id="{{$subasta->id}}">Pujar</a>
</div>
</div>
</div>
</div></a>
#endif
#endforeach
I think and image is better than explain .

Do you mean you want to limit the number of characters in a string?
This can be done in laravel by a string helper:
$value = str_limit('The PHP framework for web artisans.', 7);
So in your example:
<p>{{ str_limit($subasta->descripcion, 20) }}</p>

When whoever inputs the description of the product, can you not use max-length within the HTML element to set the limit prior to being saved?
<input type="text" maxlength="250" />

Related

Laravel: Looping through dynamic route parameter in blade template

I want to loop through named route with additional parameter, a series of IDs in this case, in the blade template. I have highlighted the line where I want to do this in the code with stars. My purpose is to access the next page when the 'next' button is clicked. The next page looks exactly like the page before, except for a different question card. At present, the page doesn't change on click of the button.
This is the defined route:
Route::get('/quiz/{id}', [QuestionController::class, 'show'])->name("question.show");
and this is the html blade template to generate the view.
`#foreach ($questions as $question)
<div class="col-12 col-md-6 my-2 offset-md-3">
<div class="card shadow p-3 mb-5 border bg-warning rounded border">
<h5 class="card-header"> {{ $question->category }} </h5>
<div class="card-body">
<h6 class="card-subtitle mb-2 text-muted">{{ $question->text }}</h6>
#foreach($question->options as $option)
<br>
<input type='radio' name="{{ $question->id }}" value="{{ $option->id }}">
{{ $option->text}} <br>
#endforeach
</div>
**<a href = "{{route('question.show',['id' => $question])}}" class="btn btn-light btn-sm offset-2 offset-md-10" role="button">
Next**
</a>
</div>
</div>
#endforeach`
You can do this by using an index with your foreach loop.
Change this: #foreach ($questions as $question)
to this: #foreach ($questions as $index=>$question)
Then in your anchor tag do something like this:
<a href="{{route('question.show',['id' => $questions[$index + 1]])}}"
class="btn btn-light btn-sm offset-2 offset-md-10" role="button">
Make sure to surround the button with an if statement to catch the last question.
#if( $index + 1 < count($questions) )
edit: missing bracket

bootstrap 4 grid row and php #foreach

I am trying to switch over a site to Bootstrap 4 from Bootstrap3 and having an issue with the Grid "row" option when nesting with looping through a PHP Laravel #foreach loop.
The code that I have tried in different ways looks like:
<div class="row">
<div class="col-12">
<div class="row">
#foreach($sofas as $sofa)
#if(isset($sofa['Sub']))
<a href="{{ Request::url() }}/{{ $sofa['Style'] }}">
<div class="col-12 col-sm-4 col-md-3" style="padding:0 5px 0 5px;">
<div class="product_tile">
<div class="product_tile_style">{{ $sofa['StyleName'] }}</div>
<div class="product_tile_frame">Frame {{ $sofa['Style'] }}</div>
<div class="product_tile_thumb">
<img width="200" height="140" src="/imagelib/bigthumbs/{{ $sofa['url'] }} " alt="">
</div>
</div>
</div>
</a>
#endif
#endforeach
</div>
</div>
The expected outcome should look like the following:
Example of what it should look like.
And this is the random mess that I am getting where it is shrinking everything and not expanding to the columns.
Example of what I am getting which is wrong.
Any ideas and help would be greatly appreciated.
Thank you all in advance!

Forelse loop inside a <div> tag

I'm trying to make a container with a list of images and attributes, but when I add a tag around the array, the array simply go outside my container, I had searched this type of question but didn't find it.
This is the code:
#if(count($elitePublications) > 0)
<div style="border:2px gold solid">
#forelse($elitePublications as $key => $publication)
#include('publications.small', compact('publication'))
#empty
<p></p>
#endforelse
</div>
#endif
It only make the container but the list is outside
The code of publications.small:
<div class="col-sm-4 book">
<a href="{{ route('users.publications.show', ['publications'=>$publication->id, 'users'=>$publication->user->username]) }}">
<div class="book-cover">
{{ $publication->image }}
<div class="inner-book-content">
<h4>{{ $publication->published_book_title }}</h4>
<strong class="book-price">
</strong>
</div>
</div>
</a>
</div>
Thanks in advance.
The issue here is about Bootstrap. Some kind of problem with the columns and the rows (who guest the results), so I just added a class in my div called "row" it act as a container for my array.
Like:
<div class="row">
<h4 style="font-style: italic;"> Recomendados </h4>
#forelse($elitePublications as $key => $publication)
#include('publications.small', compact('publication'))
#empty
#endforelse
</div>

MethodNotAllowedHttpException not found when I paginate in Laravel

Hello this is my first question here.
I am using php laravel framework and I am getting this error
MethodNotAllowedHttpException in RouteCollection.php line 233:
This error comes when I go to second page of the result list.
My controller code.
public function find_product(Request $request)
{
$search = trim($request->product);
$products = Store_product::(query-for-products-working)
->paginate(1);
return view('fc.product',compact('products','search'));
}
My web.php code
Route::post('/product', 'FlashCartController#find_product');
My view code
#foreach($products as $product)
<div class="fc-col">
<div class="panel panel-primary">
<div class="panel-heading fc-col-head"><div class="marquee">{{ $product->product_name }}</div></div>
<div class="panel-body fc-col-body">
<img src="{{ image_check('uploads/store/products/',$product->product_image1,'uploads/service/') }}" class="img-responsive" style="width:100%; height: 100%;" alt="{{ $product->product_name }}" />
</div>
<div class="panel-footer fc-col-footer">
<span class="price">Rs.
{{
price_check($product->product_discount, $product->product_price, $product->sale_id, $product->discount)
}}/-
</span>
</div>
</div>
</div>
#endforeach
<div>
{{ $products->links() }}
</div>
and the form
<form action="/product" method="POST">
{{ csrf_field() }}
<div class="input-group container">
<input type="text" name="product" class="form-control" value="{{$search}}" placeholder="Enter product name" />
<div class="input-group-btn">
<input type="submit" class="btn btn-danger" value="Search" />
</div>
</div>
</form>
Why my method is not allowed. And if this is not the method to use what should I do?
Can anyone help me with this please? :(
Okay so your problem as I understand it is that when you paginate to the next page the url becomes empty and no results are shown.
In your view you have this line:
{{ $products->links() }}
Which shows that whatever your url is at the moment, just ignore it and add pagination to it.
That means if your url is like www.abc.com?product=graphics it will ignore product and only add www.abc.com?page=1,2,... Of course your page will be blank.
Instead use this:
{{ $products->appends(request()->input())->links() }}
Now it tells the system to add pagination but append variables to it too. What variables? The variables that are appended on the url already.
Hope it helps

How to skip first item in a foreach loop in Laravel?

I am trying to fill my webpage with content based on content stored in a database. However, I would like to skip the first item; I want to start looping from the second item.
How can I achieve this?
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
As of Laravel 5.4, whenever you use foreach or for within blade files you will now have access to a $loop variable. The $loop variable provides many useful properties and methods, one of them being useful here, for skipping the first iteration. See the example below, which is a much cleaner way of achieving the same result as the other older answers here:
#foreach ($rows as $row)
#if ($loop->first) #continue #endif
{{ $row->name }}<br/>
#endforeach
Try This :
#foreach($aboutcontent as $key => $about)
#if($key > 0){
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif;
#endforeach
Assuming that the $aboutcontents is numeric array just use the good old fashioned for loop instead of your new fangled foreach
// Notice you start at 1 and your first
// elem is 0 so... ta da... skipped
#for ($i = 1; $i < count($aboutcontents); $i++){
$about = $aboutcontents[$i]; //This is the object
//now use $about as you would
}
Note: I have not used Larvel or blades but based on the docs this should be doable
There is two way to do this:
1- if your $key is numeric you can use:
#foreach($aboutcontent as $key => $about)
#if($key == 0)
#continue
#endif
code
#endforeach
2- if a $key is not numeric
use #loop->first as a condition
You need some kind of a counter if you want to do that in blade:
<?php $count = 0;?>
#foreach
#if($count>1)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif
$count++
#endforeach
EDIT:
I like the answer provided by Mark Baker in the comment better
#foreach(array_slice($aboutcontent, 1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
Alternatively, you can just remove the first element from the array before iterating:
#php
array_shift($aboutcontent);
#endphp
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
The advantage is that you do not need any conditions to verify you are not the in the first iteration. The disadvantage is that you might need the first element within the same view, but that we don't know from your example.
Note It might make more sense to remove the first element from the array before you pass on the data to the view.
For reference, see:
http://php.net/manual/en/function.array-shift.php
https://laravel.com/docs/5.4/blade#php
Try this
#foreach($aboutcontent->slice(1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach

Categories