Laravel Copy record and duplicate with User IDs - laravel

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();
}

Related

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. How to get relationships where foreign key is an array

I am trying to retrieve database rows with their relationships. However, the local key is an array. Let me explain using an example.
Lets say I have a table of countries and a table of pages. Each country can have many pages. Each page can belong to multiple countries. I do not have the flexibility to change this schema.
pages
+-------------+-----------+
| id | name | countries |
+-------------+-----------+
| 1 | Page 1 | 1 |
+-------------+-----------+
| 2 | Page 2 | 1,2,3 |
+-------------+-----------+
| 3 | Page 3 | 4,5,6 |
+-------------+-----------+
countries
+----+----------------+
| id | name |
+----+----------------+
| 1 | United States |
+----+----------------+
| 2 | United Kingdom |
+----+----------------+
| 3 | Germany |
+----+----------------+
| 4 | France |
+----+----------------+
| 5 | Hong Kong |
+----+----------------+
| 6 | Thailand |
+----+----------------+
| 7 | Belgium |
+----+----------------+
| 8 | Singapore |
+----+----------------+
My model and controller look something like:
country
public function pages()
{
return $this->hasMany(Page::class, 'id', 'countries');
}
MemberController.php
$countries = Country::with('pages')->get();
This is returning all countries, but only Page 1 contains any relationships.
Is there a way to retrieve relationships using a whereIn approach so all three countries will return appropriate pages?
Thanks in advance
Since Page can belong to many Countries, you need to create a pivot table called country_page and remove the countries column.
Then define two belongsToMany() relationships in both models:
public function pages()
{
return $this->belongsToMany(Page::class);
}
If you're not following Laravel naming conventions listed in my repo and you gave the pivot name a custom name, define it too:
public function pages()
{
return $this->belongsToMany(Page::class, 'custom_pivot_table');
}
Something like this ?
$datas = Pages::where( ##your conditions### )->get()->inArray();
$countries = Countries::pluck('name','id'); // return array of id=>name
foreach($datas as $key=>$data) {
$c = [];
foreach(explode(',',$data['countries']) as $country_id) {
$c[]=$countries[$country_id];
//or
$c[]= ['id'=>$country_id,'name'=>$countries[$country_id]];
}
$datas[$key]['countries']= $c;
}

laravel 5 Relationship has one

User table
id | name | user_type | email | password
1 | John | 1 | john#gmail.com | something
2 | Roy | 1 | roy#gmail.com | something
3 | Rax | 1 | Rax#gmail.com | something
4 | Ren | 1 | ren#gmail.com | something
AssignUser Table
id | user_id | assign_to_math | assign_to_chemistry
1 | 3 | 2 | 1
2 | 4 | 1 | 2
So, here is users table where four users are avail
Now in assign table first row Rax is assign tuitoin for math to Roy and chemistry to John
I want to get the detail of john and roy with relation
I tried in User models
public function getInfoFormathTutor(){
return $this->hasOne('App\AssignUser', 'id', 'assign_to_math');
}
It return for me null value don't know why please help
Thanks in advance
try:
public function getInfoFormathTutor(){
return $this->hasOne('App\AssignUser', 'assign_to_math', 'id');
}

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

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 :)

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