Sorry for this very noob question. I started PHP for almost 1 months. I don't have any background in programming. Without using a framework. A friend of mine lets me work with him on a project. which uses a php it had a framework which was built by them. Since their team lead decided to pick a new framework, which was laravel and rewrite the whole project. Luckily he still let me join them. I've been working only on the small stuff in the project. I haven't touched any database related during my work with them. after a week I had to go back to my home town so I can't work with them anymore. Since then I keep reading the laravel documentation. And tried database. I stumbled on this eloquent which builds query from class names or method name. I was so confused. But I don't know what to search for or how does this work. I scanned the code and hell it was so advance I give up. I can't sleep on how to make this. so if any of you could give me an example how this works I would be very happy.
class User extends Model
{
}
which when called like this
User::all()
it will give some data. but how.? it this part of php? All I see about php mysql is about pdo. I can't find any examples like this
A model in laravel is linked to a snake cased table. This means that a User model maps to a users table but this can be overwritten by adding this in your model class
protected $table == "my_custom_table_name"
When you use User:all() It will return all the records in the users table. It simply runs select * from users beneath it.
The all() method is defined in the extended model class. You can check it out to understand how laravel automagically does stuff.
Related
I'm learning Laravel and I'm watching many tutorials, but I dont really get it, what's the difference between the controller and model, because you can put in both a function.
Controllers in Laravel are used to determine how to handle http requests.
When you have anything to do with the DB, its better to place those function in the model, and call them from the controller.
In clear terms:
Model performs all operations on data from DB.
Controller call necessary model methods and ready the data.
View take care of displaying the data.
I hope this is clear enough.
You will be familiar with all of this soon.
model methods is for relationships mainly , or to make some thing for every object of this model (database table) every column in db is an object and every table is a model.
but in controller you set your app functionality that you want , and its an intermediator between model and view .
i hop this makes you good in this point.
good luck
You can write functions anywhere, you are perfectly right.
But is not an efficient way to do things.
The answers for those questions can be easily find out. Search about MVC pattern. In few words, remember brief:
MODEL => working with relational databases / storing the data
CONTROLLER => working with the logic(taking inputs, calculus etc) / general functionalities
Combining them is more efficient than working with those together, that is the reason why using a pattern is more great than writing code in a old style mode reinventing the wheel again.
I am new at Laravel.
As I am studying Laravel I noticed that ALL Model I made is named by the plural but it works properly.
I am wondering how can it happen because I read documents after I noticed that and it says like, "Model should be named by the singular.".
Can anyone explain how it works??
P.S. There is no mention $table in those Model.
I am so sorry I wanted to say "singular" not "syllable".
General convention of laravel is using a singular name for a model and a plural name for a table. Note that you can change the associated table name by using protected $table = 'yourTableName'; in your model. I personally think setting it manually is a good practice.
I can't explain how it gets the associated table name as it will be an essay(also I am not sure I will be able to explain properly) but I can send you to the right direction.
As laravel is open source you can actually peak under the hood and see how it works. You can go to framework/src/Illuminate/Database/Eloquent/Model.php or see it online to see the associate functions. You will see the getTable function which will return the table name. But it uses another function. So you need to see what is in that function.
After a few digging you will see that laravel actually uses a different library to help getting the project name. It uses inflector. You can browse a bit about it to understand how it gets the plural name. There you can find the underlying code and rules etc to understand how it works.
I am quite new to Slim, still trying to learn it and decided to redo an old app I'd made. I am trying to use Eloquent but have quickly gotten lost doing what I wouldn't think is very complicated.
The app I had was even too complicated to learn on, so I backtracked to this tutorial here, as this is more or less what I am trying to do, use models extending one other class, to see if I can even get this working: http://www.richardbagshaw.co.uk/laravel-user-types-and-polymorphic-relationships/ It's just a user type extension.
I cannot. This is a tutorial for Laravel obviously, so I know it will be a bit different. I have recreated the database (minus some of the extraneous stuff like username and password) and populated it sufficiently. I have copied the code for the User, Freelancer and Employee classes, modifying only the User class removing the extra methods which don't seem to be required for this (I think) as below.
namespace eloquent\eloquent;
class User extends Eloquent implements {
protected $table = 'users';
public function userable()
{
return $this->morphTo();
}
}
If I do this:
$user = $app->user->find(1)->firstName;
echo $user
It works as expected.
This does not:
$user = $app->user->find(1);
echo $user->userable->dayrate;
It gives me this:
Fatal error: Class 'Employee' not found in D:\Apache24\htdocs\eloquent\vendor\illuminate\database\Eloquent\Model.php on line 900
It does however correctly identify whether it's looking for an employee or a freelancer, which I assume is coming from the DB column userable_type.
Question is really how should I be accessing the fields of the subclass? Am I doing it totally wrong, or is there a better way?
I eventually came back to this question, and was able to solve it, just in case anyone else is having a similar problem.
The problem was that it was looking for the exact string in the Database as the usertype, in this case Employee. What it really needed, was to look for the whole string including the namespace. By adding the entire namespace into the database entry as userable_type (ie eloquent\eloquent\Employee) it then picks it up and can find the model...
I looked into options in Eloquent, like MorphClass but that does not appear to do what I need.
I haven't actually implemented this into a project I'm working on yet, but I imagine I'll probably either leave the whole string in there because I'm lazy and it's not a mission-critical, time sensitive app, or try to do something with middleware or ??. Anyways, hope that helps someone else.
I'm trying to figure out how to load/set models/classes in Lithium controller. This is my first so serious framework & I like it very much, but I dont know a lot about them. Have used only simple one.
The problem what I have is: I'm trying to figure out how to display different controllers/models in one view/layout (display posts, polls, login box etc in one page). I found a tutorial for cakePHP, so you can see here whats bothering me. I could find answer in Litihum docs. Maybe becouse I just don't know the real key words for that.
http://nuts-and-bolts-of-cakephp.com/tag/cakephp-dashboard/
If you want to display multiple models at once in the same view (like users, latest posts, etc) you can just reference the class:
use chowly\models\Offers;
use chowly\models\Venues;
class OffersController extends \chowly\extensions\action\Controller{
public function index(){
$venues = Venues::find('all');
$offers = Offers::find('all);
}
}
In lithium, you just need to reference a class and you can use it. (No ClassRegistry)
For a working Lithium application, take a look at https://github.com/masom/Chowly and join the irc channel on irc.freenode.net #li3
Firstly, I would like to just it out there that I am an ORM noob. I've never used an ORM in my life and have reached a point in my day-to-day developing that I need to do some crazy advanced relationships that I believe Datamapper Overzealous Edition can help me with as I am using Codeigniter.
In my database I have the following tables;
users
projects
clients
tasks
Here is my desired relationships between the tables;
A user can belong to many projects.
A project can have multiple tasks, but can only have one client.
A client can have many users and can have many projects
A task can only have one project
I have attempted to set-up my models in the models directory as it says in the documentation the model name without the s on the end, so for users I have a user.php model and so on.
I know the documentation is great, but I just can't seem to understand it properly even though it is obviously very easy. I know you instantiate the model by going for example $u = new User(); inside of your controller, but my question is setting up the relationships inside of the models.
How do I set out my models to have the above relationships so for example when I fetch a task I can see what project it belongs to and a whole heap of information from its associated database tables.
I noticed that in the documentation you use the following inside of the projects model which should tell it that it can have more than one task for a project; var $has_many = array('task')
Is that all there is to it? Is it as simple as defining the $has_many and $has_one variables and putting in the associated model name in the array?
I've never used this particular ORM but I do use Doctrine. If the one you are using works in much the same way then the simple answer to your question is - yes! With Doctrine you set up all relationships in the model classes. The ORM will then manage it all for you. So, for example, if you instantiate a new task object...
$task = new Task();
Then you can access the relationship with the Project table by simply writing
$task->Project;
I must stress that the above code is not written for datamapper so might not work as is, but I hope it clears things up for you. It sounds like you do understand the documentation but just don't believe it!!!!
That is all you have to do as long as your database structure fits the layout expected by Datamapper Overzealous.
I've been using this ORM in a project I've been building and for the most part, it's been extremely helpful and time saving.