How to store data using relation in laravel? - php

i have two table
posts
id|post_title|post_content
post_images
id|images|post_id
Controller
public function AddPost(Request $request)
{
Post::create($request->all());
// PostImage::create();
return Redirect::to('Post');
}
Also i have added Relation
class Post extends Model
{
protected $table = 'posts';
public function images()
{
return $this->hasMany('App\PostImage');
}
}
class PostImage extends Model
{
public function post()
{
return $this->belongsTo('App\Post');
}
}
I have one form where i adding post title ,post content and selecting multiple images. My question is how I can store post images along with post id in post_images table?

In you controller AddPost function try (using Form model binding)
$post = new Post($request->all());
PostImage::post()->images()->save($post);
Or you can also do like this I think
public function AddPost(Post $post, Request $request)
{
$input = Input::all();
$input['post_id'] = $post->id;
PostImage::create( $input );
return Redirect::to('Post');
}

Related

Laravel 5 push() not saving hasOne relationship

In my destroy function of my controller, I'm attempting to take a copy of my object (model with one relationship) and insert it into another database before deleting it. However only the model is being created and not the relationship. Why is this happening?
Destroy function:
public function destroy($id)
{
$user = User::with('Phone')->find($id);
$archive = $user->replicate();
$archive ->changeConnection('mysql2');
$archive ->push();
}
User model:
public function phone()
{
return $this->hasOne('App\Phone');
}
Phone model:
public function user()
{
return $this->belongsTo('App\User');
}
When I insert via the store function in my controller, it sets up the relationship just fine:
public function store(Request $request)
{
// Validation has passed, insert data into database
$user= User::create($request->all());
$user->Phone()->create($request->all());
}

Retriving two models and order them by date

I'm trying to retrive the 20 most recent elements from two differents models Post and Link and rank them by the field created_at in descending order.
Here is the link class
class Link extends \Illuminate\Database\Eloquent\Model {
public $incrementing = false;
public function author() {
return $this->belongsTo('App\Models\Author');
}
public function category() {
return $this->belongsTo('App\Models\Category');
}
}
And here the post class
class Post extends \Illuminate\Database\Eloquent\Model {
public $incrementing = false;
public function author() {
return $this->belongsTo('App\Models\Author');
}
public function category() {
return $this->belongsTo('App\Models\Category');
}
}
How can I do that with eloquent (I'm using it outside Laravel, with Slim)?
use the below code to get recent records
orderBy('created_at', 'DESC')->get();
The merge method was the answer :
$posts = \App\Models\Post::where('draft', false)->orderBy('created_at', 'desc')->take(20)->get();
$links = \App\Models\Link::orderBy('created_at', 'desc')->take(20)->get();
$result = $posts->merge($links)->sortByDesc('created_at')->take(20);

How to insert data using relation?

I have two table
posts
id|post_title|post_content
post_images
id|images|post_id
Controller
public function AddPost(Request $request)
{
Post::create($request->all());
// PostImage::create();
return Redirect::to('Post');
}
Also i have added Relation
class Post extends Model
{
protected $table = 'posts';
public function images()
{
return $this->hasMany('App\PostImage');
}
}
class PostImage extends Model
{
public function post()
{
return $this->belongsTo('App\Post');
}
}
I have one form where i adding post title ,post content and selecting multiple images. My question is how I can store post images along with post id in post_images table?
Rewrite your code like this
class Post extends Model
{
protected $table = 'posts';
//add $fillable here
protected $fillable = ['post_title', 'post_content'];
public function images()
{
return $this->hasMany(App\PostImage::class);
}
}
class PostImage extends Model
{
protected $table = 'post_images';
//add $fillable here
protected $fillable = ['images', 'post_id'];
public function post()
{
return $this->belongsTo(App\Post::class);
}
}
Now in your controller
Post::create($request->all())->images()->create($request->all());
Here Post create saves your data to db also returns the object then
you accesses the relation images and saves data to images.
See here for more details One To Many

Laravel post with comment error

So i created an app with login users and users can post, now the post is working just fine but when i put a comment function, i get an error "Invalid argument supplied for foreach()"
i have this in my Post model
<?php
class Post extends Eloquent{
protected $fillable = array('email', 'password','title','content');
protected $table = 'posts';
public function User()
{
return $this->belongsTo('User');
}
public function Comment()
{
return $this->hasMany('Comment', 'post_id');
}
}
And in my Comment model
<?php
class Comments extends Eloquent {
public function post()
{
return $this->belongsTo('Post');
}
}
and i have this in my Controller
public function viewPost($id)
{
$post = Post::find($id);
$user = Auth::user();
$this->layout->content = View::make('interface.viewPost')->with('posts', $post )->with('users',$user);
}
and in my views
<section class="comments">
#foreach($posts->comments as $comment)
<blockquote>{{$comment->content}}</blockquote>
#endforeach
</section>
now when i try do run dd($posts->comment)
it returns a null because the comment table is empty. now what i want to know is why im getting this error? thanks for your help im just curious why this error is returning and i want to solve this one thanks
In your Post Model Comment is the function name not the comments and Comment method making relation with wrong name model under hasMany method call.
public function Comment()
{
return $this->hasMany('Comments', 'post_id');
}
Try to use
#foreach($posts->Comment as $comment)
If still inaccessible then try to eager loading.
like in your controller
$post = Post::with("comment")->find($id);

Laravel Repositories Relationhips

I have a simple relationship between users and posts(User model and Post model) and i am using Repositories.
<?php namespace St0iK\Storage\Post;
use Post;
class EloquentPostRepository implements PostRepository {
protected $errors;
public function all()
{
return Post::all();
}
public function find($id)
{
return Post::find($id);
}
public function create($input)
{
return Post::create($input);
}
}
In my controller i have:
public function store()
{
// Get input from the form
$input = Input::all();
$input['user_id'] = (int)Auth::user()->id;
$p = $this->post->create( Input::all() );
if( $p->errors()->isEmpty() )
{
return Redirect::route('home')->with('flash', 'The new post has been created');
}
return Redirect::route('upload.create')->withInput()->withErrors($p->errors());
}
But the 'post' is not inserted in the database. I get validation errors(i am using Ardent) that all my fields are missing!
But if i try something like that in my controller:
$user = User::find( (int)Auth::user()->id);
$user->posts()->save($post);
it works just fine.
How can i implement this to my Post Repository so i can insert a new Post with re related user and pass the user_id?

Categories