Laravel 6 assigns unwanted variables to view - php

In my web application, I'm using a view twice, let me call it modify-customer.blade.php. It is used to create new records as well as to edit existing ones. It has completely the same design except text and form input values that are filled from the assigned customer.
In my edit-action, I'm assigning the variable "customers" containing a list of customers (because I need other customer data as well, retreived by $customers = Customer::orderBy('name', 'asc')->get()):
return view('customer.modify-customer', ['customers' => $customers]);
In my new-action, I'm assigning a single customer record retrieved by $customer = Customer::where(xyz)->getFirstOrFail(); which is assigned to the view:
return view('customer.modify-customer', ['customer' => $customer]);
The problem is:
I'm able to access variable $customer in my edit action (which returns the last record from the database table for an unknown reason). I really have no clue why, I never assigned a variable called customer to it. I only assigned customers.
Hope you guys understand what I'm trying to say. Thank you for your help; I really appreciate an idea how to disable or fix this .

Make sure you order the routes properly
/edit
/:id
constants before variables, this way it catches the edit route before before the view route.

Related

Accessing x-modal data and storing in database

I am having some trouble accessing x-model data in my laravel app and storing it into my database. Wondering if anyone can help.
So the issue seems to be coming from the fact that I have the category select as a separate blade that I call multiple times.
So ultimately, this code is called three times on my main page. I can then choose three separate categories - but when I try to call those selections and store them in database I run into an error.
Wondering if anyone has any ideas how to access the three separate categories being selected and add them to database. Some links or documentation to check out would be mighty helpful!
Using alpinejs, php, and laravel.
EDIT
In this particular case I am passing the information into the blade through the controller.
EDIT 2:this shows $selection_id
x-model takes the name of a variable inside x-data, not the value.
$selection_id here is a value.
You should do something like:
x-data="{ selection_id: {{$selection_id}} }"
on the component element
then inside x-model:
x-model="selection_id"
Note: Your code is missing, please add the part with the `x-data showing.

PHP - Load variable page content

Apologies again, as I'm new to PHP so not fully sure how this works, but thought I'd ask.
I have a page called contract.php, which I'm hoping to use as a template so that when someone selects the contract they wish to view it goes to the contract.php and loads the content for that particular contract.
Hopefully I shouldn't need to create a page for every single contract, I was hoping that I could use the contract ID, so that when the navigate to the page it would show something like contracts.php?contractid=555 and then loads the contracts details for contractid 555, so this does this for each new contract that's listed.
The issue I'm having is, I'm not sure how best to go about it, or how to use this ?contractid='xxx' etc.
Any help would be appreciated.
Many thanks
$_GET manual.
after reading the contract id from the get variable, you can search your DB for that contract and echo the specific data of this contract.
Also consider using $_POST so that you can't easily see any contract by changing the url by yourself
You can fetch respective data from your database for each contract using the contract ID. Inside the contracts.php, check whether contractid variable is set and not empty, using isset($_GET['contractid']) && !empty($_GET['contractid']). If it is true, then fetch respective data from the DB and display it to the user, in the template that you must have created.
Hope this helps. If you need more help then paste your code here.

Link that passes parameters to Controller function

I currently have a view that is shared by two controller functions that pass in correct variables so i know where it is coming from.
I have a situation where I need URL references that can be clicked by the user that point to a function in both controllers that queries a Model and gets all the data from that model.
I am looking for some pointers on how to handle passing 2 parameters to the controller based on what the user clicked. I will post code to make this more understandable. Also, if I have a function that is querying the entire database, how can I use Query Scopes, so that if these parameters are passed to it, it doesnt query the entire database but instead uses my query scopes.
Here is the code for reference:
the View: http://pastebin.com/dWCEKztS
one of the controllers that I am working on first. The function that queries all of the rows of that Model: http://pastebin.com/LUbMLajs
In the Model, I tried to create these Query Scopes: http://pastebin.com/s30saqBL
So basically in the view, it displays certain names, and corresponding counts for each status. I have this all taken care of in that $totalCount data structure. I need to pass these to the index function so I can query only that certain data based off those parameters. The admin is in another table called admin, and in the advertiser application table there is an assigned_to field that links with that admin ID.
Please let me know if I need to clarify anything further!
Thanks
Set the URL to pass variable with it, in your route.
route::get('/pageName/{variableName}', array('as' => 'page-name', 'uses' => 'yourControllerName#functionName'));
in controller:
public function functionName($variableName){
$get = Model::where('columnName', $variableName)->first();
}
Hope this help.

Accessing Model from View

I have a foreach loop which generates links with different IDs. I have an ajax function to process this ID and the username (from session) to add a record to the table. (It's like enrolling for some events).
I want to make an additional statement which will check whether the user has already enrolled for the event with some ID and if so, the link will be deactivated or change color. I've tried creating another variable (which is passed to the view)
$data['myvariable'] = $this->mymodel->myfunction();
This function in model checks all the records from the database where user's username appears and insert to array all event IDs. I've tried adding an additional if statement before links in foreach loop which checked whether the ID from link is in array but there appeared some problems with the controller. (i think that I couldnt assign the array to the variable $data['myvariable'] ).
I know that accessing to model from view is not "proper"... Anyone know how to solve this problem?
Do you have a field in the DB to store whether or not the user has enrolled? Is so, run a select query on that table checking for that value then use a conditional statement to effect the link. Kinda like this
$enrolled = $this->your_model->your_method($param);
if($enrolled){
process links here;
}
I'd say it depends on the MVC your using also. You may have to load the model in the view to use it from within the view, not ideally the right approach as you could load it via your controller and set it as a variable to be passed into the view if the variable to be passed contains multiple outputs such as a query from a DB or something you can pass it to the view as an array object and then in the view do a while, for, foreach, whatever on that array.

CakepPHP: help with the cakephp comments plugin

Hi I'm trying to use the CakePHP comments plugin found here http://cakedc.com/downloads/view/cakephp_comments_plugin but the instructions are really hard to follow. I've managed to add comments but it's displaying the commentWidget that's not working.
I'm getting confused at this part i think
To work properly, the component needs
a specific variable to be set in every
action using it. Its name should be
either
Inflector::variable(Controller::$modelClass)
or Comments::$viewVariable should be
set to other name of this view
variable. That variable should contain
single model record. for example you
need to have next line in you view
So far I've created the comments table, added it to the pluging and components arrays and added the following code to the controller:
public function beforeFilter() {
parent::beforeFilter();
$this->passedArgs['comment_view_type'] = 'flat';
}
I added the route
Router::connectNamed(array('comment', 'comment_view', 'comment_action));
And also the Comments.CommentWidget as a helper in my controller.
I'm just wondering if anyone has used this plugin before and can help me out?
thanks,
Jonesy
You're right - the documentation is really confusingly worded. However, if I understand correctly, what it wants is a copy of the record of the piece of data the comment will be attached to passed to the view the comments will render on.
So say you're making an event page, and you want people to comment on the event. You need to send to the view a variable called "event" with a copy of the base data for that event.
From their example they show: $this->set('post', $this->Post->read(null, $id));
For your event, you'd do something like $this->set('event', $this->Event->read(null, $id_of_event));
The Comment view probably needs this data for hidden fields so it can populate it with the model name and the event id.

Categories