Here I have three tables named user, product, project
In the project table, I have two foreignId named user_id and product_id
I want to show project table data using the index method in ProjectController.php
Here I can call user_id in the index method but I cant call product_id. Thanks for help
This is my index method in ProjectController.php
How to write where for product_id? I can`t get product_id value
public function index()
{
$projects = Project::where('user_id',auth()->user()->id)->where('product_id')->latest() ->paginate(20);
return view('projects.index', compact('projects'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
This is products table products.php
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('detail');
$table->string('color');
$table->string('image');
$table->string('logo');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
This is projects table projects.php
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('chapter_name', 255)->nullable();
$table->string('sub_section_name', 500)->nullable();
$table->string('title_1', 255)->nullable();
$table->string('description_1', 5000)->nullable();
$table->string('image_1', 255)->nullable();
$table->string('image_2', 255)->nullable();
$table->string('image_3', 255)->nullable();
$table->string('title_2', 255)->nullable();
$table->string('description_2', 5000)->nullable();
$table->string('title_3', 255)->nullable();
$table->string('description_3', 255)->nullable();
$table->string('video_1', 255)->nullable();
$table->string('video_2', 255)->nullable();
$table->string('video_3', 255)->nullable();
$table->unsignedBigInteger ('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// $table->foreignId('product_id')->nullable();
$table->unsignedBigInteger('product_id')->references('id')->on('products')->onDelete('cascade');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable();
});
This is ProjetcImport.php
return new Project([
'chapter_name' => $row['chapter_name'],
'sub_section_name' => $row['sub_section_name'],
'title_1' => $row['title_1'],
'description_1' => $row['description_1'],
'image_1' => $row['image_1'],
'image_2' => $row['image_2'],
'image_3' => $row['image_3'],
'title_2' => $row['title_2'],
'description_2' => $row['description_2'],
'title_3' => $row['title_3'],
'description_3' => $row['description_3'],
'video_1' => $row['video_1'],
'video_2' => $row['video_2'],
'video_3' => $row['video_3'],
'user_id' => auth()->user()->id,
'product_id' => Product::where('user_id',Auth::id())->pluck('id'))
// 'product_id' => id()->id
]);
Project::whereIn('product_id',Products::where('user_id',Auth::id())->pluck('id')->toArray())->latest()->paginate(20);
I hope it would work for you.
Related
I am working on a referral system, when a user shares his referral link to a guest, and the guest registers the user will be given a bonus (working as expected), but now what I am trying to do is
I would like to give a bonus to the first user that referred a user
For example:
A referred B and
B referred C
Anytime B referred a new user give A bonus
So if C referred a new user give B bonus
Below is my code:
$referrer = User::whereUsername(session()->pull('referrer'))->first();
$user = User::create([
'username' => $request->username,
'name' => $request->name,
'referrer_id' => $referrer ? $referrer->id : null,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
if ($user->referrer !== null)
{
$user->referrer->deposit(400);
// Notification::send($user->referrer, new ReferrerBonus($user));
}
On model:
/**
* A user has a referrer.
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function referrer()
{
return $this->belongsTo(User::class, 'referrer_id', 'id');
}
/**
* A user has many referrals.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function referrals()
{
return $this->hasMany(User::class, 'referrer_id', 'id');
}
On migration file:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->unique();
$table->unsignedBigInteger('referrer_id')->nullable();
$table->foreign('referrer_id')->references('id')->on('users');
$table->string('email')->unique();
$table->integer('is_admin')->default(0);
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->unique();
$table->unsignedBigInteger('referrer_id')->nullable();
$table->foreign('referrer_id')->references('id')->on('users');
$table->string('email')->unique();
$table->integer('is_admin')->default(0);
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
I'm trying to create or attach data to table called "user_votes", but I dunno why the connections between "Vote" and "User" doesn't work.
user_votes table
Schema::create('user_votes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('vote_id')->unsigned();
$table->string('name');
$table->string('page', 100)->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('vote_id')->references('id')->on('votes');
});
Vote model:
public function user_votes()
{
return $this->belongsToMany(User::class, 'user_votes');
}
User model:
public function user_votes()
{
return $this->belongsToMany(Vote::class, 'user_votes');
}
store controller:
auth()->user()->user_votes()->attach([
'name' => $option->name,
'page' => $option->page
]);
Thanks for any ideas.
Please try the below code hope it will help you.
$voteId = 1;
auth()->user()->user_votes()->attach($voteId => [
'name' => $option->name,
'page' => $option->page
]);
What is the reason for the error? This has happened after changing the code of the default when registering. Please take me
SQLSTATE[HY000]: General error: 1364 Field 'img' doesn't have a default value (SQL: insert into `users` (`username`, `password`, `role`, `updated_at`, `created_at`) values (aaaaaaa, .....G, user, 2019-01-14 16:40:05, 2019-01-14 16:40:05))
protected function validator(array $data)
{
return Validator::make($data, [
'username' => 'required|string|check_username|max:255|unique_username',
'password' => 'required|string|min:6',
'captcha'=>'required|captcha'
],[],[
'username'=>'user',
'password'=>'pass',
'captcha'=>'cap',
'img'=>'profile',
'fnamelname'=>'fnamelname',
]);
}
protected function create(array $data)
{
return User::create([
'username' => $data['username'],
'password' => bcrypt($data['password']),
'role'=>'user'
]);
}
Try this:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('password');
$table->string('img')->default(1);
$table->string('fnamelname');
$table->string('role');
$table->rememberToken();
$table->timestamps();
});
}
EDIT:
This is a better version
$table->string('img')->nullable();
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('password');
$table->string('img');
$table->string('fnamelname');
$table->string('role');
$table->rememberToken();
$table->timestamps();
});
}
/**
If the img field is optional, your data model should reflect that intent clearly.
In your migration:
$table->string('img')->nullable();
will set this column as nullable, thus you can create a record without setting a value explicitly.
See https://laravel.com/docs/5.0/schema#adding-columns
Also, you'd better use a BLOB type instead of a string to store binary content like images. But that's another topic.
So I have a project table:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->string('reference')->nullable();
$table->date('started')->nullable();
$table->date('ended')->nullable();
$table->string('industry')->nullable();
$table->string('operatives')->nullable();
$table->timestamps();
$table->softDeletes();
});
And I have an hours table:
Schema::create('hours', function (Blueprint $table) {
$table->increments('id');
$table->string('hours');
$table->date('date')->nullable();
$table->text('notes')->nullable();
$table->integer('project_id');
$table->integer('user_id');
$table->softDeletes();
$table->timestamps();
});
Now, is there anyway to create an association with both the project_id and user_id in one call?
I know I can do the following (adding the user_id to the hours):
$hours = [
'hours' => $request->hours,
'date' => $request->date,
'operatives' => $request->operatives,
'notes' => $request->notes,
'user_id' => auth()->user()->id,
];
$create = $project->hours()->save(new $this->hour($hours));
But I am trying to do something like so:
$hours = [
'hours' => $request->hours,
'date' => $request->date,
'operatives' => $request->operatives,
'notes' => $request->notes,
];
$create = $project->hours()->save(auth()->user()->save($hours));
Both user and project have the same hours relation in their classes:
/**
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function hours(): HasMany
{
return $this->hasMany(Hour::class);
}
Is this possible, if so, how would I go about doing it?
I think the best way to handle this would be to separate the saving of Hours as an independent instance of the model and then sync it with both like so:
$hour = Hour::create($hours);
$project->hours()->syncWithoutDetaching([$hour->id]);
$user->hours()->syncWithoutDetaching([$hour->id]);
this is my code in users tabel (migrations)
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('level')->default('user');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
when I want to change level manually like below code
return User::create([
'level' => 'admin',
'name' => 'emad',
'email' => 'emad#gmail.com',
'password' => bcrypt('123456')
]);
In the level field, the same user value is saved and admin dosent save.
I do not know why?
Judging from the little code here, i would say you haven't added the level column to the fillable model variable.Like so:
protected $fillable = ['level','any_other_field_you_want_to_assign'];
Add this line of code to the top of your User model class
Check the documentation here