**USER MODEL**
public function post(){
return $this->hasOne('App\Post','user_id','id');
}
**WEB ROUTE**
use App\User;
Route::get('/{id}/post',function($id){
return User::find($id)->post;
});
Hi everyone, I'm fairly new to both PHP and Laravel and have been struggling a bit. I just have 2 questions for this code.
In the web routes, why doesn't post have any () beside it? It was declared a function in the user model. And.. I am unsure of how these relationships work (correct me if I am wrong) but does the code above look for a user with a specific $id and connects it with a post having a similar $user_id value?
For the first bit, it is a dynamic property, here you can find how you can actually make one yourself Laravel: create a dynamic property. They essentially work because the result is a single object search based on the id, since it doesn't have to retrieve a collection it allows itself to be accessed like an attribute of the object.
And yea pretty much on the second one. It also uses laravels models to retrieve the data from the database so that you get an object back without needing to create the repositories yourself.
There are major differences between User::find($id)->post and User::find($id)->post(). First one is returning the result of the related relations so you get the post that has user_id equal to $id.
The second one returns a query builder,so you can add more conditions. For example User::find($id)->post()->where("status", 1)->get().
Related
Care to put light to the subject. Issue is that when I do what ever to code I find that code does not go further to retrieve data from table Sectors and table courses. I have applied find() I use CakePHP 4.x to make clear that it is latest version of CakePHP called Strawberry. I also do not understand that why does my code get affected while I apply simple code structure to run my code.
Take a look at code snippet to run a simple find query with CakePHP Strawberry.
$Sectors is not defined but I need to know a way to define and apply $Sectors variable so that I have given value for relative table structure.
Table Does not load because of a problem I do not understand yet but guys help me out I'll update my code and make use of real code that is to determine a structure for code that will work for CakePHP Strawberry.
code:
$this->loadModel('Sectors');
$this->loadModel('Courses');
$Sectors = $Sectors->find('all')->contain('Sectors', 'Courses');
pr($sectors);
$this->contain(['Sectors', 'Courses']);
$this->set(compact('sectors'));
To retrieve data from table so that I get data relation from Sectors and Courses table.
error:
Call to a member function find() on null
Answer is simple but problem is CakePHP would keep single model with multiple query but not multiple model with single query.
I'd like to know what is the best way of creating models using the Laravel framework? As far as I know, the model is used for the connection to the DB table and then, you can do whatever you want with its data.
Creating a new model per DB table (as stated here) isn't good and models should be created per business object. Not sure what it means. I'd like to access the same table and get some information from it using the Eloquent, so similar to this:
<?php
// Get the model class
use App\Post;
// Grab some data from the posts table
$posts = Post::all();
But it requires having a model. Using the select method, e.g.:
$posts = DB::table('posts')->select('title, body')->get();
Doesn't require having a model so I don't quite understand what's the point of models? I couldn't find enough information inside the Laravel docs: https://laravel.com/docs/5.6/eloquent.
As a full explanation of your question:
Using method:
<?php
// Get the model class
use App\Post;
// Grab some data from the posts table
$posts = Post::all();
will be the same as running a query like:
<?php
$posts = DB::table('posts')->select('*')->get();
but the biggest difference is that the first returns an Eloquent object, while the later returns a normal object. The first model requires a model, which extends eloquent, the later method is using the DB class, which does not extend eloquent. So when you want to use Eloquent you would have to use models.
Creating a new model per DB table (as stated here) isn't good and models should be created per business object. Not sure what it means.
Entities are things like Users, Posts, Comments, messages, but not relation tables such as user_has_posts or user_has_messages. Not entirely sure how else to explain it, but i'm sure you will get the idea.
Also keep in mind, models are supposed to only contain database logics, I've seen it plenty of times where people use models to filter arrays but this should not belong to models.
I know that one can create an object from a model that represents a particular entry in the DB by using ModelName::find($model_id). What I'm curious about is how one goes about grabbing the current ID within the model on a function - so say that I wanted to expand the User model so that it will return all of the songs uploaded by said user.
I want to just be able to call $user = User::find($user_id) to grab the relevant user, and then call the function from within the model like so: $user->list_songs();. How do I, within this function in the model (public function list_songs()), check to see what the current id is? I'm currently passing it as a variable, and this seems counter-intuitive to the idea of a model.
Within the model you can simply use $this->id so long as the model has been saved to the database.
I'am trying to check if the record has relation and at the same time I want to make an eager load so:
User::has('item')->with('item')->get();
I've noticed that with method do not check if the record has a relation, is this the right sequence or there are a shorter way to code this?
Just try this:
// Pull all blog users that have
// at least one related model/item
$users = User::has('item')->get();
Instead of this:
User::has('item')->with('item')->get();
Reference on Laravel website.
I have two Models:
Server (belongs to a Slave)
Slave (hasMany Servers)
In a Controller I need to get an array with information about a Server/Slave (and some additional informations, like business logic that can't be in Controller).. I thought to create a function in Server Model to build the array, and every field that I need from Slave I would just to call a function like this:
Model Server:
// I created this function for code easier to maintain
function getSlaveId($server_id){
$this->id = $server_id;
return $this->field('slave_id');
}
// Return the array that I need, with informations from Server and Slave
function getArrayByServerId($server_id){
$slave_id = $this->getSlaveId($server_id);
return array(
'slave_name' => $this->Slave->getName($slave_id)
// some other information that some did not even pull from some table..
);
}
as you can see, I created a function to pull a field from Server, because if one day that changes, I'll have to change only in one place .. the same goes for the Slave whose I'm thinking of using a function to pull each field too ..
So, the question is: should I to get Slave's info with a function for each field or there is another better way to do? (I need almost whole Slave's table info, except some fields..)
You could use the Containable behaviour which will pull the necessary information without complex logic like this. Using the behaviour you could make the request like:
$this->Server->find('first', array('conditions'=>array(...), 'contain'=>array('Slave')));
This way you don't make your models unnecessary fat (healthy are they :)) and you will take what is required. With containable you can get specific fields too.