I have a small problem in my laravel application. In my blade file, I would like to extract data from timestamp sql query result like using twig filter in Symfony.
Here is what I would like to do (here I used twig filter syntax)
<div class="day">{{ $post->published_at|date("d") }}</div>
<div class="month">{{ $post->published_at|date("m") }}</div>
How to do the samething in Larave blade file ?
Note that I tried to use the syntaxe below but it is not working for me
<div class="day">{{ $post->published_at->format('d') }}</div>
<div class="month">{{ $post->published_at->format('M') }}</div>
My project used laravel 8.
Is there somoene can help me please.
I think you can use Carbon.Here i assume your published_at format in db is timestamp or datetime
Carbon\Carbon::parse($post->published_at)->format('d')
As suggested by Alberto Sinigaglia in comment,If you cast published_at in your model like below then you can easily format without manually parsing.
In your model
protected $casts = [
'published_at'=>'date' // or datetime
];
then in your view
{{$post->published_at->format('d')}}
Use strtotime and date functions:
<?php
$time = strtotime($post->created_at);
$day = date('D', $time);
?>
<div class="day">{{ $day }}</div>
Related
Hi am trying to use diffForHumans in laravel blade but its not working.please help`enter code here
Heres my code
#foreach($students as #student)
$time=$student->created_at->diffForHumans()
#php echo $time;#endphp
#endforeach
$student->created_at is a string, hence you can't call diffForHumans() function on it. Instead, you should parse it via Carbon instead. Here's how to do it
#foreach($students as #student)
#php
$time= \Carbon::parse($student->created_at)->diffForHumans()
#endphp
//Now do what you need to do
#endforeach
Also, you can't just write php code inside blade. If you do that, you need to contain the code inside #php tags as shown above. Although, it's not really clean to write php code in blade. With that being said, if you just want to display the time, here's how I would do it
#foreach($students as #student)
<p>Created At : {{ \Carbon::parse($student->created_at)->diffForHumans() }}</p>
#endforeach
All You need is to parse the date using Carbon like this
{{ Carbon\Carbon::parse($student->created_at)->diffForHumans() }}
I'm trying to build Laravel project that will have a multi-select dropdown with list of categories. Selecting category should reload the page and apply filter.
"scopeFilter" in my model is actually doing filtering, but here i just need to find a way to properly form URL. The problem is that this code i have:
<form id="cats-form" action="#" method="GET">
<select multiple class="chosen-select" name="test[]">
#foreach($categories->get() as $cat) //this loop goes through existing categories in the system
#php
//if category is part of URL, pre-select it in the select:
$arr = request()->all();
$selected = '';
if(array_key_exists('test', $arr)) {
$testArr = $arr['test'];
$selected = in_array($cat->id, explode(',', $testArr)) ? 'selected' : '';
}
#endphp
<option {{ $selected }} value="{{ $cat->id }}">{{ $cat->name }}</option>
#endforeach
</select>
</form>
<script>
$(".chosen-select").chosen({ })
$('.chosen-select').on('change', function(evt, params) {
$('#cats-form').submit();
});
</script>
Is actually giving me this URL:
http://localhost:11089/?test%5B%5D=19&test%5B%5D=5
While i actually need this:
http://localhost:11089/?test=19,5
I guess it's a trivial problem to solve for someone with better knowledge of Laravel, can you tell me pls what i'm doing wrong here?
This is rather how the language or framework or library reads an URL query string. Assuming that your prefered way of URL string is a valid format (as I usually see the format ?arr[]=val1&arr[]=val2 and never see the second format that you preferred), both query string formats should be acceptable to PHP.
As Yarin stated that the best way PHP reads an array is the first method, you don't have to worry too much because the decoded URL query string is exactly in the first format.
I've been working with Laravel livewire for a while now, I've a nested components, which is product list for my site and inside that list I've another component for adding product to wishlist. As per documentation stated here , it says
"Similar to VueJs, if you render a component inside a loop, Livewire has no way of keeping track of which one is which. To remedy this, livewire offers a special "key" syntax:"
Like this:
<div>
#foreach ($users as $user)
#livewire('user-profile', $user, key($user->id))
#endforeach
</div>
Here is my code snippets from my project.
<div>
#foreach($products as $product)
<div class="product-box white-bg mb-8" data-dusk="product">
{{-- here im passing product id as param in key(), 'productList' is a static value for a variable of mount(). --}}
#livewire('desktop.wish-list-add', $product, key($product->id), 'productList')
<div class="product-content d-flex justify-content-between align-items-center p-5">
...............
#endforeach
{{ $products->links() }}
</div>
The issue is when I try to pass $product->id as param for key(), it gives error
key() expects parameter 1 to be array, integer given
But the doc clearly shows that we have to pass id as param. Has anyone faced this issue so far?
#livewire('photos.photo-wire', ['photo' => $photo], key($photo->id))
instead of
#livewire('photos.photo-wire', ['photo' => $photo, key($photo->id)])
because that will generate the error:
key() expects parameter 1 to be array, integer given
okay, I found the solution ( however it doesn't make sense to me , but it works :/ )
You have to pass other parameters for mount() like this:
#livewire('desktop.wish-list-add', 'productList', $product->id, key($product->id))
Instead of this:
#livewire('desktop.wish-list-add', $product, key($product->id), 'productList')
I want to display the name of a user alongside his notes. I have two models the user and the Notes Model. The Notes Model has the id of the user and the relationship is good.
How do I fetch the name of a user using their id in the view?
I have used plain php between the <span> tags </span>
#foreach($notes as $note)
<div class="card-body">
<div class="d-flex justify-content-center">
<span><strong><?php $userid=$note->user_id;
$user = App\User::find($userid);
$name = $user->name;
echo $name;?>:::</strong></span>
<span class="success">{{$note->note_title}}</span>
</div>
<div class="form-panel"> {!! html_entity_decode($note->note_body)!!}</div>
</div>
#endforeach
Am getting the correct name but I guess that's not the correct way in laravel.
If your model relationships are configured correctly the user model doesn't need to be fetched in view.
You can get the users name through the one to many relationship.
Take a look here
https://laravel.com/docs/5.8/eloquent-relationships#one-to-many-inverse
Looking at my own question two years later and I cannot believe that was me, anyway its progress.
Actually that`s easy {{$note->user->name}}
Working on an app whereby am capturing some input fields using bootstrap datepicker. Am displaying the format in dd/mm/yy format to the user which works fine. On the backend (build in Laravel PHP), I need to convert it to yy-mm-dd format which is the required format when storing the date field in the API.
In the controller when I dump the data I get 28/01/2019 but when I convert it I get 1970-01-01. What could be the issue?
Markup
<div class="form-line registar love {{ $errors->has('dob') ? ' has-error' : '' }}">
<input type="text" placeholder="Date of Birth *" class="form-input dateTextBox" name="dob" id="dob" value="{{isset($user->dob) ? $user->dob : old('dob') }}" required>
</div>
Javascript Logic (Bootstrap datpicker)
var maxBirthdayDate = new Date();
maxBirthdayDate.setFullYear( maxBirthdayDate.getFullYear() - 18 );
maxBirthdayDate.setMonth(11,31);
$( function() {
$( "#dob" ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd/mm/yy',
maxDate: maxBirthdayDate,
yearRange: '1900:'+maxBirthdayDate.getFullYear(),
});
});
Controller Logic
public function validateDate(Request $request){
//dd($request->dob);
//28/02/2019 after dd() above
//Convert
$dobCv = date("Y-d-m", strtotime($request->dob));
dd($dobCv);
//1970-01-01 after dd() above
}
Why don't you use Laravel build in Carbon
$date = Carbon::createFromFormat('d/m/Y', $date);
Then you can do $date->toDateTimeString(); or whatever you want to do with the Carbon date object
You can simply use Carbon :
$dobCv = Carbon::createFromFormat('d/m/Y', $request->dob)->format('Y-m-d');
And don't forget the import :
use Carbon\Carbon;
Encode it in json format and send it to the controller
Changing input type text into date might help you.
eg: <input type="date" .......>
You have the problem because forward slash (/) signifies American M/D/Y formatting in strtotime and in your example 28/02/2019 28 is month. So strtotime returns false. I recommend you to use Carbon library as was said above or change / with - if you want your way:
$dobCv = date("Y-d-m", strtotime(str_replace('/', '-', $request->dob)));