I try to fill the receiver of an email from database. If I write the adress direct to $mail->to everything works fine but not with this code?:
$users = User::where('message_receiver','=',true)->get();
$data = array(
'url' => Config::get('app.url'),
'name' => 'test'
);
Mail::send('emails.message', $data, function($mail) use ($users)
{
$mail->from('test#localhost.de','test#localhost.de');
foreach ($users AS $user) {
$mail->to($user->email, $user->firstname.' '.$user->surname);
}
$mail->subject('New Message');
});
print_r($user->email) in the loop dumps the correct adress!
I never work with Laravel, but by this link http://bundles.laravel.com/bundle/Messages
As I understand, to send emails to multiple email addresses, you have to add all addresses into array first, then pass that array to $message->to() method, don't you?
And this is a sample from that link:
Message::send(function($message)
{
$message->to(array('someone#gmail.com', 'email#address.com' => 'name'));
$message->cc('more#addresses.com');
$messages->bcc(array('evenmore#address.com' => 'Another name', 'onelast#address.com'));
$message->from('me#gmail.com', 'Bob Marley');
$message->subject('Hello!');
$message->body('I really like spamming people!');
});
So, in my idea, you should try to make array from $users and then pass it to the method, something like this:
Mail::send('emails.message', $data, function($mail) use ($users)
{
$mail->from('test#localhost.de','test#localhost.de');
foreach ($users AS $user) {
$receivers = array_add($receivers, $user->email, $user->email);
}
$mail->to($receivers);
$mail->subject('New Message');
});
I'm not sure if I used array correctly, but I think you have alredy know how to do that.
Related
i get data (query) in command file and want to pass to controller via API (route)
here my request code in command file :
$request = Request::create('/create_data_account', 'post', ['data'=>$data]);
$create = $app->dispatch($request);
this is the route :
$router->post('/create_data_account', 'APIController#create_account_data_api');
and my controller :
public function create_account_data_api(Request $request)
{
$count = 0;
foreach ($data as $key => $value) {
$insert = Account::create([
'account_name' => $value->account_name,
'type' => $value->type,
'role_id' => $value->role_id
]);
if($insert){
$count++;
}else{
return $response = ['result'=>false, 'count'=>$count, 'message'=>'Internal error. Failed to save data.'];
}
}
return $response = ['result'=>true, 'count'=>$count, 'message'=>'Account data saved Successfully'];
}
i'm confused why passing data to controller not working with that code. anyone can give me solution ? Thanks
As you are making the request with ['data'=>$data]. That means all the data is contained on the key data of your array. So before the foreach loop, you need to add a declaration statement $data = $request->input('data'); to get data in $data variable.
Announce that new event have been posted
Please, I have this below if I use event() it works fine but if I add toOthers it gives me an error why?
Route::post('/messages', function(){
$user = Auth::user();
$message = $user->messages()->create([
'message' => request()->get('message')
]);
//Announce that new event have been posted
broadcast(new MessagePosted($message, $user))->toOthers;
return ['status' => 'OK'];
})->middleware('auth');
toOthers() is a function not a property
broadcast(new MessagePosted($message, $user))->toOthers();
From the docs
However, the broadcast function also exposes the toOthers method which allows you to exclude the current user from the broadcast's recipients:
broadcast(new ShippingStatusUpdated($update))->toOthers();
From the source in vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php Line 41
public function toOthers()
{
if (method_exists($this->event, 'dontBroadcastToCurrentUser')) {
$this->event->dontBroadcastToCurrentUser();
}
return $this;
}
Hope this helps
Added this toOthers();
broadcast(new MessagePosted($message, $user))->toOthers();
Route::post('/messages', function(){
$user = Auth::user();
$message = $user->messages()->create([
'message' => request()->get('message')
]);
broadcast(new MessagePosted($message, $user))->toOthers();
return ['status' => 'OK'];
})->middleware('auth');
I'm using composer require gloudemans/shoppingcart I am not sure how to maintain amount.
When i'm using one route that says add item when i'm using this route adding multiple item
How can i conditionally setup to add item in cart if this is unique
public function bookItem($id) {
$item = Item::where([
'status' => '1',
'id' => $id
])->first();
$product = Cart::add($item->id, $item->name, 1, $item->price); // This should not call always if it has not generated a row id then it should call
Cart::update($product->rowId, ['price' => 200]); // Will update the price if it is differ
return redirect()->route('book.item', ['id' => $item->id]);
}
I am not sure how to manage it. please guide
Looks like the package has a getContents() function that gathers all items into a collection. It also has a search(Closure) function that calls getContents() and then uses Laravel's filter function on the collection and returns the result.
$search = Cart::search(function($key,$value) use ($item) {
return $value === $item->id;
})->first();
if(!empty($search)){
Cart::update($search->rowId, ['price' => 200]);
}
else {
$product = Cart::add($item->id, $item->name, 1, $item->price);
}
Definitely check out the Laravel collection docs if you aren't familiar. This is the entey on filters:
https://laravel.com/docs/5.8/collections#method-filter
I have a model that has a one to many relationship to the versions of the description.
In my Controller
$tag = Tags::create([
'name' => $request->get('name'),
'user_id' => \Auth::id(),
]);
$tag->update([
'content' => $request->get('description')
]);
In my Model:
public function setContentAttribute(string $value)
{
$this->versions()->create([
'user_id' => \Auth::id(),
'value' => $value
]);
}
So I can't put content directly as an attribute in the create method because there is no Model right now.
But is it possible to overwrite the create Method?
When I try to overwrite something like this in my Model it will do an infinity loop
public static function create($attr) {
return parent::create($attr);
}
So my question is if it is possible to have something like this:
$tag = Tags::create([
'name' => $request->get('name'),
'user_id' => \Auth::id(),
'content' => $request->get('content')
]);
and in the Model:
public static function create($attr) {
$value = $attr['content'];
$attr['content'] = null;
$object = parent::create($attr);
$object->content = $value;
$object->save();
return $object;
}
Update
I didn't overwrite the create method but called it customCreate. So there is no infinity loop anymore and I can pass all variables to the customCreate function that handles the relationships for me.
Solution
After reading the changes from 5.3 to 5.4 it turns out that the create method was moved so you don't have to call parent::create() anymore.
The final solution is:
public static function create($attr) {
$content = $attr['content'];
unset($attr['content']);
$element = static::query()->create($attr);
$element->content = $content;
$element->save();
return $element;
}
I don't see why not and you could probably implement a more general approach? Eg. checking if set{property}Attribute() method exists, if it does - use it to assign a value, if it doesn't - use mass assigning.
Something like:
public static function create($attr) {
$indirect = collect($attr)->filter(function($value, $property) {
return method_exists(self::class, 'set' . camel_case($property) . 'Attribute');
});
$entity = parent::create(array_diff_key($attr, $indirect->toArray()));
$indirect->each(function($value, $property) use ($entity) {
$entity->{$property} = $value;
});
$entity->save();
return $entity;
}
I haven't really tested it but it should work. I use something like this in one of my Symfony apps.
I read similar batch_update Qs but I still have issues. Thanks in advance for help! I receive error message ("One or more rows submitted for batch updating is missing the specified index.") for the following code:
Controller:
public function do_edit_page(){
$id = $this->input->post('page_id', TRUE);
$link_title = $this->input->post('page_link_title', TRUE);
$link = $this->input->post('page_link_sub_title', TRUE);
$this->content_model->update_links($id, $link_title, $link);
$this->index();
}
Model:
public function update_links($id, $link_title, $link){
$data = array(
array(
'page_id' => $id,
'link_title' => $link_title,
'link' => $link
)
);
$this->db->update_batch('content_links', $data, $id);
}
Check the CI documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html
The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.
so the third parameter should be a column not data.