So to break that title down...
I have 2 tables -> bosses (parent table) & employees (child)
bosses hasMany employees and employees hasOne boss
What I need to accomplish is to list all the bosses in an element and "select" the current boss. And if I change the boss from the list, it should change the id of the element to the new selection.
#foreach ($bosses as $boss)
<option value="{{ $boss->id }}" {{ $boss->id == ($employee->boss_id) ? 'selected' : '' }}>
{{ $boss->full_name }}
</option>
#endforeach
Controller
public function details($id){
$employee = Employee::find($id);
$bosses = Boss::get();
return view('employees.details',[
'employee' => $employee,
'bosses' => $bosses,
] );
}
Right now I'm still setting everything up so the search isn't working.
With the above code I'm getting all of the $publishers listed by name but it is not selecting the the boss associated the the boss_id foreign key.
Thanks in advance!!!
You could use the helper method old() for this.
For example:
<select name="boss_id>
#foreach ($bosses as $boss)
<option value="{{ $boss->id }}" {{ (old('boss_id', $boss->id) == $employee->boss_id) ? 'selected' : '' }}>
More info: https://laravel.com/docs/9.x/validation#repopulating-forms
I have an issue where i want to show the current brand in a select, on the edit page of a vehicle mode.
The select has to contain all the brands, so it is possible to choose another brand on editing the model.
I have this in my vehicleModelsController edit:
$vehicle_brands = VehicleBrand::all();
$selected_vehicle_brand = VehicleBrand::first()->vehicle_brand_id;
return view('vehicleModels.edit', compact(['vehicleModel', 'vehicle_brands'], ['selected_vehicle_brand']));
And in my edit.blade file i have the following select:
<select class="form-control" name="vehicle_brand_id">
#if ($vehicle_brands->count())
#foreach($vehicle_brands as $vehicle_brand)
<option value="{{ $vehicle_brand->id }}" {{ $selected_vehicle_brand == $vehicle_brand->id ? 'selected="selected"' : '' }}>
{{ $vehicle_brand->brand }}</option>
#endforeach
#endif
</select>
And it works just fine with editing, but it does not show the current value as selected, it shows the first value in the brand table.
I have Brand1, Brand2, Brand3 and Brand4 as test values in the brand table, but no matter what brand the model is related to, it shows Brand1 in the select on the edit page.
Any help is greatly appreciated!
In the controller you are assigning value of VehicleBrand::first()->vehicle_brand_id to $selected_vehicle_brand and then you are comparing it with id field in {{ $selected_vehicle_brand == $vehicle_brand->id ? 'selected="selected"' : '' }}
If there is not column/field named vehicle_brand_id on VehicleBrand model then VehicleBrand::first()->vehicle_brand_id will be null. So when you compare $selected_vehicle_brand == $vehicle_brand_id it will be like null == $vehicle_brand->id which will be false for any id value.
So it should either be (in Controller)
$selected_vehicle_brand = VehicleBrand::first()->id;
//Here we are hard coding the value to be the id of first record of VehicleBrand
//However in practice it should come from either request/route param eg: $id received via the request/route param
//Or from VehicleModel being edited eg: $vehicleModel->vehicle_brand->id
And {{ $selected_vehicle_brand == $vehicle_brand->id ? 'selected="selected"' : '' }} in the view
Or, if vehicle_brand_id column/field exists on VehicleBrand, it should be (in Controller)
$selected_vehicle_brand = VehicleBrand::first()->vehicle_brand_id;
and {{ $selected_vehicle_brand == $vehicle_brand->vehicle_brand_id ? 'selected="selected"' : '' }}
The field you are comparing the values for should be the same.
And as a side note, if you are on Laravel 9 you can use #selected blade directive
#selected(old($vehicle_brand->id) == $selected_vehicle_brand)
I am just building my first Laravel App and have the following question:
Simplified Table Structure:
Table CORPORATE
id pk
name
checkbox "isSupplier"
checkbox "isManufacturer"
Table COMPONENT
id pk
name
if_corporate fk (where isManufacturer=true)
So my question is, where do I need to put hands on the code to get this "selector" implemented?
Thanks
edit:
resources/views/admin/components/create.blade.php
<select class="form-control select2 {{ $errors->has('corp') ? 'is-invalid' : '' }}" name="corp_id" id="corp_id" required>
#foreach($corps as $id => $corp)
<option value="{{ $id }}" {{ old('corp_id') == $id ? 'selected' : '' }}>{{ $corp }}</option>
#endforeach
</select>
is this the correct place to modify?
You are already too late in the code you have shared. That is the view, which comes after the controller in this instance.
The controller is probably called something like ComponentsController there will be a line of code that says
$corps = Corporates::all();
Or something like that. That is where you need to update it. Probably something like this:
$corps = Corporates::where(["isManufacturer" => true)->get();
Now $corps will be a collection of only ones that are manufacturers.
First, my apologies if the issue has been resolved. I read a lot of similar posts, but not quite the right one for my case.
I am setting up a simple project in Laravel 5.8. I want to create a simple bookstore and I need a book to have multiple authors so each title is followed by the author, or authors - if many - separated by commas and the last one by the word 'and'.
I have set up two Models, 'Author' and 'Book' and their respective tables, as well as a pivot table since they are in a relation belongsToMany. Everything works like a charm, I get my results as expected. However, I cannot get to format the results as I need. In the case of multiple authors, I always get an extra comma in the end. Since I cannot get the commas right I haven't yet tried to add the last 'and' in the case of the last author.
In the case of a single author the solution is easy, I just use a conditional.
However in the case of multiple authors, thing get complicated.
The most popular method to similar problems was the use of implode() in various similar ways. The problem with this method is since Eloquent is using its internal logic to perform the query, when I loop through 'books', there is no author column, just a reference to the pivot table. In addition, the authors' include first name and last name in different columns. So, when I try to manually create an 'implodable()' array by fetching the respective data otherwise, I get double the item size, since each name consists of the first name and theist name. And on top of that, the whole thing runs inside a double loop, making things even more complicated for me.
I am sure there is a simple way around this.
This is a sample of my blade code as of now. Of course the conditionals should be rearranged accordingly when the problem will be solved, to implement the 'and' case:
<ul>
#foreach ($books as $book)
<li>{{ $book->title }} by
#if (count($book->author) == 1)
#foreach ($book->author as $name)
{{ $name->last_name }}
{{ $name->first_name }}
#endforeach
#else
#foreach ($book->author as $name)
{{ $name->last_name }}
{{ $name->first_name }}
{{-- print a comma here if there are other names, or an 'and' if it the last one. Problem seems that it needs to be solved outside the conditional, but how? --}}
#endforeach
#endif
</li>
#endforeach
</ul>
My DB structure:
'authors': 'id', 'last_name', 'first_name','created_at', 'updated_at'
'books': 'id', 'title', 'created_at', 'updated_at'
'author_book': 'id', 'author_id', 'book_id','created_at', 'updated_at'
Expected result:
Title 1, by Author 1
Title 2, by Author 1 and 2
Title 3, by Author 1, 2 and 3
Actual Result:
Title 1, by Author 1
Title 2, by Author 1 and 2 and
Title 3, by Author 1 and 2 and 3 and
If you didn't need the "and" mechanism, then implode with comma would do the job. For "and" mechanism use below code:
<ul>
#foreach ($books as $book)
<li>{{ $book->title }} by
#for ($i = 0; $i < count($book->author); $i++)
{{ $book->author[$i]->last_name }}
{{ $book->author[$i]->first_name }}
#if ($i == count($book->author) - 2)
and
#endif
#if ($i < count($book->author) - 2)
,
#endif
#endfor
</li>
#endforeach
</ul>
added non-breaking-space   wherever necessary to not get them linked to each other
Delete #if (count($book->author) == 1), if you use foreach inside.
You can't use #else, when before you close condition with #endif.
This #else has wrong construction, use #elseif instead.
If you want check authors count and show extra value, do it inside foreach with $book->author()->count().
What relation u have for book -> author? One book can have many authors or not?
Just display implode(', ', $book->author), based on your actual result you don't need to do complicated checking if/else anymore.
#foreach ($books as $book)
<li>{{ $book->title }} by {{print implode(', ', $book->author)}}</li>
#endforeach
I have a table column which is separate with comma. Column name is 'Intro' and the values are:
Rumi, school,2018,lecturer
I need to show this comma separated value one by one. The output should be like this :
Rumi
I tried this code in my blade file :
#foreach ($evaluation as $client)
#foreach (explode(',', $client->Intro) as $client_intro)
{{ $client_intro[0]}}
#endforeach
#endforeach
but this shows nothing. what is the problem ?
Just simply remove the index
{{ $client_intro[0]}} ---->> should be --> {{ $client_intro}}
Try This:
#foreach ($evaluation as $client)
#foreach (explode(',', $client->Intro) as $client_intro)
{{ $client_intro }}
#endforeach
#endforeach