How to get record from post table in laravel using eloquen relationships - laravel-5

I have tow table first table is posts table and second table is home_posts table so i want to home_posts table (post_id) throw get record in posts table.
I was used hasMany() relationship in post model but not getting any record. I want only home_posts table post id record from posts table. I want home post table to post table record.
Post table
+----+-----------------+
| id | title |
+----+-----------------+
| 1 | Post 1 |
| 2 | Post 3 |
| 26 | Post 4 |
| 27 | Post 5 |
| 28 | Post 6 |
| 29 | Post 7 |
| 30 | Post 8 |
| 32 | Post 9 |
+----+-----------------+
home_post table
+----+------------+
| id | post_id |
+----+------------+
| 1 | 28 |
| 2 | 29 |
| 3 | 2 |
+----+------------+

In Post model:
public function home_posts()
{
return $this->hasMany('App\HomePost', 'post_id', 'id');
}
In HomePost model:
public function post()
{
return $this->belongsTo('App\Post', 'post_id', 'id');
}
To get post through home_post:
$homePost = \App\HomePost::find(1);
$post = $homePost->post;
or
$posts = \App\HomePost::has('post')->get();
To get home_post through post:
$post = \App\Post::find(1);
$homePost = $homePost->home_posts;
or
$homePost = \App\Post::has('home_posts')->get();
For more detail read this: One To Many
Hope it help you :)

Related

Laravel Copy record and duplicate with User IDs

How can i copy a record and save it with a different value for 1 or more field? for example
I have a table(post table) with following columns - ID | User ID | Post Title | Post Type
And User Table(user) with following columns - ID | Username | Email | Password
What i need to do is - When the saving post data needs to be duplicate with user ids
For example - In user table there is 10 users and when i save the post it should be duplicate for all ten user id
ID | User ID | Post Title | Post Type
1 | 1 | Test Post | Public
2 | 2 | Test Post | Public
3 | 3 | Test Post | Public
4 | 4 | Test Post | Public
5 | 5 | Test Post | Public
6 | 6 | Test Post | Public
7 | 7 | Test Post | Public
8 | 8 | Test Post | Public
As suggested, you can do this using model replication. Like
$post = Post::find(12892); //change with your id.
// get all the users.
$users = User::all();
foreach($users as $user){
$newPost = $post->replicate();
$newPost->user_id = $user->id;
// save post.
$newPost->save();
}

Join tables and fetch from Many to many (Laravel)

User Table
|---------------------|------------------|
| id | email |
|---------------------|------------------|
| 3 | 18 |
|---------------------|------------------|
Post Table
|---------------------|------------------|
| id | Title |
|---------------------|------------------|
| 12 | My Post |
|---------------------|------------------|
Category Table
|---------------------|------------------|
| id | Title |
|---------------------|------------------|
| 18 | My Category |
|---------------------|------------------|
Category-Post
|---------------------|------------------|
| post_id | category_id |
|---------------------|------------------|
| 12 | 18 |
|---------------------|------------------|
User-Category
|---------------------|------------------|
| user_id | category_id |
|---------------------|------------------|
| 3 | 18 |
|---------------------|------------------|
In this table structure, I want to fetch all the post with any given user id, according to their favorite category, with a single query.
I have defined the relations for all the models
Category:
public function posts(){
$this->belongsToMany(Post::class,'category-post','category_id','post_id')
}
Post:
public function categories(){
$this->belongsToMany(Category::class,'category-post','post_id','category_id')
}
User:
public function favCategories(){
$this->belongsToMany(Category::class,'user-category-','user_id','category_id')
}
Can try this with your relationship model
$data = User::with("favCategories.categories.posts")->find($user_id);
// "favCategories" get first of all favourite categories
// "categories" get all category
// "posts" get all post with these categories wise
Here is the single eloquent query:
<?php
dd($posts = User::find(3) //user id 3
->categories()->where('id', 18) //favorite category id 18
->posts);

How can I get a post with all related ones?

Here is my table structures:
// tickets
+----+------------+----------------------+--------+---------+
| id | subject | content | closed | user_id |
+----+------------+----------------------+--------+---------+
| 1 | subject1 | question1 | 0 | 123 |
+----+------------+----------------------+--------+---------+
// answers
+----+----------------------+---------+-----------+
| id | content | user_id | ticket_id |
+----+----------------------+---------+-----------+
| 1 | answer1 | 123 | 1 |
| 2 | answer2 | 456 | 1 |
+----+----------------------+---------+-----------+
Now I need to get a ticket with along all its answers. I can do that using pure SQL like this:
SELECT t.*, a.*
FROM tickets t
LEFT JOIN answers a
ON t.id = a.ticket_id
WHERE t.id = ?
Now I'm using Laravel and I want to do that using with() class (however not sure it is possible). Any idea what change should I make in the models for doing that?
Noted that I can get just a ticket in Laravel like this:
$ticket = Tickets::where('id', $request->id)->get();
First make a hasMany() relationship in your Tickets model class.
public function answers()
{
return $this->hasMany(Answers::class, 'ticket_id', 'id');
}
And then in your controller query
$tickets = Tickets::with('answers')->where('id', $request->id)->get();

Laravel Eloquent relationships - hasOne?

I have a question table which has a type_id column. The value defined here relates to the QuestionTypes model.
Question Table:
--------------------------
| id | title | type_id |
--------------------------
| 1 | apple? | 2 |
| 3 | banana? | 2 |
| 4 | kiwi? | 2 |
| 5 | pear? | 3 |
--------------------------
QuestionTypes
----------------
| id | title |
----------------
| 1 | multi |
| 2 | single |
| 3 | free |
----------------
In the Questions Model I have:
public function type()
{
return $this->hasOne(QuestionType::class);
}
I would like to print the title from questiontypes table but when I try to output in the view using $question->type->title I get:
Column not found: 1054 Unknown column 'x__questiontypes.questions_id' in 'where clause'
(SQL: select * from `x__questiontypes` where `x__questiontypes`.`questions_id` = 2 and `x__questiontypes`.`questions_id` is not null limit 1
Have I mixed up the relationships?
Solution via Attaching a hasOne model to another Laravel/Eloquent model without specifying id
Updated to Questions model:
public function type()
{
return $this->belongsTo(QuestionType::class, 'type_id');
}
Added to QuestionType model:
public function questions()
{
return $this->hasMany(Question::class, 'type_id');
}

Insert multiple values on multiple rows

I have a form that has a dropdown where the user can select up to 5 countries.
I have two related tables, users and countries, where each user can have multiple countries selected.
The users table:
+----+----------+----------------------+
| id | username | description |
+----+----------+----------------------+
| 1 | user1 | Some description. |
| 2 | user2 | Another description. |
+----+----------+----------------------+
The countries table:
+----+---------+---------------+
| id | user_id | country |
+----+---------+---------------+
| 1 | 1 | Canada |
| 2 | 1 | United States |
| 3 | 1 | France |
| 4 | 2 | Spain |
| 4 | 2 | Italy |
+----+---------+---------------+
As you can see, the country dropdown values should each have their own row in the table.
Here is my UserController so far (it only inserts into the users table for now):
$user = new User;
$user->username = Input::get('username');
$user->description = Input::get('description');
$user->save();
How would I insert multiple dropdown values such that each has its own row? What should my models look like for this to work?
If you're gonna use eloquent to do it, here's a suggestion:
//in your User model
public function countries()
{
return $this->hasMany('Country');
}
//insert from the controller
$countries = json_decode(Input::get('countries'), true); //assuming you use json to submit the multiple countries
$user_countries = array();
foreach($countries as $country)
{
$user_countries[]=new Country(array('country'=>$country));
}
//save the countries into the DB
$user->countries()->saveMany($user_countries);
for more info, do refer to the documentation:
http://laravel.com/docs/4.2/eloquent#inserting-related-models

Resources