OctoberCMS get id of component being saved - laravel

I am trying to update a table with the path of the uploaded file so that it is easy to email a download link but I cannot seem to get the id.
My component looks like this:
public function onAddJob() {
$manual = new Job();
$manual->company = Input::get('company_name');
$manual->ordered_by = Input::get('client_name');
$manual->ordered_by_email = Input::get('client_email');
$manual->emergency_no = Input::get('emergency_no');
$manual->instructions = Input::get('instructions');
$manual->project_name = Input::get('project_name');
$manual->fileupload = Input::file('fileuploader');
$manual->save();
$this->id = $this->property('id');
Db::table('manual_jobs')->where('id', $this->id)->update(['path' => $manual->fileupload->getPath()]);
Everything saves fine but path is not updated as I am not getting the id correctly, can anyone help show me where I am noobing?

The id component is defined by the variable $primaryKey on the model
default the primary key is 'id' corresponding to a database table field named id
You can overwrite the default keyname by setting $primaryKey to another key
class Foo extends Model {
$primaryKey = 'foo_id';
}
Why i'm explaining this is because you don't need to know the name of the field.
What you can do is:
$foo = new Foo();
$foo->bar = 'baz';
$foo->save();
echo $foo->getKey();
echo $foo->getAttribute($foo->getKeyName());
echo $foo->{$foo->primaryKey}
They will all print out the newly created primary key on the object.
getkey() returns the value of the primary key.
getKeyName() returns the name of the primary key field defined in the model

The solution was a lot simpler than I thought.
What I was looking for was this:
$manual->id
So the update query looks like this:
Db::table('manual_jobs')->where('id', $manual->id)->update(['path' => $manual->fileupload->getPath()]);

Its because when you call ajax request it will not call pageCycle.
as a result your code in page will not executed.
your code on page may be look like this one
{% component 'yourComponent' id=someID %}
but this code is not executed during ajax call
to execute page code during ajax call you need to explicitly call $this->controller->pageCycle()
so new code will look like
public function onAddJob() {
// we are calling page code explicitly
$this->controller->pageCycle();
$manual = new Job();
$manual->company = Input::get('company_name');
$manual->ordered_by = Input::get('client_name');
... other code
}
refer this answer as well
Link : OctoberCMS. Variable disappears after ajax request
if you still find issue please comment.

Related

how to pick single data row from DB using link (A tag)

here is my Data Requesting link
<input type="Hidden" name="addid" value="{{$add->id}}">
{{ __('View More') }}
then this is connect with Route like this
Route::get('/PickAdd/{id}', 'MAddController#view')->name('PickAdd');
controller is like this
public function view($id)
{ //dd($id);
$Padd = Madd::find('$id');
return view('adds.add',compact('Padd','id'));
}
this controlling function not working.
but I change like this
$Padd = Madd::find('22');
then it works,
that mean $id =22
if I run DD, result is null
according to my knowledge my code cant catch passing data from front end, can you help me to solve this
You are passing $id as a string from your controller
Change
$Padd = Madd::find('$id');
to
$Padd = Madd::find($id);
Don't use find() method, in case the record does not exist. Use first()
$Padd = Madd::where('id',$id)->first();
That works when the record exists or not.
Also, look in the Madd model is you defined another primary key other than id, like
protected $primaryKey = 'some-other-key';
because that would force Eloquent to look for that instead of the id

How can I get Joomla component parameter values?

===
UPDATE:
I think now I am literally just trying to get a database value into my component php files, but again, there seems to be very little documentation that can give an example of a function that will return this info like there is in Wordpress.
So I have a table called membersarea_countries that will have records of differnt countries I want to store values for.
I've read about JTable and other things, but how can I simply just bring back the records from this table?
$row = JTable::getInstance('membersarea_countries', 'Table', array());
But this returns a boolean of 0.
I'd really appreciate some help if anyone can.
===
I've been following what several online guides explain, which are all pretty much the same thing, but I never seem to return the values that I'm expecting.
In Components > Members Area (my component), I have a table set up to allow me to enter a record for each country, and then store a uniqueRef, signature, and URL within that record. (for GeoIP purposes).
I've created the first record, however when I try to use the following code, which the tutorials suggest, I don't see any of my fields within this:
$app = JFactory::getApplication();
$params = $app->getParams();
$uniqueRef = $params->get('uniquereference');
$signature = $params->get('signature');
This is all I see in NetBeans:
There's nothing about $app, and no sign of the fields I've got in the Joomla backend.
I don't understand what's happening, or exactly what I should be doing here. Wordpress uses a simple get_option function. Can anyone try and help me?
Below is the link to the detailed document about JTable -
https://docs.joomla.org/Using_the_JTable_class
Firstly you need to create JTable instance using below code and also change table file name to membersareacountries.php
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_membersarea/tables');
$row = JTable::getInstance('Membersareacountries', 'Table', array());
JTable Class in this file /administrator/components/com_membersarea/tables/membersareacountries.php-
<?php
defined('_JEXEC') or die();
class TableMembersareacountries extends JTable
{
public function __construct($db)
{
parent::__construct( '#__membersarea_countrie', 'id', $db );
}
}
Then you can use load method to get any records. This accepts primary key value of that table -
$id = 1;//change id as per your record
$row->load($id);
//read data
echo $row->id;
echo $row->title;

Laravel controller and adding to the result set

I've checked the Q&A about this and can't find anything, so thought i'd ask.
I have a very simple Laravel controller returning all results from a table as below via the 'Name model'. There is then also a further call to my controller, via the model to count the rows and all works and sends to the result set fine...
// All results from my 'Name' model:
$results = $this->name->getAllResults(); // All works fine.
// I then use my controller again, count the rows via the model and add them to $results:
$results['count'] = $this->countNames(); // Again fine
BUT, when i try to add a string to the $results array before i pass it off to th view, as in:
$results['test'] = 'Test'; // This fails in the view
$results['test'] = 124; // But this passes in the view and renders.
It only seems to allow me to add an INT to my result set array. as $results['test'] = 124 also fails.
I then finally, have this sending to my view via:
return view('names', compact('results')); // And that works fine.
Can anyone see what it is I am missing and why integer added to $results works and not a string?. Many thanks in advance.
You are updating collection data. The following line will give collection of models.
$results = $this->name->getAllResults(); // This may give collection of the model
And below, you are updating the collection object.
$results['count'] = $this->countNames();
You can do the following to safely send data to view, without modifying any.
$results = $this->name->getAllResults();
$count = $this->countNames();
$test = 'Test';
$test2 = 124;
return view('names', compact('results','count','test','test2'));

Model Create Method Not Working

I've the following line of code to determine whether a record exists in the DB, if it does then update it accordingly, if not create it:
$test = Result::where('match_id','=',$input['id'])->get();
if(empty($test)){
Result::create($result);
} else {
Result::where('match_id','=',$input['id'])->update($result);
};
return redirect('admin/results');
The update section works just fine, however the create element doesn't seem to work. When I strip down and just use create it works fine, so can't figure out the problem
You must set your attributes as fillable in your model:
protected $fillable = ['a','b'];
And there is a better way:
Result::updateOrCreate(['match_id'=>$input['id']], ['a'=>$input['a'],'b'=>$input['b']])

How to create user-friendly and seo-friendly urls in codeigniter?

i want to change my codigniter url which looks like
http://dev.hello.com/about/updateVision/9
to
http://dev.hello.com/about/updateVision/this-is-test-edit/
i have no idea how to make looks like that anyone can help please
thanks !
This all depends on you database structure. I would introduce a field called reference or slug. This will contain a unique string for your record. When creating the record you can use the url_title function to remove spaces as mentioned by #Rooneyl
This reference must be unique so I would create a function that creates a reference in a loop, checks it against the database. When it returns false, it is unique.
public function unique_reference($name)
{
$name = trim(strtolower($name));
$unique_reference = url_title($name);
$counter = '';
do {
$result = $this->db->select('id')
->from('your_table')
->where('reference', $unique_reference)
->get()->row();
if ($result)
$counter++;
$unique_reference = url_title(trim("{$name} {$counter}"));
} while ($result);
return $unique_reference;
}
You can then retrieve record using this unique value

Resources