Reputation: 11
I'm trying to do a one to many relationship using laravel 5.1 . The problem is that when I create a new product it doesn't belong to any user in my database. I have 2 tables in my database: products and users. One of the rows in my products table is user_id.
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$inputs = $request->all();
$product = Product::Create($inputs);
return redirect()->action('ProductController@index');
}
This is the code in my ProductController. And this is code in my model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'name',
'price',
'descr',
'image',
'sale',
'user_id'
];
public $timestamps = false;
protected $table = 'products';
public function books(){
return $this->hasMany('App\Books');
}
}
I have no idea how to do this whatever I try. If I've forgotten to add anything above tell me. Thank you
Upvotes: 1
Views: 7466
Reputation: 5099
For one to many relationship or any relationship you need to have correct database structure. For example in your case you have two table products
and users
Lets just create migration for your tables products
and users
Migration
Schema::create('users',function(Blueprint $table){
$table->increaments('id');
$table->string('name');
//and so on ...
});
So basically to identify users you've got users.id
column.
Schema::create('products',function(Blueprint $table){
$table->increaments('id');
$table->string('product_name');
$table->integer('user_id')->unsigned();
//and so on ...
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Now you have your users
table and your products
table. And there's a relationship between your tables is users can have products.
Models
class User extends Model {
public function products(){
return $this->hasMany('\App\Product'); //Product Model Name
}
}
Now if you want to access products
which belongs to users
you'll have to do this in your controller.
$user = Users::find(1); //lets say for test we just took firs user
return $user->products()->get();
This will give you a clean json of user details and along with the products which is associated with that particular user.
NOTE: In your case you don't have any user_id associated with your product details, in that case it wont work as there should be some relationship.
For Storing the data into database
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$inputs = $request->all();
$product = Product::Create($inputs);
return redirect()->action('ProductController@index');
}
Now you're mass assigning the values into database so $request->all()
must have user_id
of the logged user.
So create a hidden field in the form area of products.create
view.
<form action="someaction" method="POST">
<input type="hidden" name="user_id" value="{{Auth::user()->id}}"/>
<input type="text" name="product_name" />
</form>
Now your $request->all()
method will contain key user_id
and its value form logged in user.
Auth::user()
gives the information of logged in users;
Alternatively in your controller you can do like this too..
public function store(Request $request)
{
$request->user_id = Auth::user()->id; //assigning user_id value to the current logged in user's id (this has to be before adding the request to the inputs variable so when you execute ::Create($inputs) it will be there)
$inputs = $request->all();
$product = Product::Create($inputs);
return redirect()->action('ProductController@index');
}
Hope this would help. :)
Upvotes: 4