Laravel 5.6 - Using model functions in ModelFactory - laravel

I am working with Laravel 5.6 and found myself a weird problem while extending the functionality of my project.
Right now i need to create two new models: order and item. It was quite easy to fill the items table with dummy data using Faker and Laravel Factories/Seeders. The biggest problem is while working with the order model.
This little fellow is related to a company with a foreign key named company_id and user with a foreign key named seller_id. The company field is okay, the trouble is behind my seller_id
This seller needs a role related to the company my factory will randomly pick for it because the user is not related to the company (directly) and i can't just look for it with a company_id.
In order to get all the users "related" to my company, i've created the next function on my Company model:
public function users()
{
$roles = $this->roles;
$users = [];
foreach ($roles as $role) {
foreach ($role->users as $user) {
$user->makeHidden(['pivot']);
array_push($users, $user);
}
}
$users = array_unique_objects($users);
return $users;
}
btw: I'm using laravel-permissions, a library made by Spatie.
What this functions does is get every role from a company and then it pushes it to an array of users.
This custom helper: array_unique_objects tracks any repeated user on my array and removes them.
That function works find because i've tested on a couple of controllers so i know there is no problem with it. Either way, my OrderFactory.php looks like this:
<?php
use Faker\Generator as Faker;
use App\Models\User;
use App\Models\Company;
$factory->define(App\Models\Order::class, function (Faker $faker) {
$company = Company::get()->random(1);
$users = $company->users();
$user = array_random($users);
return [
'company_id' => $company,
'seller_id' => $user->id,
'code' => strtoupper(str_random(10)),
'description' => $faker->sentence($nbWords = rand(2, 4), $variableNbWords = true),
'created_at' => $faker->dateTimeBetween($startDate = '-1 year', $endDate = 'now', $timezone = null)
];
});
But when i run the php artisan db:seed command, it throws the next error in console:
BadMethodCallException : Method Illuminate\Database\Eloquent\Collection::users does not exist.
at >/home/ironman/Documentos/Sandbox/Proventas/Backend/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:99
95| */
96| public function __call($method, $parameters)
97| {
98| if (! static::hasMacro($method)) {
99| throw new BadMethodCallException(sprintf(
100| 'Method %s::%s does not exist.', static::class, $method
101| ));
102| }
103|
Exception trace:
1 Illuminate\Support\Collection::__call("users", [])
/home/ironman/Documentos/Sandbox/Proventas/Backend/database/factories/OrderFactory.php:10
2 Illuminate\Database\Eloquent\Factory::{closure}(Object(Faker\Generator), [])
/home/ironman/Documentos/Sandbox/Proventas/Backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:274
Please use the argument -v to see more details.
Is there anything I can do to fix this problem? I know that using Laravel Relationships will fix my problem but the specifications of this project says that i have to keep things just as the are.

Your call to
$company = Company::get()->random(1);
does not return a single company. It returns a Collection, which does not have a users dynamic function. Try
$company = Company::get()->random(1)->first();

Related

Laravel - Get array with relationship

I have an ajax call that returns an array:
$reports = Report::where('submission_id', $submissionID)
->where('status', 'pending')
->get(['description','rule']);
return [
'message' => 'Success.',
'reports' => $reports,
];
From this array, I only want to return the fields 'description' and 'rule'. However I also want to return the owner() relationship from the Report model. How could I do this? Do I have to load the relationship and do some kind of array push, or is there a more elegant solution?
You can use with() to eager load related model
$reports = Report::with('owner')
->where('submission_id', $submissionID)
->where('status', 'pending')
->get(['id','description','rule']);
Note you need to include id in get() from report model to map (owner) related model
you will have probably one to many relationship with Reports and owners table like below
Report Model
public function owner() {
return $this->belongsTo('App\Owner');
}
Owner Model
public function reports() {
return $this->hasMany('App\Report');
}
your controller code
$reports = Report::with('owner')->
where('submission_id', $submissionID)->where('status', 'pending')->get()
return [
'message' => 'Success.',
'reports' => $reports,
];
This is what I ended up going with:
$reports = Report::
with(['owner' => function($q)
{
$q->select('username', 'id');
}])
->where('submission_id', $submissionID)
->where('status', 'pending')
->select('description', 'rule','created_by')
->get();
The other answers were right, I needed to load in the ID of the user. But I had to use a function for it to work.

How to queue the logics in controller

I have used two logic in my controller in my laravel project
public function multiStore()
{
$user = User::create([
'name'=>$request->name,
'email'=>$request->email,
'password'=>Hash::make($request->name),
]);
$post = MyPost::create([
'name'=>$request->post_name,
'body'=>$request->post_body,
]);
return redirect()->to('/admin/home);
}
Is it possible to make like if user is created successfully only then the post will be created so that I can use post created by user relationship
I have tried something like if condition but it is not working
You can try the code bellow , I assume you have a user_id field in posts table since you mentio9ned a relation ship. This is not the best way but i try to keep things simple, so I just edited the code.
Note : make sure you listed all the table fields in protected $fillable in your model before using Create()
public function multiStore()
{
$user = User::create([
'name'=>$request->name,
'email'=>$request->email,
'password'=>Hash::make($request->name),
]);
if($user){
$post = MyPost::create([
'user_id' => $user->id
'name'=>$request->post_name,
'body'=>$request->post_body,
]);
}
return redirect()->to('/admin/home);
}
Enclose your query in database transaction
https://laravel.com/docs/5.8/database#database-transactions
Either you can:
DB::transaction(function() {
Model::create();
AnotherModel::create();
});
Or you can use the following to find and catch error...
DB::beginTransaction();
// Your queries here...
// Model::create();
// AnotherModel::create();
DB::commit();

Create a factory that doesn't return anything - Laravel

Intro
Hello everyone,
Recently I've picked Laravel and I'm still learning about the framework (which by the way I find amazing).
I'm working on a project in where i have a model called Order which I use for grouping other Order models (for example ClassicOrder, InstantOrder etc...) by using a one-to-one morph relationship.
The Orders table store an id, an order_id and the order_type which is used for the morph relationship.
The Problem
I've made a factory for each Order type and now I want to create a factory that generates n orders by randomly picking between all the order types.
I've done it like this:
$factory->define(Order::class, function (Faker $faker) {
$className = collect(Order::getModels())->random();
$order = factory($className)->create();
return [
'order_id' => $order->id,
'type' => get_class($order)
];
});
Now, this is working but the problem is that each order use a trait called Order which already register the order in the orders table so when I call the factory I'll get two rows in the order table for the same order.
This is the order trait:
Trait Order {
public static function boot()
{
parent::boot();
self::created(function ($model) {
// Add the order to the orders table to give him a public id
DB::table('orders')->insert(['order_id' => $model->id, 'type' => self::class]);
// Set and create the order path if the order isn't instant
if (!is_a($model, 'App\InstantOrder')) {
$orderType = explode('\\', get_class($model))[1]; // App\OrderType -> OrderType
$folderName = $orderType . '_' . $model->publicId . '_' . time() . '/';
$model->path = public_path() . '/storage/orders/' . $folderName;
$model->save();
File::makeDirectory($model->path, 0777, true);
}
});
self::creating(function ($model) {
$model->{$model->getKeyName()} = Uuid::generate()->string;
});
}
}
I can avoid this by calling factory()->make() instead of factory->create() but this doesn't seem right to me.
The Question
I've thought about some solutions and I've come out with the followings:
- Don't make the factory return anything, but looks like I can't.
- Delete the inserted rows before returning the data to store in the Orders table, and even if not really great, it looks like the only solution.
Can I make a factory without returning anything?
Thanks and wish a great day to everyone.
-Riccardo
Well lemme first welcome you, and then ask who said it wasn't a a good idea to make a factory that return anything, as it's mentioned in Laravel docs that's how it's written:-
use Illuminate\Support\Str;
use Faker\Generator as Faker;
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => Str::random(10),
];
});
As mentioned Here
And then you can use it in Model factories as mentioned Here
I guess that's best practice as the Documentation says i guess.
And this is also a quick intro that you should check out for seeding data with Relationships

Query returning every row null in laravel

I'm trying to build a chat application using laravel echo and pusher, everything works but the data that returns to the databse is either null or the default value, here's the code
public function sendMessage(Request $request){
$conID = $request->conID;
$message1 = $request->message;
$user = Auth::user();
$fetch_userTo = DB::table('messages')
->where('conversation_id', $conID)
->where('user_to', '!=', Auth::user()->id)
->get();
$userTo = $fetch_userTo[0]->user_to;
$message = Message::create([
'user_from' => Auth::user()->id,
'user_to' => $userTo,
'conversation_id' => $conID,
'message' => $message1,
]);
if($message) {
$userMsg = DB::table('messages')
->join('users', 'users.id','messages.user_from')
->where('messages.conversation_id', $conID)->get();
broadcast(new MessagePosted($message))->toOthers();
return $userMsg;
}
}
NB: when i put insert() instead of create in the query the data goes through the database normally but there's an error in broadcasting
Have you tried to create a message like this? instead of using a model event?
$message = new Message;
$message->user_from = Auth::user()->id;
$message->$user_to = $userTo;
$message->conversation_id = $conID;
$message->message = $message1;
$message->save();
You have a lot more control this way, i.e
if($message->save()) { ... }
Or you could wrap the whole thing in a transaction?
Be sure your Message model allows the fields that you want to add in the $fillable array
Create method check fillable attributes into Laravel model. You have to write your all columns into fillable and then use create method.
Second solution is use Active Record technique. #Devin Greay answer is helpful to use Active record.
More information visit https://laravel.com/docs/5.6/eloquent#mass-assignment

Laravel: find posts from current user

I´ve cloned this sample app: https://github.com/codekerala/laravel-and-vue.js-spa-Recipe-Box
In RecipeController there's an index function that returns all posts (or recipes in this case). I need to get only recipes added by the current logged in user.
public function index()
{
$currentuser = Auth::id();
$recipes = Recipe::where('user_id', '=', $currentuser)
->get(['id', 'name', 'image']);
return response()
->json([
'recipes' => $recipes
]);
}
When trying this, my recipes array is empty, but no other errors from what I can see. I can hardcode value 1 instead of $currentuser and it returns all recipes that are made by user # 1.
I am declaring use Auth;, but am new to the Laravel framework, maybe someone could give any assistance?
Using Laravel 5.4.15
There might be any problem in Auth::user() please check it.
Add the following in your controller
use Illuminate\Support\Facades\Auth;
And add a constructor
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$currentuser = Auth::user();
dump($currentuser);
$recipes = Recipe::where('user_id', '=', $currentuser->id)
->get(['id', 'name', 'image']);
return response()
->json([
'recipes' => $recipes
]);
}
I think there is problem in your current user. You are not able to retrieve user id
Just use this statement.
$current_user = \Auth::user()->id;

Resources