In my Blade file, there are two dropdowns, "Countries" and "Regions". I want the Regions dropdown only to display the values linked to the country selected in the "Countries" dropdown.
For that, I believe, I have to pass a variable for $countries and $regions in order to compact them and call the variables in the blade file.
This is what I currently have (which is totally wrong), but what should it be?
public function create()
{
$countries = Countries::orderBy('name')->get();
$regions = Regions::where('countries_id' == $countries('id'))->orderBy('name')->get();
return view('admin.cities.create', compact('regions', 'countries'));
}
I know that the $regions variable part "where('countries_id' == $countries('id'))" is incorrect, but I've tried multiple other ways, and still not able to figure it out.
Is this variable = model::where(foreign_key == parent_table ID) possible?
All my relationships have been set up with Laravel Eloquent already.
Any assistance would be appreciated
I started to write how to fetch data from controller(Country::all and Regions::all), and what to do in blade file and ajax functions that does stuff on events and figured - maybe there already is a solution.
Sorry, I don't have 50 rep to comment, but if you follow what they did here (both answers), I think you're gonna figure out what to do in your example :)
Keep in mind you need the part in the bottom of the page!
Laravel dynamic dropdown country and state
Related
I am making a website for college administration where professors log in and assign marks to the students they are teaching.
There's a table, called "IA_Marks" in my database:
|Student_ID|Subject_Code|Name|Marks1|Marks2|Marks3|Semester|Division|
There's also a table called "Classroom_Mapper" in my database, that helps map a professor to a classroom, with a subject:
|Prof_ID|Subject_Code|Semester|Division|
This is a method in my controller:
public function showTable(){
$sem = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('semester');
$division = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('division');
$data = DB::table('iamarks')->where([['semester','=',$sem],['division','=',$division]])->get();
return view('ia',compact('data'));
}
Using this, I can fetch rows that belong to the professor who has logged in.
But, there's a problem.
Say the professor teaches two subjects, in two semesters. Then, the where clause will return multiple results from the mapper table.
For example:
select semester from classroom_mapper where Prof_ID=auth()->user()->Prof_ID
output:
8
5
Then the students from both 5th and 8th semester will be shown on his dashboard. Our target semester was, say 5th. Then it'll be a problem.
Registering for a subject, is done as shown here:
form screenshot
Let's call the subject being registered in the screenshot "SUBJECT 4".
It is a subject for the 5th semester, division A.
I want to dynamically make a button(SUBJECT 4) on the dashboard, which when clicked, sends the semester(5) and division(A) of choice to the controller.
Dashboard Screenshot
This button should open a newly made page with name of the subject(subject4.blade.php), where the database table contents for target semester and division(5 and A) will be shown.
How do I make this dynamic view creating button which sends specific info to controller? Is it even possible?
There are a few ways to do this with Laravel, but my goto is usually to create a single blade template for each view (dashboard, subject, etc.) that can be dynamically populated -- assuming that the layout for each subject view is the same.
In your dashboard view, you could generate a url for each button that uses a format like this: http://cas.jce.in/subject/semester/5/division/a/
Next, create a route that uses a couple of paramaters, something like this:
Route::get('/subject/semester/{semester_id}/division/{division_id}', 'ControllerName#showSubject');
More info here: https://laravel.com/docs/5.8/routing#required-parameters
Then in your controller, add a showSemester function like this:
function showSubject($semester_id, $division_id){
$data = DB::table('table_name')->where('semester', '=', $semester_id)->where('division', '=', $division_id)->first();
return view('subject', ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);
}
Your route parameters are available to the controller, in order of appearance. So we can add $semester_id and $division_id as the first two parameters of our function. Next, we'll to the database work to retrieve the data we need before returning everything to a view.
Note here that we're using a single view rather than dynamically selecting one. You could create individual views for each subject, but im thinking you probably don't need to unless the layout of each one is unique in some way. In that case, you can simply do something like this, but I'd generally try to avoid it.
$view = 'subject'.$data->subject_id;
return view($view, ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);
Also, just a quick note ... you may consider adjusting your database queries from above to use a select statement rather than pluck. The end result is the same, but using a select can boost performance by only loading the data you want ... rather than loading everything up front and throwing most of it away.
$sem = DB::table('classroom_mappers')->where('Prof_ID', Auth()->user()->PID)->pluck('semester');
... becomes ...
$sem = DB::table('classroom_mappers')->select('semester')->where('Prof_ID', auth()->user()->PID)->get();
I am wanting to list all comments. Comments have a Polymorphic relationship with Blog, News etc. When listing all the comments I want to convert the commentable_id to the slug I have saved in the Blog/News table.
So far I have:
public function getCommentableIdAttribute($value)
{
// $post_type = $this->commentable_type;
$post_detail = Blog::find($value);
return $post_detail['slug'];
}
I want to use $post_type and find the value based on that. Naturally though, I am getting a non-object issue if I replace Blog::find() with $post_type->find().
I don't know at this point which table I want to reference.
• Is there a better, more Laravel way of reversing this? The docs seem to suggest that I need to know the Model first.
• If not, any ideas how to make this work?
Thanks in advance for taking the time to help.
this has been driving me nuts...
I'm in my view I'm calling
Auth::user()->userlists
which brings me back an array of values like
{"id":"1","user_id":"1","name":"list1".....}
all I want to do is bring back the names I've tried all these;
Auth::user()->userlists->name
Auth::user()->userlists()->name
Auth::user()->userlists->name->get()
Auth::user()->userlists.name
But I always get an error such as "Undefined property"
How do I return this single property, it's in my array for all the items but I'm clearly just getting the syntax incorrect...?
The reason i'm trying to do this is that I need the values placed in a drop down box, it's finding the correct rows in the table but displaying all the data instead of just the name
Form::select('userlist_id', Auth::user()->userlists);
Many thanks.
You say a User has many Userlist models, so it's a collection not single model, thus you could work with it in a loop, BUT there is better way:
$lists = Auth::user()->userlists()->lists('name','id');
// To make it available in all the views, place this for example in a controller:
View::share('userlists', $lists);
This will fetch id and name for the related models collection and return it as an array, so it's just the one you will use in a Form builder:
Form::select('userlist_id', $lists)
i'm developing a web platform in codeigniter (first time with CI) to calculate quotes for a growing number of different products.
The problem i'm facing is that each of my products have different sets of 10+ options but I want to save this data to just one table in my database. I have previously used a different table for each product allowing the table structure to represent the different sets of options however this isn't very scalable with our growing product range.
After some research it appears one solution would be using the 'serialize' function to store all of my post data (from the quote form) for each product in one column and then unserialize when I want to use this data...
Is serializing the data the best approach and would anyone be able to provide a simple example to show how to handle the insert from a form submission / retrieving the data?
Thanks very much in advance
EDIT: Searching will only ever on a product type or unique id. My thoughts were to have a table like 'id, product_type, product_options' with the product_options containing the serialized data?
EDIT 2: Taking an EAV approach seems like a good shout. I'm used to querying and returning a single result object to be passed into the view ($query->quote_ref, $query->quote_date for example). Could anyone point me in the right direction on how to use the single quote's data when the query would return multiple rows, one for each attribute?
As you mention you would not need to search on the serialized data, then yes this approach will be fine. I would opt for json_encode as the data is more human readable in this form.
Then code will depend on your DAL but a basic example would be:
$productOptions=array($_POST['option1'], $_POST['option2']);//etc will need to validate data
$databaseMapper->product_type='product type';
//your product_options column is suitable sized varchar
$databaseMapper->product_options=json_encode($productOptions);
$databaseMapper->save();
To retrieve:
$databaseMapper->loadById(20);
//$productOptions is a standard php array
$productOptions = json_decode($databaseMapper->product_options, true);
EDIT re displaying in your view.
This is codeignitor specific (whereas the above is not).
Based on code from here: http://ellislab.com/codeigniter/user-guide/general/views.html
In your controller:
//code similar to above to retrieve product options data, ideally contained within a model
$data['productOptions']=$productOptions;
$this->load->view('content', $data);
in your view:
<ul>
<?php foreach ($productOptions as $option):?>
<li><?php echo $option;?></li>
<?php endforeach;?>
</ul>
I'm relatively new to coding in general, first year CS student and all of that. I'm using PHP to display a list of school classes categories and a list of assignment posts within each category. The desired output would be something like this:
CATEGORY 1
-Assign Post 1
-Assign Post 2
CATEGORY 2
-Assign Post 0
Using join gives me partial functionality, because it gets all the assign posts, but when I loop through and post the data it posts the Category title more than once. I then tried using group_by but it's only posting one post per category.
I'm not gonna post all my code, simply the db queries. I'll happily post the other code if you think the problem may be there.
//Select our classes, and get assignments from each class
$this->db->select('*');
$this->db->from('categories');
$this->db->join('assignments', 'categories.cat_id = assignments.assign_cat_id');
$this->db->group_by("cat_id");
//Return the classes and assignments in an array
$query = $this->db->get();
return $query->result_array();
Not to tricky right? Any help is of course appreciated :).
UPDATE
I have figured out that group_concat will post the assignment names, and then I can explode the results. Is this the only way, or is there a way to get it to post in a nice friendly array. The only reason I ask is I feel this adds un-necessary code in my view file, where I'm exploding the data.
Thank you!
Dont use group by ...use sort by Category id and while showing/or preparing array of data you can club records as per category..you cant achive this by single SQL