i have a model called employees whiche has a name in latin and arabic, so i used in my migration this:
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('picture');
$table->string('name');
$table->string('name_ar');
$table->string('name_fr');
$table->string('position');
$table->string('position_ar');
$table->string('position_fr');
$table->timestamps();
});
}
And i want to display the correct name in the language depending on the localization, for example if the localization is 'fr' i should display name_fr or name_ar for 'ar',
i am using a localization package.
for now i came up with this:
#if (Lang::locale() == 'ar')
<h4 class="text-white"> {{ $employee->name_ar }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_ar }}</p>
</div>
#elseif(Lang::locale() == 'fr')
<h4 class="text-white"> {{ $employee->name_fr }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_fr }}</p>
</div>
#else
<h4 class="text-white"> {{ $employee->name }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position }}</p>
</div>
#endif
is there a better way to display the name in the correct language, for example in a controller or in the Lang directory ?.
you could achieve that with a one-line like this
{{ $employee->{'name_' . app()->getLocale()} ?? $employee->name }}
thanks to #Tim Lewis for mentioning it.
Related
I'm am trying to get data from a database table and passing it to the modal but it is saying the array I am passing in undefined. Here is my Controller:
public function displayLocNotesForModal() {
$notesLoc = Note::all();
return view('/components/callCenter/modalLocNotes', ['notesLoc' => $notesLoc]);
}
Here is my Route:
Route::get('/components/callCenter/modalLocNotes', 'App\Http\Controllers\CallCenter\NoteController#displayLocNotesForModal');
Here is my modal:
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-headline">
Location #{{ $title }} Notes
</h3>
<div class="mt-2">
<p class="text-sm leading-5 text-gray-500">
{{-- {{ $slot }} --}}
#foreach($notesLoc as $notes)
#if($notes == $title)
works
#endif
#endforeach
</p>
</div>
</div>
I think it should be like below, assuming the components folder is in your resources/views folder
return view('components.callCenter.modalLocNotes', ['notesLoc' => $notesLoc]);
Also providing a link to docs Laravel nested view directories
I want to go to specific page after click show button but after click show will get this error Undefined offset: 1
web.php
Route::get('/product/{id}', 'ProductController#show')->name('product.show');
ProductController.php
public function show($id)
{
$product = Product::find($id);
return view('product.show')->with(compact('product'));
}
home.blade.php
<div class="row">
#foreach($products as $product)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<div class="card-body">
<h4 class="card-title">
{{ $product->name }}
</h4>
<h5>{{ $product->price }}</h5>
<p class="card-text">{{ $product->description }}</p>
<td><a class="btn btn-primary" href="{{ route('product.show', $product->id ) }}">Show</a></td>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
#endforeach
</div>
Your are doing wrong in blade file. Note here php array start from 0. So array is [0,1,2,3] and database id is start from 1.
this is the correct link
{{ route('product.show', ['Product' => $product->id]) }}
You have to use like below code
{{ route('product.show', ['id' => $product->id]) }}
return view('product.show', compact('product'));
And in your show.blade.php you have to use like below
{{$product->id}} or {{$product->name}} //etc //But not use foorloop
The find method will return only one item. You can't use the loop on it.
Try the following code.
web.php
Route::get('/product/{product}', 'ProductController#show')->name('product.show');
ProductController.php
public function show(Product $product)
{
return view('product.show')->with(compact('product'));
}
home.blade.php
<div class="row">
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<div class="card-body">
<h4 class="card-title">
{{ $product->name }}
</h4>
<h5>{{ $product->price }}</h5>
<p class="card-text">{{ $product->description }}</p>
<td><a class="btn btn-primary" href="{{ route('product.show', $product->id ) }}">Show</a></td>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
</div>
you are in wrong blade
you are returning product.show blade in your controller:
return view('product.show')->with(compact('product'));
but you put home.blade code!!!
put the right blade code
show.php: Why not try it $_ Get["id"]
#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
HTML Code
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">{{ $post->owner_user->username }}</p>
</div>
</div>
#endforeach
Laravel code
public function testpage()
{
return Posts::with(['OwnerUser','like'])->get();
}
when I just return Posts table, it has column owner_user with user information in it.
relations work, but somehow I get "Null" as a result in {{ $post->owner_user->username }} when I return it in HTML code.
because that is not a single object try
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">#(foreach $post->owner_user as $user){{$user->username }}#endforeach</p>
</div>
</div>
#endforeach
I'm trying to get some data from two different tables,
So it does look like this now:
But I don't want the empty fields (Globale-Moderator, Moderator and Proef-Moderator) to be shown. How can I do this whit this code;
Controller:
public function ForumTeam()
{
$roles = Role::where('id', '>', '4')->orderBy('id', 'DESC')->get();
return View::make('team')->with('roles', $roles);
}
View:
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->user as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};"><strong>{{ $user->username }}</strong></h4>
<p>{{ $user->usertitle }}</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
So I want to hide the not filled in fields, how should I do dis propperly?
My other option is to show some text inside of the 'empty' field. Like 'There is nobody with this rank'.
Thanks!
You can/should be able to use:
#foreach($roles as $role)
#if($role->user != null || !empty($role->user->name))
#endif
#endforeach
You can then check to see if the role->user is NOT NULL OR the name of the role is not empty.WHERE name is the name of the role, i.e. "Admin", "Moderator"
Alternatively, try:
#foreach($roles as $role)
#if(count($role->user) >= 1)
// Do stuff
#endif
#endforeach
Since you are getting User which has a one-to-many relationship, therefore there will be more than one user.
You could add an if statement after the first #foreach, to check if the role
has users,
#foreach($roles as $role)
#if(isset($role->user))
...
#endif
#endforeach