How to save multi selected values? - laravel

I a have a one to many relationship between culprits and crimes and also a one to many relationship between species and crime.
culripts
id
first_name
last_name
crimes
id
culprit_id
species_id
fishing_method_id
species
id
species_name
fishing_methods
id
fishing_methods_name
What I need to achieve is to save many species for one crime
my form
{{ Form::select('species_id',$speciesRepository, array('Action'), array('multiple', 'class' =>'form-control')) }}
my controller
public function store(CreateCrimeRequest $request)
{
$input = $request->all();
// $data = implode(',', $input);
// dd($data);
$crime = $this->crimeRepository->create($data);
Flash::success('Crime saved successfully.');
return redirect(route('crimes.index'));
}
When I select 3 species from the dropdown the last selected species is saved while the other two are not.
could anyone help to solve this

You are missing [ ] in your select name attribute
{{ Form::select('species_id[]', $speciesRepository, array('Action'), array('multiple', 'class' =>'form-control')) }}

You're missing () here:
$crime = $this->crimeRepository->create($data);
Must be:
$crime = $this->crimeRepository()->create($data);
You can use createMany too:
$crime = $this->crimeRepository()->createMany($data);
more examples here: https://laravel.com/docs/5.5/eloquent-relationships#the-create-method

Well, you have to code it to create three records in your pivot table. I assume you have a table of species You have a table of crimes. You need a belongsToMany relationship to store many species for one crime and a pivot table of crime_species You will then attach species to crime
and You are missing [ ] in your select name attribute
{{ Form::select('species_id[]', $speciesRepository, array('Action'), array('multiple', 'class' =>'form-control')) }}

Related

How to get data from additional table in many to many relationship?

I have a many to many relationship between tables 'products' and 'recipes'. I created table product_recipe to handle this and my table looks like:
id | recipe_id | product_id | amount
In my blade file I use
#foreach($recipe->product as $row)
<p>{{ $row->name }}</p>
#endforeach
to get a name of used products in recipe. How I can get an amount of product which is stored in product_recipe table? Do I have to create a model for product_recipe table and get it from model?
You will have to declare the relationship wants to include the extra pivot information so that the pivot object that is attached to the records will have more than just the 'id's:
public function products()
{
return $this->belongsToMany(Product::class)->withPivot('amount');
}
Now the pivot object will include the field amount:
foreach ($recipe->products as $product) {
echo $product->pivot->amount;
}
"By default, only the model keys will be present on the pivot object. If your pivot table contains extra attributes, you must specify them when defining the relationship"
Laravel 6.x Docs - Eloquent Relationships - Many To Many - Retrieving Intermediate Table Columns withPivot
You have to use pivot attribute.
#foreach($recipe->product as $row)
<p>{{ $row->pivot->field_name}}</p>
#endforeach
No need to create Model for product_recipe. Just specify table name in relation of two Models.
Like return $this->belongsToMany('App\Product', 'product_recipe')->withPivot('amount'); and similar for other model.

Laravel - Filter records by distinct relation

Right now I have the following models structure,
Country -> State -> City -> Student
And the Student model includes first_name and last_name.
So, I want countries to by filtered by giving student's first name. Like if I give John then I want list of countries which has students whose first name is John.
I was trying something like this,
I added a method called Students() in Country model and returning Student instances from that method. But now I stuck to find out how to filter countries.
Thanks in anticipation.
I came across a similar question recently and I came with the following solution myself. Probably, there are easier ways, but this will get you going for now, I guess.
First we query all the users with first_name == John, as you described, and we limit the query to output only the ID's.
$users = User::where('first_name', 'John')->get()->pluck('id');
We then cross-compare this with the result of Students() from the country model. As I don't know what you're querying for to get the country that you want, I'll just take the Netherlands as an example — that's where I'm from.
$users = Country::where('name', 'the Netherlands')->students()->whereIn('id', $users)->get();
For this to work, you must make sure that within the Students()-function in your model, the get() is left out.
Final 'result':
$users = User::where('first_name', 'John')->get()->pluck('id');
$users = Country::where('name', 'the Netherlands')->students()->whereIn('id', $users)->get();
in the Student Model create this:
public function city()
{
return $this->belongsTo(City::class,'city_id', 'id');
}
and in the City Model create this:
public function state()
{
return $this->belongsTo(State::class,'state_id', 'id');
}
Finally in the State Model:
public function country()
{
return $this->belongsTo(Country::class,'country_id', 'id');
}
For example if you made things like that in controller:
$students = Student::where('name', 'John')->get();
in the view:
#foreach($students as $student)
$student->city->state->country->country_name;
#endforeach
You can access like that.
If you have setup the model and relationship properly then you need to call them using in with function
$students = Student::where('name','John')->with('city.state.country')->get();
Loop through the $students
#foreach($students as $student)
{{ $student->city->state->country }}
#endif

laravel one to one retrieve many results

I have business and location table and they have one to one relationship. Everything works fine so far but they problem is I only find 1 result.
However business_id which is a foreign key in location table has loads of results.
My business table has id from 5-95 and these are the records I want to return
so in my location table I also have business_id which is from 5-95.
How do I tell eloquent to retrieve these specific records?
At this moment I have:
function transportProfile() {
$type = 2;
$business = Business::where('type', $type)->get();
$location = Business::find(3)->locations;
echo $location;
return view('userProfile', compact('business', 'location'));
}
So to clarify business table has id from 5-95 and location table has business_id from 5-95 I want to return results from those 2 tables so I can use it in view
Use the eager loading:
$businesses = Business::where('type', $type)->with('location')->get();
To display the data:
#foreach ($businesses as $business)
{{ $business->location->name }}
#endforeach

laravel passing data to view blade

//model
return $this->hasMany('App\Model\Order', 'customer_id')->select('name');
//controller
$customer = Model\customer::find($id);
return view('customer', array('data' => $customer));
//view (blade)
{{ $data }} //{"id":1,"name":"Tony"}
{{ $data->orders }} //[{"name":"T-shirt"},{"name":"Macbook"}]
i'm new in laravel
I have a question about passing data to view.blade
I used hasMany to join 2 table together and pass $data to view
when I try to output $data, I could not see order object inside of data object
but when I did $data->orders, it shows object
can anyone tell me how it works?
The behavior you are seeing is because Laravel lazy loads relations when they are accessed. If the relation has not been loaded, Laravel will send another query and add it to your $data object behind the scenes. That's why when you dump the $data variable, you are not seeing the orders.
To demonstrate, run the following snippet.
{{ $data }} //{"id":1,"name":"Tony"}
// Laravel will lazy load the orders relation
{{ $data->orders }} //[{"name":"T-shirt"},{"name":"Macbook"}]
// Now the $data object has the orders property.
{{ $data }} //{"id":1,"name":"Tony", "orders": [{"name":"T-shirt"},{"name":"Macbook"}]}
Solution
You have a couple options here. Here are 2.
1. Eager load relation when querying model.
$customer = Model\customer::with('orders')->find($id);
This is the preferred method as it prevents n+1 querying.
2. Load relation after model has been queried.
$customer = Model\customer::find($id);
$customer->load('orders');
Now when you dump the model, you will see the orders property.
In your model,
public function orders(){
return $this->hasMany('App\Model\Order', 'customer_id');
}
In view
{{ $data->orders->order_name }}
I hope it will works
You have defined hasMany relationship in your model. So, for a particular customer there can be multiple orders. So, it shows in array. You have to loop through:
foreach($data->orders as $order)
{
$order->name
}
to get the orders.

Laravel Foreign Keys Drop-down List

I have 2 tables:
CUSTOMERS(id, full_name, company_id)
COMPANIES(id, company_name)
I already created the relation between the two tables, and it's working fine because I can display company name in the customers view like this: $customer->company->company_name
I'm now having issues with the customer create and edit views. I'd like to have the company_name as a drop-down (Form Select) in create and edit views. Then insert the company id to the CUSTOMERS table.
You need to supply Form::select with companies as an array('id'=>'name'):
// Controller, repo or wherever you want it:
$companies = Company::lists('company_name','id');
// $companies passed to the view, then in the create view:
{{ Form::select('company_id', $companies, null, $options) }}
// edit view:
{{ Form::model($customer, array('route' => array('YourCustomerUpdateRoute', $customer->id))) }}
...
{{ Form::select('company_id', $companies, null, $options) }}
// form model binding autopopulates the form, so correct option will be selected
After submitting the form validate the input, check if provided company_id exists on the companies table and save the customer, that's all.
The Jarek Tkaczyk answer is excellent. However if you want make a default value for create form and avoid pre-select the first element in the $companies array, you can do something like this in your controller:
$companies = Company::lists('company_name','id');
$companies = array('0' => 'Select Company') + $companies;
Then pass $companies array to the view as Jarek Tkaczyk said.
Note:
I did
$companies = array('0' => 'Select Company') + $companies;
in order to preserve the array keys.

Resources