I've been tackling on Laravel and I haven't found a few answers to minor questions.
Is there anyway that in the model I can create ALIASES for columns?
If not, I've been trying a few variations for my queries but none seem to give me what I want.
public function getEditHeader($adhID){
return View::make('adventures.editHeader')
->with('adventureheader', AdventureHeader::find($adhID)->select('adhID', 'adhTitle AS title', 'adhDescription AS description')->get())
->with('adventuretypes', AdventureType::orderBy('adtDescription')->lists('adtDescription', 'adtID'));
}
I've tried many variants but I feel like i'm not comprehending what I'm actually attempting with Laravel. I just want my queries to come out with alias because i dont have my tables prefix in the form's input names.
Use the selectRaw method:
AdventureHeader::find($adhID)
->selectRaw('adhID, adhTitle AS title, adhDescription AS description')->get()
Whoever is looking to achieve ALIASES please take a look into this link:
Laravel 4 Eloquent Column Alias
Related
I've created migrations and also their relationships. looks like I'm using polymorphic relations, because in one table, I could have the value of one model or i could have the value of another model. as you might guess, I have something like this
ID Mappable_type Mappable_id value
Mappable_type could be one or many other models. mappable_id will be appropriate id from that model. I needed this because I'm creating dynamic form builder and that's how I designed a database.
Now I need to fetch some records from 5 tables together, so I gotta use joins. the table where I have mappable_type is at the third place from those 5 joins. now What should I do also to only fetch data from these 5 joins that have specific model type that is in that morph table? I'm doing something like that for now:
$final = Dynamic::join('dynamic_properties_values','dynamic_properties_values.dynamic_id','=','dynamic.id')
->join('dynamic_properties_mapping','dynamic_properties_mapping.id','=','dynamic_properties_values.mapping_id')
->where('dynamic_properties_mapping.mappable_type','=','App\Driver')
->get();
As you see, I have written by hand something like
"App\Driver"
. It doesn't matter I have written it by hand or had it saved into a variable and than used it here. WHat really bothers me is that let's say when I was inserting data into these morph tables, Model got saved as "App\Driver". what if I make a mistake and when fetching with joins, I write something like App\Http\Driver? or what If I changed the location of the model itself in my code project?
What's the best practice not to make those kind of errors by the time my project reaches huge amount of users?
I'm trying to get data from multiple relationships. But I don't want to end up with multiple queries. So the with method won't work for me.
I want to use joins to get the data needed, but laravel overwrites keys if they are duplicated. Is there a way to save the results from a join as a relation. Something like this below (join is incorrect I know).
Post::select('post.*', 'category.*')->join('category');
If both have an 'id' field it's overwritten by the other. So I would like to have category.* results as a relation so I can call ->category->id like I can when I use the with method.
Is there any way to do this?
You may use the whereHas and orWhereHas methods
Check this link:
http://laravel.io/forum/04-19-2014-many-to-many-to-many-in-wherehas
And Laravel Documentation:
https://laravel.com/docs/5.1/eloquent-relationships
I write this line at my controller
<i>
$model_town=ResTown::model()->with(array('get_by_town'))->findAll(array('limit'=>'3'));
</i>
I need to make get_by_town with limit=3 and user_active=1
I got all records but conditions don't work
If you would like to add condition on related table you should write it inside with() method.
So, you should get something similar `
->with('get_by_town'=>array('limit'=>3, 'user_active'=>1))
But in some cases it can not helps you, because it depends on many things: for example, relation type, join type and so on.
The easiest way to work with ActiveRecord for me, first of all write down pure sql, and then convert to ActiveRecord representation. Thus, I get optimized query and exactly knowing of what I'm doing %)
So far I have the following model:
class Listing extends Eloquent {
//Class Logic HERE
}
I want a basic function that retrieves the first 10 rows of my table "listings" and passes them on to the view (via a controller?).
I know this a very basic task but I can't find a simple guide that actually explains step-by-step how to display a basic set of results, whilst detailing what is required in the model, controller and view files.
First you can use a Paginator. This is as simple as:
$allUsers = User::paginate(15);
$someUsers = User::where('votes', '>', 100)->paginate(15);
The variables will contain an instance of Paginator class. all of your data will be stored under data key.
Or you can do something like:
Old versions Laravel.
Model::all()->take(10)->get();
Newer version Laravel.
Model::all()->take(10);
For more reading consider these links:
pagination docs
passing data to views
Eloquent basic usage
A cheat sheet
The simplest way in laravel 5 is:
$listings=Listing::take(10)->get();
return view('view.name',compact('listings'));
Another way to do it is using a limit method:
Listing::limit(10)->get();
This can be useful if you're not trying to implement pagination, but for example, return 10 random rows from a table:
Listing::inRandomOrder()->limit(10)->get();
this worked as well IN LARAVEL 8
Model::query()->take(10)->get();
This also worked in Laravel 9
Model::query()->take(10)->get();
Is there a way to retrieve the column's comment (like from INFORMATION_SCHEMA.COLUMN table on MySQL) from a certain table without actually "hardcode" the query with Doctrine ORM?
Finally I found an way. what I was trying to do was get a column comment from inside a Controller
//lets say we have a table named 'product'
//and we want to get the comment from the 'name' column
//first we get a list of columns from 'product'
$columns = $this->getDoctrine()->getEntityManager()->getConnection()->getSchemaManager()->listTableColumns('product');
//then we just access getComment function from the 'Column' class
//for the 'name' column:
echo $columns['name']->getComment();
I doubt it, and the same would go for Propel as well. Both use PDO, and afaik there is no database-neutral way to do this.
To achieve this, you can often query the table columns using SELECT statements on system tables (certainly Oracle and PostgreSQL allow this). However, the names of the tables involved differ from one vendor to another. Here is how to get column names, for example, in PostgreSQL - getting comments would probably be quite similar.
It would be quite possible for an ORM to offer what you want, but the underlying implementation would be the approach I outline above. Perhaps you could request it on the Doctrine mailing list?