How to insert data using relation? - 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?

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

Related

laravel create, delete, update hasMany with many fields

I'm new to laravel. I have 2 table
class Client extends Model
{
protected $fillable = ['name'];
public function clientmoreinfo()
{
return $this->hasMany('App\Moreinfoclient');
}
}
class Moreinfoclient extends Model
{
protected $table = 'clients_additionalfield';
protected $fillable = ['client_id', 'nameinput', 'valueinput'];
public function clients()
{
return $this->belongsTo('App\Client');
}
}
and i need to be able to add many additional fields at once as seen here:
this is how I add any number of entries, and more importantly, how do I update or remove them later?
thank you all in advance !

How to store data using relation in laravel?

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

Check if category belongs to post

Hello I'm trying to make script that show all categories and if category belongs to post this category should be checked with checkbox. My script wont works could you help me please. It shows only one checked category when have to shows two.
Controller Category model:
class Category extends Eloquent {
protected $table= "categories";
public function posts(){
return $this->belongsToMany('Post');
}
}
Post model
class Post extends Eloquent {
protected $table = 'posts';
public function categories(){
return $this->belongsToMany('Category');
}
}
catRelation:
[{"id":"45","post_id":"132","category_id":"1","created_at":"2014-12-26 20:32:41","updated_at":"2014-12-26 20:32:41"},{"id":"46","post_id":"132","category_id":"3","created_at":"2014-12-26 20:32:41","updated_at":"2014-12-26 20:32:41"}]
all categories: [{"id":"1","name":"Laravel","slug":"laravel","created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00"},{"id":"3","name":"PHP","slug":"php","created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00"}]
A category shouldn't ever belong to a post, but rather a post should belong to a category.
I assume catRelation is your pivot table. It's worth nothing that you need neither id, created_at or updated_at for this.
Example models:
class Post extends Eloquent
{
protected $table = 'posts';
public function category()
{
return $this->belongsToMany('Category', 'catRelation', 'category_id');
}
}
class Category extends Eloquent
{
protected $table = 'categories';
public function post()
{
return $this->belongsToMany('Post', 'catRelation', 'post_id');
}
}
Note: I may have got the final arguments in belongsToMany the wrong way around.

Laravel Eloquent: How to automatically fetch relations when serializing through toArray/toJson

I figures this works for automatically fetching user and replies when I am serializing my object to JSON, but is overriding toArray really the proper way of doing this?
<?php
class Post extends Eloquent
{
protected $table = 'posts';
protected $fillable = array('parent_post_id', 'user_id', 'subject', 'body');
public function user()
{
return $this->belongsTo('User');
}
public function replies()
{
return $this->hasMany('Post', 'parent_post_id', 'id');
}
public function toArray()
{
$this->load('user', 'replies');
return parent::toArray();
}
}
Instead of overriding toArray() to load user and replies, use $with.
Here's an example:
<?php
class Post extends Eloquent
{
protected $table = 'posts';
protected $fillable = array('parent_post_id', 'user_id', 'subject', 'body');
protected $with = array('user', 'replies');
public function user()
{
return $this->belongsTo('User');
}
public function replies()
{
return $this->hasMany('Post', 'parent_post_id', 'id');
}
}
Also, you should be using toArray() in your controllers, not your models, like so:
Post::find($id)->toArray();
Hope this helps!
I must submit a new answer since I'm a SO pleb. A more proper way to accomplish this for those finding this on Google like I did would be to avoid using protected $with if you don't have to and instead move that with() call to your retrieval.
<?php
class Post extends Eloquent
{
protected $table = 'posts';
protected $fillable = array('parent_post_id', 'user_id', 'subject', 'body');
public function user()
{
return $this->belongsTo('User');
}
public function replies()
{
return $this->hasMany('Post', 'parent_post_id', 'id');
}
}
And then you could modify the Post call to pre-load as needed:
Post::with('user','replies')->find($id)->toArray();
This way, you won't be including un-needed data every time you grab a record, if you don't need it.

Laravel 4 Eloquent one-to-many relations with morph

I'm pretty new to Laravel and Eloquent, so this could easily be something trivial but anyways I've been missing a good solution so far. So heres my problem:
I have a main gallery and i want the user to be able to upload any images or videos that he needs into that main gallery and select images/videos from that gallery to be shown elsewhere on the website (basically what i'm doing is centralising multimedia content all in one place).
Using deduction (with all my poor knowledge of Eloquent) i found this as best solution for me:
//Gallery Model
class Gallery extends Eloquent {
protected $table = 'gallery';
protected $guarded = array();
public static $rules = array();
public function images() {
return $this->morphMany('Image', 'imageable');
}
public function videos() {
return $this->morphMany('Video', 'videoable');
}
}
//About Orchestra Model
class OrchestraAbout extends Eloquent {
protected $table = 'orchestra_about';
protected $guarded = array();
public static $rules = array();
public function images() {
return $this->morphMany('Image', 'imageable');
}
public function videos() {
return $this->morphMany('Video', 'videoable');
}
}
//Image Model
class Image extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function imageable()
{
return $this->morphTo();
}
}
//Video Model
class Video extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function videoable()
{
return $this->morphTo();
}
}
And when inserting into database i simply duplicate the row and only change the imageable_id /videoable_id and imageable_type/videoable_type to match the OrchestraAbout Model.
Anyone here knows of a better solution for this?
Thanks for all the smart answers! Cheers!

Categories