Laravel query builder using hasMany output - laravel

This is my syntax in blade file :
{{ $product->filters->where("filter_id","10") }}
And my output for this looks like :
{"3":{"id":7153,"product_id":"1","filter_id":"10","data":"Kajaria","created_at":null,"updated_at":null}}
And I want to extract only a particular data i.e. Kajaria.
I'm getting confused in writing syntax.
I had already tried these syntaxes :
{{ $product->filters->where("filter_id","10")->data }}
{{ $product->filters->where("filter_id","10")['data'] }}

Try something like this:
{{ $product->filters->where("filter_id","10")->first()->data }}
The thing is $product is a collection and you need to get property of just one object, so first() method returns first object from a collection which you can use.

What I understand is that you want to get a filter for a product. So, try this.
Product::with('filters')->where('filter_id', 10)->first()->data;

Related

Auth::user on Laravel with different table

I have two additional column for login with different tables. So, I used 3 tables for login with output like this:
When I want to use username its simply write it like this {{ Auth::user()->username }} so I get value KASIR WEB. But it not work with kd_lokasi and kd_shift. Where I should register it? Or how I can get kd_lokasi and kd_shift value and paste it on view? Thank you for helping me.
Table :
$kd_lokasi = DB::table("tb_kasir_lokasi")->where("vTampil", 1)->get()
->pluck("nm_lokasi", "kd_lokasi");
$kd_shift = DB::table("tb_kasir_shift")->where("vTampil", 1)->get()
->pluck("nm_shift", "kd_shift");

How can I get created_at month name on laravel blade?

I have created_at field. on my database, and I need to get the month name, for example;
...
{{\Carbon\Carbon::now()->monthName}}
...
So how can I do it for $value->created_at?
I tried some solutions but didn't get the month name.
created_at already a Carbon instance, so you can do that by :
{{ $value->created_at->format('F') }}
Or,
{{ \Carbon\Carbon::parse($value->created_at)->format('F') }}
F - A full textual representation of a month (January through
December)
M - A short textual representation of a month (Jan through Dec)
Translate to other language : You can use carbon to format your local language, as for Russian language :
#php
\Carbon\Carbon::setLocale('ru');
#endphp
{{ \Carbon\Carbon::parse($value->created_at)->translatedFormat('F') }}
// output : ноябрь
you can try as created_atalready a Carbon instance, so you can use this
{{ $value->created_at->monthName }}
make sure $value is a instance of Illuminate\Database\Eloquent\Model;
if it is not a instance of laravel model then you can do like this
{{ \Carbon\Carbon::parse($value->created_at)->monthName }}
you can write by
{{ date('F', strtotime($value->created_at)) }}
you can get the name of month by the following code:
{{ date("F", strtotime($value->created_at)) }}
you can get more details from (php website)

Using Eloquent with Spaces in Table Columns

I am using Laravel and have a table that has columns with spaces. I can't change the names of the columns. I am trying to use Eloquent and have tried many things to get this to work but can't. I thought this would be the answer:
{{$provider->'Entity Type Code'}}
Thanks
You can use {} brackets
{{ $provider->{'Entity Type Code'} }}
Or use another variables
$variable_name = 'Entity Type Code';
{{ $provider->$variable_name }}

How to display I like it laravel format created_at timestamp

echo `{{$result->created_at}}`
result is 2018-06-23 08:05:14
but I want to echo just date. not time
I mean echo only 2018-06-23 not 2018-06-23 08:05:14
It's a Carbon instance. The recommended option is simply
{{ $result->created_at->toDateString() }}
Carbon docs: https://carbon.nesbot.com/docs/#api-formatting
This is pretty simple, directly escape date to whichever format you want like in the example below
{{ date('Y-m-d', strtotime($result->created_at))}}
You may handle this on the PHP side by just using substr:
echo {{ substr($result->created_at, 0, 10) }}
There are also ways that you could handle the formatting on the MySQL side, e.g. by using DATE_FORMAT:
DATE_FORMAT(created_at, '%Y-%m-%d')
you can use laravel Accessor by put the following method inside your model:
public function getCreatedAtAttribute($date)
{
return $date->format('Y-m-d');
}

Laravel returns undefined offset 0 when trying to print from array

Controller
$new_products = Product::with('images')->orderBy('created_at', 'desc')->take(10)->get();
return view('site/home', compact('new_products'));
View
{{ $new_product->images[0]->image_name }}
This gives me error undefined offset 0. How do i print the values of images?
values returned on dd($new_products)
If you want to get the first item, you can use the first method.
{{ $new_product->images->first()->image_name }}
You also have the offsetGet method to get an item at a given offset.
{{ $new_product->images->offsetGet(0)->image_name }}
You can also loop through the collection though and do this:
#foreach ($new_product->images as $image)
{{ $image->image_name }}
#endforeach
Note: The first two method will only work if your products have images. If they don't, then Laravel will return an empty collection. The third method of looping through the collection will work in all cases.
If you want to make sure that products have images, you can use the has method.
$new_products = Product::with('images')->has('images')->orderBy('created_at', 'desc')->take(10)->get();
This will only return products that have at least one image.
Docs on collection methods: http://laravel.com/docs/master/collections#method-first
Docs on relationships: http://laravel.com/docs/5.1/eloquent-relationships
From the out it appears that $new_product is also an array of two items,
{{ $new_product[0]->images[0]->image_name }}
Edit: Thomas Kim has a better answer
Your relation (images) is a collection object, if you all() method on that then you'll get the underlying array so then you can access any item from array using the index:
{{ $new_product->images->all()[0]->image_name }}
Also toArray() method will work:
{{ $new_product->images->toArray()[0]['image_name'] }}
So, either pass the array to your view like $new_product->all() or loop it. You may check the documentation here for more information about Collection object in Laravel.

Resources