how to select many data with array in laravel - php

my controller
public function lemari_surat_alpha(){
$show_dinas = dinas::all();
$show_surat = surats::all();
return view('frontend.daftar_surat_perdinas', compact('show_dinas','show_surat'));
}
I don't know to show many data with array for selecting in Model

You're basically trying to group the results in alphabetical order within view. Why not arrange your results first and then display it.
Try this.
public function lemari_surat_alpha(){
$show_dinas = dinas::all();
// Uppercase alphabet keys collection
$alphabets = collect(range('A', 'Z'))->flip();
// Grouped results
$results = surats::all()->groupBy(function($item) {
return strtoupper($item->nama_surat[0]);
});
// Merge results with alphabets
$show_surat = $alphabets->map(function($item, $key) use ($results) {
return $results->has($key) ? $results->get($key) : null;
});
return view('frontend.daftar_surat_perdinas', compact('show_dinas','show_surat'));
}
This will return grouped results. Like this
then you can iterate over the collection in your blade file like this
#foreach ($show_surat as $alphabet => $surats)
<div class="entry-image">
<a href="#" class="entry-link" target="_blank">
{{ $alphabet }}
</a>
</div>
#if (is_null($surats))
No Results
#else
<ul style="margin-left: 20px;">
#foreach($surats as $surat)
<li style="font-size: 18px;">{{ $surat->nama_surat }}</li>
#endforeach
</ul>
#endif
#endforeach

VIEW:
#foreach( $show_dinas as $show_dina){
//do something
#endforeach

Related

how to show multiple category name from checkbox value in eloquent relationship laravel

firstly, i'm a newbie in laravel.
i insert a array data as a string in server.
column name "category_id" & value like "1,2,3,4".
Now i just want to show these id's category name with eloquent relationship.
Using laravel latest version.
view page
{{$category_var = explode(',',$post->category_id)}}
#foreach($category_var as $category)
#if($category)
<span class="badge mb-3">{{$post->category->category}} </span>
#endif
#endforeach
class page
public function category(){
return $this-> belongsTo(Category::class);
}
Controller page
public function index()
{
$posts = post::paginate(9);
return view('pages/home',compact('posts'));
}
anything else need just ask me.
Thanks
just use the benefits of models and relation like below:
#foreach($posts as $post)
#foreach($post->category as $category)
<span class="badge mb-3">
{{ $category->(what ever you want from category model) }}
</span>
#endforeach
#endforeach

Getting a value from another two models at the same time in Laravel

Basically I have a function within a controller which is
public function get_all_clubs($city)
{
$city = ucwords($city);
$city_des = Town::where('town', $city)->value('town_desc');
$clubs = Club::get()->where('city', $city)->where('profile_enabled', '1');
$events = Event::where('club_id', $clubs->first()->id)->get();
$lowercase_city = Str::lower($city);
return view('frontend.pages.allclubs', compact('clubs', 'lowercase_city', 'city_des', 'events', 'flag'));
}
The events and the club tables are related using an id as you might have figured out. I have been able to get the events associated to a city based on the club_id. In my view I am echoing each event using the
#foreach Laravel syntax. The problem however is I need to create an hyperlink for each event on on my view. The routes are structred in the way below:
<a href="{{ route('event.show_event', [$club->slug, $event->slug]) }}">
Effectively, I need to pass the $event->slug which I already have and also the $club->slug which is what my question is, how do I get hold of this variable during my #foreach loop. Please note I am an intermediate Laravel/php developer.
My view is as follows
#foreach ($events as $event)
#if ( ( (strtotime((str_replace('/', '-', $event->date)))) >= (strtotime("today")) ) && ($event->event_private))
#php $flag='1'; #endphp
<li>
<div class="widget-posts-body">
<h6 class="widget-posts-title"> {{$event->title}} </h6>
</div>
</li>
#endif
#if ( ($loop->index) == '10' )
#break
#endif
#endforeach
#if($flag != '1')
<li>
<p> There are no events live at this time. Please check back later</p>
</li>
#endif
make a one to one relation in event model to club like this:
/**
* #return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function club()
{
return $this->hasOne(Club::class);
}
then call it in view like this:
<a href="{{ route('event.show_event', [$event->club->slug, $event->slug]) }}">

Add elements to an array inside a foreach loop

'm trying to get non duplicated in My blade tasks using this Code
I have Edited My Question So i added Controller and full blade Code
My Controller
$posts2 = Path::with(['pathtags' => function ($q) use ($TagArray)
{$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
->with('tasktags');
}]);
}])->where('id', '=', 1)->get();
My Blade
#foreach ($posts2 as $item)
<h2> {{$item->name}}</h2>
#foreach ($item->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#php $a=array(); #endphp
#if (in_array($Task->task_name,$a))
<li> Task :: {{ $Task->task_name }} </li>
#php
array_push($a,"$Task->task_name");
#endphp
#else {
<li> Task :: Not Found </li>
}
#endif
#endforeach
#endforeach
#endforeach
you are emptying the array in each iteration. move the initialization of array before the foreach loop. also, the whole logic is wrong. you are checking if the item exists in the array, and if so, add it again.
#php $a=array(); #endphp
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->task_name,$a))
<li> Task :: {{ $Task->task_name }} </li>
#php
array_push($a,$Task->task_name);
#endphp
#else
<li> Task :: Duplicated </li>
#endif
#endforeach
Change this
array_push($a,"$Task->task_name");
with
array_push($a, $Task->task_name);
// or
$a[] = $Task->task_name;
In stock PHP you can use array_count_values to get the count of each array items.
Then use array_diff or array_intersect to get the different unique or duplicated items.
Array_keys return the values from original array.
$arr = ["one", "one", "two", "two", "three"];
$count = array_count_values($arr);
echo "duplicates \n";
var_dump(array_keys(array_diff($count, [1])));
echo "uniques \n";
var_dump(array_keys(array_intersect($count, [1])));
https://3v4l.org/DKIbZ

Split string in Laravel Framework

How do I split string and display it in a table in Laravel Framework? I fetch data from my database that consists of one column but in a string such as { 1234, normal, r4r3r2 }. I want to split it into three different parts/values by commas and display it in a table of three columns.
For now, I only can display the data without splitting the them.
My HomeController:
public function index()
{
$test = Test::all();
return view('home')->with('test', $test);
}
My home.blade.php:
<ol>
#foreach($test as $row)
<li>{{ $row->data }}</li>
#endforeach
</ol>
First explode the $test variable to get Array,
<ol>
#foreach(explode(',',$test) as $row)
<li>{{ $row }}</li>
#endforeach
</ol>
After explode using foreach single key we can access from the array.
I hope it helps.
You can explode you string in blade file like this
#foreach(explode(',', $row->data) as $fields)
<li>{{$fields}}</li>
#endforeach
And using of your model you can also done like this
class Test extends Model
{
public function getDataAttribute()
{
return explode(',', $this->data);
}
}
it's return your data explode with comma separated array.

Laravel displaying data from 2 tables

Let me explain situation first.
I am on the page with list of skills available such as "Plumber","Carpenter" and "Painter", when I click on one of those skills I want to get a list of handymans that have that skill and once clicked on a handyman I get full details about him.
Skills are displayed however when I click on one of the skills it doesn't want to retrieve any data. Both tables "handymen" and "skills" have many to many relationship and also there is a junction table. What am I doing wrong here?
Route::group(['middleware' => ['web']], function () {
Route::get('home', 'HandymanController#home');
Route::get('search', 'HandymanController#search');
Route::get('details/{handyman}', 'HandymanController#details');
Route::post('assignjob', 'HandymanController#assignJob');
Route::get('addjob', 'HandymanController#addJob');
Route::post('addjform', 'HandymanController#addjForm');
Route::get('jobs', 'HandymanController#jobs');
Route::get('jobsdetails/{jobId}', 'HandymanController#jobsdetails');
Route::get('deletejob', 'HandymanController#deleteJob');
Route::post('deletejform', 'HandymanController#deletejForm');
Add Job View:
#extends('layouts.master')
#section('title', 'Add Job')
#section('header2')
<ul>
<li>Assign job</li>
</ul>
#show
#section('content')
<h1>Handyman details</h1>
<ul>
#foreach ($handymen as $handyman)
<a href= "{{ url("HandymanController#details", $handyman->id) }}">
{{$handyman->name}}
</a>
#endforeach
</ul>
Controller:
function search()
{
$skills = Skill::all();
return view('layouts/search',['skills' => $skills]);
}
function details($skillId)
{
$skill = Skill::find($skillId);
$handymen = $skill->handymen;
return view('layouts/details', ['handymen' => $handymen]);
}
According to your edited question, first you list all the skills in your search view. So, in your search view, you would have something like this:
#foreach ($skills as $skill)
<a href= "{{ action("HandymanController#addjForm", $skill->id) }}">
{{ $skill->name }}
</a>
#endforeach
So you have to define in your routes file this route:
Route::post('addjform/{skill}', 'HandymanController#addjForm');
This will point to Handyman Controller, where you will list all handymen that have that skill:
public function addjForm(Request $request, Skill $skill)
{
$handymen = $skill->handymen;
return view('layouts/skilledHandymen', ['skill' => $skill,'handymen' => $handymen]);
}
For this to work, you have to define in Skill Model, the association:
public function handymen()
{
return $this->belongsToMany(Handyman::class,
'handyman_skill',
'skill_id',
'handyman_id');
}
The controller will point to a view where you will list all handymen that have such skill:
In your case, it would be easier if you define an association in Skill model that links to Handyman:
#foreach ($handymen as $handyman)
<a href= "{{ action("HandymanController#details", $handyman->id) }}">
{{ $handyman->name }}
</a>
#endforeach
When you choose a handyman, it will take you to Handyman controller details:
function details(Request $request, Handyman $handyman)
{
return view('layouts/details', ['handymen' => $handymen]);
}
For this you will define this route:
Route::get('/handyman/{handyman}', 'Handyman#details');
And this will point you finally to the details of chosen handyman, and you can show his details:
<p>{{ $handyman->id }}<p>
<p>{{ $handyman->name }}</p>
The thing that is important to understand here is that you will first have a Collection of skills that will lead you to a Collection of Handymen, and not just a single one. After you choose a Handyman from the second list you will be able to show his details. If you try to jump over this step you will be trying to show details of a list.
Hope this helps...

Categories