I'm using $query->created_at->diffForHumans(now()) to try to get it to show me for example "1 day ago" but the result it gives me is "1 day before" and I tried to change the language but they tell me nothing the same in English for example 1 day before what could it be due to and how can I change it?
Lendy Rodriguez Silva's questions
Is it possible to activate and deactivate an if from a button? what I need is relatively easy... although I can't get it... well let's get to the topic what I want is simply that when I press a button it shows the content of an if any suggestion??
<button type="button" name="button"></button>
@if()
<table>
<tr>
<td>contenido1</td><td>contenido2</td>
</tr>
</table>
@endif
I have the following controller:
public function show($id)
{
// Busco el perfil
$perfil = lista::where('di', $id)->first();
//con el resultado de la busqueda muestro el perfil
$mostrarperfil = Perfiles::where('id', $perfil['di'])->get();
return view('products.show', compact('mostrarperfil'));
}
This driver works correctly for me, but what I need is to do this but with several profiles at the same time, let's see if I understand myself. I want to change this:
$perfil = lista::where('di', $id)->first();
For this:
$perfil = lista::where('di', $id)->get();
This would give me several results but I don't know what changes I would have to make to it to be able to show all the profiles that $profile throws at me:
$mostrarperfil = Perfiles::where('id', $perfil['di'])->get();
I hope you understand, I tried to do a foreach but I can't do it, also I don't know if it's the correct way..
I'm trying to do it in the following way but it keeps showing me a single result..
public function show($id)
{
// Busco el perfil
$perfil = lista::where('di', $id)->get();
foreach($perfil as $perfi){
//con el resultado de la busqueda muestro el perfil
$mostrarperfil = Perfiles::where('id', $perfi->di)->get();
}
return view('products.show', compact('mostrarperfil'));
}
I need to create an if with several conditions, there are many examples in the forum but I can't find any that work for me. I need the if to check two conditions, the first verifying if it $user_id
is equal to auth()->user()->id)
if it does not match, I will try the second option, which would be to verify if $privacidad is null in addition to point greater than 100, I hope I have explained myself well.
This is what I'm trying to do but is this syntax correct? "It's just a bad example for you to understand me better"
@if( ( $user_id == auth()->user->id ) or ( $private = null and point >=100 ) )
I am using an autocompletion script for the search engine it works correctly but I have a problem that when I try to use any shortcut with the # symbol it copies it to the search engine input, for example I try to access a div with <a href="#div1"><a>
and it copies it to the input. .
script
<script>
$(document).ready(function(){
$('#nombre').keyup(function(){
var query = $(this).val();
if(query != '')
{
var _token = $('input[name="_token"]').val();
$.ajax({
url:"{{ route('autocomplete.fetch') }}",
method:"POST",
data:{query:query, _token:_token},
success:function(data){
$('#List').fadeIn();
$('#List').html(data);
} }); } });
$(document).on('click', 'li', function(){
$('#nombre').val($(this).text());
$('#List').fadeOut();
});
});
</script>
Controller "I think the problem is in the Script but I share the Controller in case it is necessary"
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class AutocompleteController extends Controller
{
function index()
{
return view('autocomplete');
}
function fetch(Request $request)
{
if($request->get('query'))
{
$query = $request->get('query');
$data = DB::table('products')
->where('nombreyape', 'LIKE', "%{$query}%")
->get();
$output = '<ul class="dropdown-menu" style="display:block; position:absolute">';
foreach($data as $row)
{
$output .= '
<li><a href="#">'.$row->nombreyape.'</a></li>
';
}
$output .= '</ul>';
echo $output;
}
}
}
More extensive explanation of my failure
The problem is that if I try to call any div using the #, for example, <a href="#div1"><a>
or <a href="#loquequieras"><a>
it doesn't matter if the div doesn't exist, but when I click on the link, the text copies it to the search engine, for example, <a href="#div1"><a>
it copies me to the div1 search engine.
I try to upload several images at the same time and what is to upload it to the server uploads it but it only saves the name of the first one in the database, the others resize it and save them in the folder but it does not insert the name into the database what can be owed?
Form
{!! Form::open(array('route'=>'posts.store','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('user_id', auth()->user()->id) }}
{{ Form::hidden('di', 'di') }}
{{ Form::label('image', 'Imagen') }}
{!! Form::file('profile_image[]', array('multiple' => true)) !!}
{{ Form::submit('Guardar', ['class' => 'btn btn-sm btn-primary']) }}
{!! Form::close() !!}
Controller
public function store(Request $request)
{
$post = Post::create($request->all());
$files = $request->file('profile_image');
foreach ((array) $files as $file)
{
if($file) {
//get filename with extension
$filenamewithextension = $file->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $file->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.time().'.'.$extension;
$post->fill(['file' => $filenametostore])->save();
//Upload File
$file->storeAs('public/images', $filenametostore);
$file->storeAs('public/images/mini', $filenametostore);
$file->storeAs('public/images/minin', $filenametostore);
//Resize image
$thumbnailpath = public_path('storage/images/mini/'.$filenametostore);
$img = Image::make($thumbnailpath)->resize(null, 200, function($constraint) {
$constraint->aspectRatio();
});
$img->save($thumbnailpath);
//Resize image perfil
$thumbnailpat = public_path('storage/images/minin/'.$filenametostore);
$imgn = Image::make($thumbnailpat)->resize(null, 200, function($constraint) {
$constraint->aspectRatio();
})->resizeCanvas(200, 200 );
$imgn->save($thumbnailpat);}
}
return back()->with('info', 'Foto añadida correctamente');
}
Where this error?? When the empty variable is full it works perfectly but when it is empty it does not show me the other part or the image or the link where the error could be?
@foreach($products as $product)
<div>
@foreach($product->fphoto as $produ)
@if(empty($produ->id))
<a href="{{ route('products.show', $product->id) }}">
<img src="{{asset('img/profile.jpg')}}" alt="">
@else
<a href="{{ route('products.show', $product->id) }}">
<img src="{{ $produ->photo }}" alt="">
@endif
@endforeach
<h3>{{ $product->nombreyape }}</h3></a>
<p>Textop>
</div>
@endforeach
I'm trying to modify the Policies function which is currently this:
public function pass(User $user, Product $product)
{
return $user->id == $product->user_id;
}
Let's see if I explain myself correctly... I have some products that, when trying to access them, check that the id
user 's is the same as the user_id
product that they are trying to access up to here, everything is perfect, but what I also need is to create some products that are accessible to them by all users without restrictions that all users regardless of their id have access to them. For example, I have thought about using user_id 1
it in all products without restriction, but I don't know how to make the function of free access to all products, do you have user_id 1
any ideas?
How can I limit a @foreach ? I use a foreach to show the data of a related table and I would like to limit the results of that foreach I try to use this but it gives me an error
{{$count = 0;}} @foreach($product->post as $produc) {{if($count == 4) break; }}
///contenido
{{$count++;}}
any ideas ?
is this controller code correct?
$product = Product::find($id)->with('dsa');
using only this
$product = Product::find($id);
It works correctly for me but when I try to add the related table I can't do it and I tried in a thousand ways and nothing... I have related tables both in my innoDB database and in the models...
MODEL
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'id', 'nombre', 'apellidos', 'nombreyape', 'nacimiento', 'fallecimiento', 'user_id'
];
public function dsa()
{
return $this->hasMany('App\Dsa', 'di', 'id');
}
}
Dsa model namespace App;
use Illuminate\Database\Eloquent\Model;
class Dsa extends Model
{
protected $fillable = [
'user_id', 'di', 'nombre', 'apellidos', 'enlace'
];
public function product()
{
return $this->belongsTo('App\Product', 'id', 'di');
}
}
using this controller
$product = Product::with('dsa')->find($id);
It works for me to display the entire table using
{{ $product->dsa }}
and it returns me this exactly
[{"id":10,"user_id":21,"di":39,"name":"pepe","surname":"lorenzo","link":"example","created_at":"2018 -10-06 14:50:37","updated_at":"2018-10-06 14:50:37"},{"id":11,"user_id":21,"di":39,name" :"juan","surname":"dented","link":"example","created_at":"2018-10-06 14:51:11","updated_at":"2018-10-06 14: 51:11"}]
but if i try to call something just like the name for example
{{ $product->dsa->nombre }}
I get the following error Property [name] does not exist on this collection instance.