Laravel 5 sending mail to multiple users at once - laravel

I am trying to sending mails to all users. But i can't figure out how to make this. In my controller, i made this.
public function send_mail()
{
$mails = Joinus::all();
$array = array();
$allmails = array();
foreach ($mails as $mail)
{
$allmails = array_push($array, $mail->email);
};
Mail::to($allmails)->send((new SendMail(new Joinus('email')))->delay(30));
}
I am getting all types of error. Last one is
__construct() must be of the type array
In my SendMail.php
public function __construct($email)
{
$this->email = $email;
}
I wasted my one day and can't make. I am very grateful for your help. Thanks an advance.

public function send_mail()
{
$mails = Joinus::pluck('email')->toArray();
foreach ($mails as $mail)
{
Mail::to($mail)->send((new SendMail(new Joinus($mail)))->delay(30));
};
}

$allmails = array_push($array, $mail->email); is wrong
Right answer is just array_push($array, $mail->email);
array_push($array, $mail->email); this is returning an array.
$allmails = array_push($array, $mail->email);
But this is returning int value.

You can try this.
public function send_mail()
{
$mails = Joinus::all();
$array = array();
$allmails = array();
foreach ($mails as $mail)
{
$allmails = array_push($array, $mail->email);
};
Mail::to($allmails)->send(new SendMail(new Joinus('email')))->delay(30);
}
Thanks,

Related

A non well formed numeric value encountered in laravel 8

Everything is working fine but after installation of auth package in laravel 8, and after logging into
that website "A non well formed numeric value encountered" error occured in every view. I can't find where is the problem. I also updated from command "composer update" but still error is there.
This is The error image
I need it's answer to reslove this issue.
Below is the Controller Code.
<?php
namespace App\Http\Controllers;
use App\Models\Cart;
use App\Models\Category;
use App\Models\Image;
use App\Models\Product;
use App\Models\state;
use Illuminate\Support\Facades\Auth;
class FrontController extends Controller
{
public function homePage()
{
$popular = Product::inRandomOrder()->limit(8)->get();
$latest = Product::inRandomOrder()->limit(4)->get();
$img = Image::get();
$imgs = Image::orderBy('id','desc')->get();
$categories = Category::where('parent_id',0)->get();
$pimage = [];
foreach ($img as $image) {
if (!isset($pimage[$image->product_id]))
$pimage[$image->product_id] = $image->image;
}
$pimages = [];
foreach ($imgs as $images) {
if (!isset($pimages[$images->product_id]))
$pimages[$images->product_id] = $images->image;
}
$cartCount = 0;
if(Auth::check()){
$cartItems = Cart::join('products','carts.product_id','=','products.id')->where('user_id',Auth::id())->get();
$cartCount = Cart::where('user_id',Auth::user()->id)->count();
return view('pages.index', compact('popular','latest','pimage','pimages','img','cartItems','cartCount', 'categories'));
}
else
return view('pages.index', compact('popular','latest','pimage','pimages','img','cartCount', 'categories'));
}
public function ProductDetail($slug)
{
$product = Product::where('slug',$slug)->first();
$images = Image::where('product_id',$product->id)->get();
$categories = Category::where('parent_id',0)->get();
$cartCount = 0;
if(Auth::check()){
$cartItems = Cart::join('products','carts.product_id','=','products.id')->where('user_id',Auth::id())->get();
$cartCount = Cart::where('user_id',Auth::user()->id)->count();
return view('pages.product_detail', compact('product','images','cartCount','cartItems','categories'));
}
else
return view('pages.product_detail', compact('product','images','cartCount','categories'));
}
public function CartDetail()
{
$img = Image::get();
$categories = Category::where('parent_id',0)->get();
$pimage = [];
foreach ($img as $image) {
if (!isset($pimage[$image->product_id]))
$pimage[$image->product_id] = $image->image;
}
$cartItems = Cart::join('products','carts.product_id','=','products.id')->where('user_id',Auth::id())->get();
$cartCount = Cart::where('user_id',Auth::user()->id)->count();
return view('pages.cart', compact('cartCount','cartItems','pimage','categories'));
}
public function Checkout()
{
$img = Image::get();
$categories = Category::where('parent_id',0)->get();
$pimage = [];
foreach ($img as $image) {
if (!isset($pimage[$image->product_id]))
$pimage[$image->product_id] = $image->image;
}
$state = state::where('country_id',101)->get();
$cartItems = Cart::join('products','carts.product_id','=','products.id')->where('user_id',Auth::id())->get();
$cartCount = Cart::where('user_id',Auth::user()->id)->count();
return view('pages.checkout', compact('cartCount','cartItems','pimage','state','categories'));
}
}
I don't know how without this code on every line of Cart::join, "select('carts.*','product_name')->" the error occured but when i inserted this select statment on specific columns then issue is gets resolved.

Too few arguments to function App\Awe\JsonUtility::addNewProduct(),

i am trying to create a CRUD app and am having trouble, if anyone can point me in the right direction i would be grateful, thank you.
Hi there i am having difficulty using data from the json.
i have used it here and it as worked
class JsonUtility
{
public static function makeProductArray(string $file) {
$string = file_get_contents($file);
$productsJson = json_decode($string, true);
$products = [];
foreach ($productsJson as $product) {
switch($product['type']) {
case "cd":
$cdproduct = new CdProduct($product['id'],$product['title'], $product['firstname'],
$product['mainname'],$product['price'], $product['playlength']);
$products[] = $cdproduct;
break;
case "book":
$bookproduct = new BookProduct($product['id'],$product['title'], $product['firstname'],
$product['mainname'],$product['price'], $product['numpages']);
$products[]=$bookproduct;
break;
}
}
return $products;
}
this is my controller
public function index()
{
// create a list.
$products = JsonUtility::makeProductArray('products.json');
return view('products', ['products'=>$products]);
}
this is my route
Route::get('/product' , [ProductController::class, 'index'] );
how can i use this on my controller and what route should i set up to create a product
public static function addNewProduct(string $file, string $producttype, string $title, string $fname, string $sname, float $price, int $pages)
{
$string = file_get_contents($file);
$productsJson = json_decode($string, true);
$ids = [];
foreach ($productsJson as $product) {
$ids[] = $product['id'];
}
rsort($ids);
$newId = $ids[0] + 1;
$products = [];
foreach ($productsJson as $product) {
$products[] = $product;
}
$newProduct = [];
$newProduct['id'] = $newId;
$newProduct['type'] = $producttype;
$newProduct['title'] = $title;
$newProduct['firstname'] = $fname;
$newProduct['mainname'] = $sname;
$newProduct['price'] = $price;
if($producttype=='cd') $newProduct['playlength'] = $pages;
if($producttype=='book') $newProduct['numpages'] = $pages;
$products[] = $newProduct;
$json = json_encode($products);
if(file_put_contents($file, $json))
return true;
else
return false;
}
This is where i am trying to type to code into.
public function create()
{
//show a view to create a new resource
$products = JsonUtility::addNewProduct('products.json');
return view('products', ['products'=>$newProduct], );
}
your function addNewProduct() is expecting 7 parameters when called.
you are getting this error because you cannot provide those parameters that your function is looking for.
in your code above you are passing 'products.json' which is in a string format.
lets assume that it is a JSON data. it will still fail because you are only passing 1 parameter to a function that is expecting 7 parameters.
what you could probably do is change it to
public static function addNewProduct($data)
{
// code here
}
then you can pass your JSON data and then go through each of your json using a loop.

How to check if an item in an array has been removed and delete it?

I have a question that has options like multiple choice options for answering, those options are what is inside the array, if someone removes an option while editing and saves the question, I'm not sure how to check for that, my code checks for editing an existing option and checks for adding a new option.
This is my code for the update
public function update(Request $request, $id)
{
DB::beginTransaction();
$preg = SurveyQuestion::findOrFail($id);
$preg->question = $request->question;
$preg->survey_section_id = $request->survey_section_id;
$preg->response_type_id = $request->response_type_id;
$preg->optional = $request->optional;
$preg->save();
if ($request->get('questionOptions')) {
foreach ($request->get('questionOptions') as $item) {
$opts = [];
if (empty($item['id'])) {
$option = new SurveyQuestionOption();
$option->survey_question_id = $preg->id;
$opts[] = $item['id'];
} else if (!empty($item['id'])) {
$option = SurveyQuestionOption::findOrFail($item['id']);
$opts[] = $item['id'];
}
$option->option = $item['option'];
$option->save();
}
}
DB::commit();
return back();
}
Currently I'm not checking for if its being removed, because like I said I'm not sure how, my idea was creating an array and storing the ids of the ones being created or edited and somehow using that to compare to the array of options being sent to the controller from the vue. Not sure how viable this is, any help is appreciated.
This is the solution I arrived at
public function update(Request $request, $id)
{
DB::beginTransaction();
$preg = SurveyQuestion::findOrFail($id);
$preg->question = $request->question;
$preg->survey_section_id = $request->survey_section_id;
$preg->response_type_id = $request->response_type_id;
$preg->optional = $request->optional;
$preg->save();
$ids = [];
if ($request->get('questionOptions')) {
foreach ($request->get('questionOptions') as $item) {
if (empty($item['id'])) {
$option = new SurveyQuestionOption();
$option->survey_question_id = $preg->id;
} else if (!empty($item['id'])) {
$option = SurveyQuestionOption::findOrFail($item['id']);
}
$option->option = $item['option'];
$option->save();
$ids[] = $option['id'];
}
}
if (count($ids) > 0) {
SurveyQuestionOption::whereNotIn('id', $ids)->where('survey_question_id', $preg->id)->delete();
}
DB::commit();
return back();
}

Too few arguments to function App\Http\Controllers\homepageController::edit_web_services(), 0 passed and exactly 1 expected

my function
public function post_sofware_technology(Request $request)
{
$titile = $request->input('category_title');
$excerpt = $request->input('Excerpt');
$post = new posts();
$post->post_title = $titile;
$post->post_excerpt = $excerpt;
$post->save();
$soft_id = $post->id;
$this->edit_web_services($soft_id);
}
Another Function
public function edit_web_services($soft_id)
{
$soft = $soft_id;
dd($soft);
}
I want to pass my $soft_id = $post->id; value from post_sofware_technology() method to edit_web_services($soft_id) within the same controller.
try with this
public function post_sofware_technology(Request $request)
{
$titile = $request->input('category_title');
$excerpt = $request->input('Excerpt');
$post = new posts();
$post->post_title = $titile;
$post->post_excerpt = $excerpt;
$post->save();
$soft_id = $post->id;
return $this->edit_web_services($soft_id);
}

Codeigniter-passing value from controller to model

My English is not good so sorry for any mistake. I'm new to Codeigniter, I am trying to search record by using this code, this is my controller code:
public function search() {
if(isset($_POST['search']))
{
$s = $_POST['search'];
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name =' ,"{$s}" );
$query = $this->db->get();
$res = $query->result();
$data['user'] = null ;
if($res)
{
$data['user'] = $res ;
}
$this->load->view('filter' , $data);
}
}
It is working fine but I want to write this code in separate Model.
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name =' ,"{$s}" );
$query = $this->db->get();
$res = $query->result();
But i don't know how to pass this variable value $s = $_POST['search']; to my model. Any suggestion?
you can try the following
//in your application/models Folder put a File called UserSearch_Model.php
class UserSearch_Model extends CI_Model
{
public function search($strSearch)
{
$query = $this->db
->select('u_id,name,email,phone')
->from('user')
->where('name' ,$s)
->get();
return $query->result();
}
}
//and in your controller
public function search()
{
$strSearch = $this->input->post('search');
if (!empty($strSearch))
{
$data = [];
$this->load->model("UserSearch_Model");
$data['user'] = $this->UserSearch_Model->search($strSearch);
if (is_array($data['user']) && count($data['user']) > 0)
{
$this->load->view('filter' , $data);
}
}
}
You can try this solution for your problem :
public function search() {
$search_value = $this->input->post('search'));
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
if (isset($search_value) && !empty($search_value)) {
$this->db->where('name',$search_value);
// OR
$this->db->like('name', $search_value);
}
$query = $this->db->get();
$res = $query->result();
$data['user'] = null;
if ($res) {
$data['user'] = $res;
}
$this->load->view('filter', $data);
}
I hope it will help.
Controller
public function search() {
$search = $this->input->post('search');
$this->load->model('your_model'); //<-- You can load the model into "__construct"
$search_result = $this->your_model->your_search_function($search); //<-- This is how you can pass a variable to the model from controller
$this->load->view('your_view',['search_result'=>$search_result]); //<-- This is how you can load the data to the view. Hear your search result array is loaded on your view
}
Model
public function your_search_function($search) {
//You will receive this search variable ^^ here in your model
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name',$search);
$query = $this->db->get();
$result = $query->result_array();
if(isset($result) && !empty($result))
{
return $result;
} else
{
return FALSE;
}
}

Resources