My problem at the moment is that I want to save some values to the database but the don't get saved and I don't get an error..
Both, either $product-save(); or $product-update(array(...)) are not working and I cannot tell why.. My ASIN Model looks fine and is filled with the right fillable attributes...
You guys know why it isn't working?
My Laravel Version: Laravel Framework 5.5.36
This is my class so far:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\ASIN;
class CheckPrice extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'post:CheckPrice';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
$product = ASIN::find(1410);
$product->price = "HELLO";
$product->amountSaved = "HELLO";
$product->percentageSaved = "HELLO";
$product->url = "HELLO";
$product->imgUrl = "HELLO";
$product->save();
//$product->update(array("price" => "HELLO", "amountSaved" => "HELLO", "percentageSaved" => "HELLO", "url" => "HELLO", "imgUrl" => "HELLO"));
$this->printProduct(ASIN::find(1410));
}
My ASIN Model so far:
namespace App;
use Illuminate\Database\Eloquent\Model;
class ASIN extends Model
{
protected $connection = 'facebook_amazon';
public $table = "ASINS";
protected $fillable = [
'ASIN',
'price',
'amountSaved',
'percentageSaved',
'category',
'url',
'imgUrl',
'showApp'
];
}
Kind regards and Thank You!
Use this in the handle methode
$product = App\ASIN::find(1410);
Or while impoting ASIN model use this if you want to keep the handle methode same
use App\ASIN as ASIN;
Use Laravel logs:
if(!$product->save()){
foreach($products->errors() as $error){
Log::info($error);
}
}
Hope this help.
Related
In my Controller I am using several functions. In this functions I am using similar code.
So I am wondering if there is a possibility outsource this code to not have to write it repeatedly. If this is possible, what would be the best way to do it?
class PagesController extends AbstractController
{
/**
* #Route("/documents/{slug}", name="documents", methods={"GET","POST"})
*/
public function documents($slug, Request $request)
{
$page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$entityManager = $this->getDoctrine()->getManager();
$cmf = $entityManager->getMetadataFactory();
$classes = $cmf->getMetadataFor($relation_name);
$fieldMappings = $classes->fieldMappings;
$associationMappings = $classes->associationMappings;
$fields = (object)array_merge((array)$fieldMappings, (array)$associationMappings);
}
/**
* #Route("/blog/{slug}", name="single", methods={"GET","POST"})
*/
public function blog($slug, Request $request)
{
$page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$entityManager = $this->getDoctrine()->getManager();
$cmf = $entityManager->getMetadataFactory();
$classes = $cmf->getMetadataFor($relation_name);
$fieldMappings = $classes->fieldMappings;
$associationMappings = $classes->associationMappings;
$fields = (object)array_merge((array)$fieldMappings, (array)$associationMappings);
}
/**
* #Route("/contact/{slug}", name="contact", methods={"POST", "GET"})
*/
public function contact($slug, Request $request)
{
$page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$entityManager = $this->getDoctrine()->getManager();
$cmf = $entityManager->getMetadataFactory();
$classes = $cmf->getMetadataFor($relation_name);
$fieldMappings = $classes->fieldMappings;
$associationMappings = $classes->associationMappings;
$fields = (object)array_merge((array)$fieldMappings, (array)$associationMappings);
}
}
You can use private method and call it, but in your case you could use Page typehint right in the parameter:
/**
* #Route("/contact/{slug}", name="contact", methods={"POST", "GET"})
*/
public function contact(Page $slug, Request $request)
The keyword here is services. Move your business logic to a other classes and auto-inject it in your controller using autowiring. This is a Symfony Best Practice:
Symfony follows the philosophy of "thin controllers and fat models".
This means that controllers should hold just the thin layer of
glue-code needed to coordinate the different parts of the application.
You should read about these best practices!
You can inject services in your controller class and in a specific action:
class PagesController extends AbstractController
{
public function __construct(Rot13Transformer $transformer)
{
$this->transformer = $transformer;
}
/**
* #Route("/documents/{slug}", name="documents", methods={"GET","POST"})
*/
public function documents($slug, Request $request, PagesRepository $repo)
{
$page = $repo->findOneBy(['slug'=>$slug]);
$foo = $repo->doSomethingDifferentWithEntities($page)
$bar = $this->transformer->transform($foo);
}
}
#Jarla Additionally to #Stephan Vierkant answer you can use #ParamConverter annotation
In your case, it will be:
/**
* #Route("/documents/{slug}", name="documents", methods={"GET","POST"})
* #ParamConverter("page", options={"mapping": {"slug": "slug"}})
*/
public function documents(Page $page, Request $request)
{
$foo = $repo->doSomethingDifferentWithEntities($page)
$bar = $this->transformer->transform($foo);
}
I'm trying to reference the Requests class in Laravel, I've tried so many fixes with the keyword "use" but each time I keep getting Reflection exception
that says app\path\specified doesn't exist. I'm confused.
Here is my code:`
<?php
namespace App\Http\Controllers;
//namespace App\Http\Request;
//use Illuminate\Http\Requests;
//use app\Http\Requests\ContactFormRequest;
use App\Message;
use App\Mail\SendMessage;
use Session;
//use App\Requests;
class AboutController extends Controller
{
public function create()
{
return view ('about.contact');
}
public function store(App\Requests\SendMessageRequest $request)
{
$message = $request->message;
Mail::to('myemail')
->send(new SendMessage($message, $request->email,$request->name));
THE REQUESTS CLASS
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SendMessageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
'name' => 'required',
'email' => 'required|email',
"message" => 'required',
];
}
}
The commented line(//) are what I've tried
SendMessageRequest is the name of my Request class.
Sorry, I canĀ“t comment your post. However can you also send the SendMessageRequest Class? Is that a subclass of the Request in Laravel?
I am getting the following error when trying to seed in Laravel 5.4
[Symfony\Component\Debug\Exception\FatalThrowableError]
Parse error: syntax error, unexpected '$faker' (T_VARIABLE), expecting function (T_FUNCTION) or c
onst (T_CONST)
Here is the code for the seed file.
<?php
use Illuminate\Database\Seeder;
use App\Book;
use Faker\Factory as Faker;
class BookSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
$faker = Faker::create();
public function run()
{
foreach (range(1, 30) as $index) {
Book::create([
'title'=> $faker->sentence(5),
'author'=> $faker->sentence(7),
'description'=>$faker->paragraph(4)
]);
}
}
}
I have created the model and done the migration. I can't seem to find any good tutorials on how to do this with Laravel 5.4 . Any help would be appreciated.
Declare $faker variable in run method will solve the problem
`<?php
use Illuminate\Database\Seeder;
use App\Book; use Faker\Factory as Faker;
class BookSeeder extends Seeder
{
/**
* Run the database seeds. *
* #return void */
public function run() {
$faker = Faker::create();
foreach (range(1, 30) as $index) {
Book::create([
'title'=> $faker->sentence(5),
'author'=> $faker->sentence(7),
'description'=>$faker->paragraph(4)
]);
}
}
}`
I need to override the default Profile model. I have managed to add the fields i need but there is something i am missing since. On insert and update these fields are not getting update to the database.
I have created the necessary migrations so i have these fields in the database already
What am i missing> see below my app/models/Profile.php
<?php
namespace app\models;
/**
* Description Profile
*
* This form #overrides dektrium\user\models\Profile
*/
use dektrium\user\models\Profile as BaseProfile;
use yii\web\UploadedFile;
use Yii;
use dektrium\user\models\User;
class Profile extends BaseProfile {
/**
* public variables to be added to the model
*/
public $profile_pic;
public $expertise_id;
public $country_id;
public function rules() {
$rules = parent::rules();
$rules['profile_pic'] = ['profile_pic', 'file'];
$rules['expertise_id'] = ['expertise_id', 'integer'];
$rules['country_id'] = ['country_id', 'integer'];
return $rules;
}
/**
* #inheritdoc
*/
public function attributeLabels() {
$labels = parent::attributeLabels();
$labels['profile_pic'] = \Yii::t('user', 'Profile Picture');
$labels['bio'] = \Yii::t('user', 'Biography');
$labels['expertise_id'] = \Yii::t('user', 'Expertise');
$labels['country_id'] = \Yii::t('user', 'Country');
return $labels;
}
}
First thing, remove this lines:
public $profile_pic;
public $expertise_id;
public $country_id;
If you already added those fields in the table, you dont need to declare them. As you can see, none of the others properties are being declared either. This is already being done by extending the model from ActiveRecord and declaring the tableName
I am new in Zend Framework
This is my DBTable
class Application_Model_DbTable_Employee extends Zend_Db_Table_Abstract
{
protected $_name = 'tab_employee';
}
This is my Models
public function InsertEmployees($array){
$tblEmployee = new Application_Model_DbTable_Employee();
$tblEmployee->insert($array);
}
This is my controller
public function AddEmployeeAction(){
$request = $this->getRequest();
$params = $request->getParams();
$emp = new Application_Model_InsEmployee();
$emp->InsertEmployees(array(
'Name' => $params['name'],
'Date' => $params['date']
));
}
Anybody knows what is the error of this code because it always return an application error . Thanks for advance
In your DbTable do this:
public function InsertEmployees($array){
$this->insert($array);
}
So your DB will look like this:
class Application_Model_DbTable_Employee extends Zend_Db_Table_Abstract
{
protected $_name = 'tab_employee';
public function InsertEmployees($array){
$this->insert($array);
}
}
Then from the controller, create the DbTable instance and "bypass" the model.
$model = new Application_Model_Db_Table();
$model->InsertEmployees($data)