I have a simple car model with one attribute:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
/*
* TODO:
* Property Fotos ausfüllen
*/
class Car extends Model
{
//Tablenname
protected $table = 'cars';
protected $fillable = ['title'];
public $timestamps = false;
/**
* #var string
*/
protected $title = NULL;
/**
* #var integer
*/
protected $imagesId = NULL;
/**
*
* #return string
*/
public function getTitle()
{
return $this->attributes['title'];
}
/**
*
* #param string $title
*
* #return void
*/
public function setTitle($title)
{
$this->attributes['title'] = $title;
}
}
This is my store function from the controller:
<?php
namespace App\Http\Controllers;
use App\Car;
class CarController extends Controller
{
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$car = new Car();
// $car->setTitle($request->title);
$car->setTitle('stackoverflow');
$car->save();
...
}
}
However, this is how the entry in the database looks like:
Title is always zero! I also tried it with other models, same.
Check field datatype.if you want to store string you have to change your 'title' field datatype to varchar.
hope it works.
Related
I do not know how to solve this. I get an Error. I am trying to dynamically save the category in the database. But when I press create category I get an error. I am following a course that has a previous laravel version so I am not sure where the problem is.
Class 'App\Category' not found
CategoriesController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use App\Models\Category as ModelsCategory;
class CategoriesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('categories.index')->with('categories', Category::all());
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('categories.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|unique:categories'
]);
$ncategory = new Category();
Category::create([
'name' => $request->name
]);
return redirect(route('categories.index'));
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Category.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $fillable = ['name'];
}
you have two different import for Category
use App\Category;
use App\Models\Category as ModelsCategory;
remove The top one. and change the second one as follow:
use App\Models\Category;
use App\Models\Category;
It should work
I have an application in my local environment and in a production server. This application have a controller called ArticlesController with this code:
<?php
namespace App\Admin\Controllers;
use App\Core\Controllers\CoreController;
use App\Admin\Requests\ArticlesRequest;
use App\Admin\Interfaces\ArticlesRepositoryInterface;
class ArticlesController extends CoreController
{
/**
* #var ArticlesRepositoryInterface
*/
private $articleRepository;
public function __construct(ArticlesRepositoryInterface $articleRepository)
{
$this->articleRepository = $articleRepository;
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\View\View
*/
public function index()
{
$articles = $this->articleRepository->all();
return view('admin.articles.index')->with(compact('articles'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\View\View
*/
public function create()
{
return view('admin.articles.create');
}
/**
* Store a newly created resource in storage.
*
* #param \App\Admin\Requests\ArticlesRequest $request
* #return \Illuminate\Routing\Redirector
*/
public function store(ArticlesRequest $request)
{
$data = $request->validated();
$article = $this->articleRepository->create($data);
return redirect()->route('articles.edit', $article)->with('successMessage', 'Article created! Now you can edit the article with new information');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(int $id)
{
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$article = $this->articleRepository->find($id);
return view('admin.articles.edit')->with(compact('article'));
}
/**
* Update the specified resource in storage.
*
* #param \App\Admin\Requests\ArticlesRequest $request
* #param int $id
* #return \Illuminate\Routing\Redirector
*/
public function update(ArticlesRequest $request, int $id)
{
$data = $request->validated();
$this->articleRepository->update($data, $id);
return redirect()->route('articles.index')->with('successMessage', 'Article updated!');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$this->articleRepository->delete($id);
return redirect()->route('articles.index')->with('successMessage', 'Article deleted!');;
}
}
How you can see, this controller uses ArticlesRepositoryInterface. This is the code:
<?php
namespace App\Admin\Interfaces;
use App\Admin\Models\Article;
use Illuminate\Database\Eloquent\Collection;
interface ArticlesRepositoryInterface extends BaseRepositoryInterface
{
/**
* #return Collection
*/
public function all(): Collection;
/**
* #param array $data
* #return Article
*/
public function create(array $data): Article;
/**
* #param array $data
* #param int $id
* #return int
*/
public function update(array $data, int $id): int;
/**
* #param int $id
* #return int
*/
public function delete(int $id): int;
/**
* #param int $id
* #return Article
*/
public function find(int $id): ?Article;
}
Also, I have a provider that I use to instantiate the repositories with this code:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Admin\Interfaces\BaseRepositoryInterface;
use App\Admin\Interfaces\ArticlesRepositoryInterface;
use App\Admin\Interfaces\FilesRepositoryInterface;
use App\Admin\Repositories\BaseRepository;
use App\Admin\Repositories\ArticlesRepository;
use App\Admin\Repositories\FilesRepository;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(BaseRepositoryInterface::class, BaseRepository::class);
$this->app->bind(ArticlesRepositoryInterface::class, ArticlesRepository::class);
$this->app->bind(FilesRepositoryInterface::class, FilesRepository::class);
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
The code of the BaseRepository is this:
<?php
namespace App\Admin\Repositories;
use App\Admin\Interfaces\BaseRepositoryInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
class BaseRepository implements BaseRepositoryInterface
{
/**
* #var Model
*/
protected $model;
/**
* #param Model $model
*/
public function __construct(Model $model)
{
$this->model = $model;
}
/**
* #return Collection
*/
public function all(): Collection
{
return $this->model->all();
}
/**
* #param array $data
* #return Model
*/
public function create(array $data): Model
{
return $this->model->create($data);
}
/**
* #param array $data
* #param int $id
* #return int
*/
public function update(array $data, int $id): int
{
return $this->model->where('id', $id)->update($data);
}
/**
* #param int $id
* #return int
*/
public function delete(int $id): int
{
return $this->model->destroy($id);
}
/**
* #param int $id
* #return Model
*/
public function find($id): ?Model
{
return $this->model->find($id);
}
}
And finally, the code of the ArticlesRepository is this:
<?php
namespace App\Admin\Repositories;
use App\Admin\Interfaces\ArticlesRepositoryInterface;
use App\Admin\Models\Article;
use Illuminate\Database\Eloquent\Collection;
use App\Admin\Repositories\BaseRepository;
class ArticlesRepository extends BaseRepository implements ArticlesRepositoryInterface
{
/**
* #var Article
*/
protected $article;
/**
* #param Article $article
*/
public function __construct(Article $article)
{
$this->article = $article;
}
/**
* #return Collection
*/
public function all(): Collection
{
return $this->article->all();
}
/**
* #param array $data
* #return Article
*/
public function create(array $data): Article
{
return $this->article->create($data);
}
/**
* #param array $data
* #param int $id
* #return int
*/
public function update(array $data, int $id): int
{
return $this->article->where('id', $id)->update($data);
}
/**
* #param int $id
* #return int
*/
public function delete(int $id): int
{
return $this->article->destroy($id);
}
/**
* #param int $id
* #return Article
*/
public function find($id): ?Article
{
return $this->article->find($id);
}
}
It works perfectly in my local environment, but, is strange, because in the remote server, with exactly the same code, it throws an error:
Declaration of App\Admin\Repositories\ArticlesRepository::create(array $data): App\Admin\Models\Article must be compatible with App\Admin\Repositories\BaseRepository::create(array $data): Illuminate\Database\Eloquent\Model
Any ideas?
All function declarations should be exactly the same, including the return type declarations:
ArticlesRepositoryInterface:
public function create(array $data): Article;
BaseRepository:
public function create(array $data): Model
ArticlesRepository:
public function create(array $data): Article;
App\Admin\Models\Article and Illuminate\Database\Eloquent\Model cannot be used both.
Perhaps this doesn't throw an exception locally because of a different PHP version?
Note: you might want to consider to extend all the repositories from a single BaseRepository.
I am using a service within twig like this
{{ count_service.getCount(term.getId) }}
I want the service to use a repository function, repository function
<?php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping;
class SynonymRepository extends EntityRepository
{
public function getCount($termId)
{
$qbSynonymType = $this->getEntityManager()->createQueryBuilder();
$synonymTypes = $qbSynonymType->select('synonymType.id, synonymType.type')
->from('AppBundle:SynonymType', 'synonymType')
->getQuery()->getResult();
$qb = $this->getEntityManager()->createQueryBuilder();
$count = [];
$qb->select('count(synonym.synonymId)')
->from('AppBundle:Synonym','synonym');
foreach($synonymTypes as $type) {
$count[$type['type']] = $qb
->where('synonym.term = :termId')
->andWhere('synonym.synonymType = :type')
->setParameter('termId', $termId)
->setParameter('type', $type['id'])
->getQuery()->getSingleScalarResult();
}
$qbTerm = $this->getEntityManager()->createQueryBuilder()->from('AppBundle:Term', 'term');
$count['parent'] = "NaN";
$count['children'] = "NaN";
return $count;
}
}
My service.yml looks like this
synonymrepository:
class: Doctrine\ORM\EntityRepository
factory: ["#doctrine.orm.entity_manager", getRepository]
arguments:
- AppBundle\Entity\SynonymType
term_count:
class: AppBundle\Services\TermCount
arguments:
- "#synonymrepository"
And finally my service looks like this
<?php
namespace AppBundle\Services;
use AppBundle\Repository\SynonymRepository;
class TermCount
{
private $repository;
public function __construct()
{
$this->repository = new SynonymRepository();
}
public function getCount($termId)
{
return $this->repository->getCount($termId);
}
}
When running this I am getting the following error
Type error: Too few arguments to function Doctrine\ORM\EntityRepository::__construct(), 0 passed in /var/www/html/src/AppBundle/Services/TermCount.php on line 15 and exactly 2 expected
I assume this is happening because extending SynonymRepository with the EntityRepository requires EntityManagerInterface $em and Mapping\ClassMetadata $class. But I am not sure how pass them to EntityRepository.
I was using this answer to get me here, lost on how to actually implement the finall bit.
Thanks for helping.
UPDATE
Entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Table(name="synonym")
* #ORM\Entity(repositoryClass="AppBundle\Repository\SynonymRepository")
*/
class Synonym
{
/**
* #var int
* #ORM\Id()
* #ORM\Column(name="synonym_id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $synonymId;
/**
* #var Term
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Term", inversedBy="synonyms")
*/
protected $term;
/**
* #var SynonymType[]
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\SynonymType", inversedBy="synonyms")
*/
protected $synonymType;
/**
* #var int
* #ORM\Column(name="language_id", type="integer")
*/
protected $languageId;
/**
* #var string
* #ORM\Column(name="synonym", type="string", length=255)
*/
protected $synonym;
public function __construct()
{
// $this->synonymType = new ArrayCollection();
}
/**
* #return int
*/
public function getSynonymId(): int
{
return $this->synonymId;
}
/**
* #return Term
*/
public function getTerm(): Term
{
return $this->term;
}
/**
* #param int $termId
* #return Term
*/
public function setTerm(int $termId): Term
{
$this->term = $termId;
return $this->term;
}
/**
* #return SynonymType[]
*/
public function getSynonymType()
{
return $this->synonymType;
}
/**
* #param SynonymType $synonymType
* #return SynonymType
*/
public function setSynonymType(SynonymType $synonymType): SynonymType
{
$this->synonymType = $synonymType;
return $this->synonymType;
}
/**
* #return int
*/
public function getLanguageId(): int
{
return $this->languageId;
}
/**
* #param int $languageId
* #return Synonym
*/
public function setLanguageId(int $languageId): Synonym
{
$this->languageId = $languageId;
return $this;
}
/**
* #return string
*/
public function getSynonym(): string
{
return $this->synonym;
}
/**
* #param string $synonym
* #return Synonym
*/
public function setSynonym(string $synonym): Synonym
{
$this->synonym = $synonym;
return $this;
}
}
You need to use DI (Dependency injection) in your construct insted of using new cause as i see the erreur your SynonymRepository depends on other services
<?php
namespace AppBundle\Services;
use AppBundle\Repository\SynonymRepository;
class TermCount
{
private $repository;
public function __construct(SynonymRepository $synonymRepository)
{
$this->repository = $synonymRepository;
}
public function getCount($termId)
{
return $this->repository->getCount($termId);
}
}
I am getting the following error:
FatalErrorException in usercontroller.php line 21: Class 'APP\User' not found
usercontroller.php:
<?php
namespace App\Http\Controllers;
use APP\User;
use Illuminate\Http\Request;
use App\Http\Requests;
class usercontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::all();
return view('admin/users', compct('user'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
User.php (it's in App/User.php):
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
I create to users.blade.php to listing user in Admin Panel but at last then i m click to a user then they have an error on screen please say what can i do
Typo
Change use APP\User; to use App\User;
It's case sensitive so they are not the same thing.
please use User class in UserController with this namespace App/User NOT APP/user
Change use APP\User; to use App\User;
Change also compct to compact
public function index()
{
$user = User::all();
return view('admin/users', compact('user'));
}
I am using a github project named mnabialek/laravel-modular. The package works fine but I cant pass requests to a different controller in diffrent module. How can I do that.
TestModule Controller
<?php
namespace App\Modules\TestModule\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use App\Modules\TestModule\Http\Requests\TestModuleRequest;
use App\Modules\Admin\Http\Requests\AdminRequest;
use App\Modules\TestModule\Repositories\TestModuleRepository;
use App\Modules\TestModule\Services\TestModuleService;
class TestModuleController extends Controller
{
/**
* #var TestModuleRepository
*/
protected $repo;
/**
* #var TestModuleService
*/
protected $service;
/**
* TestModuleController constructor.
*
* #param TestModuleRepository $repo
* #param TestModuleService $service
*/
public function __construct(TestModuleRepository $repo, TestModuleService $service)
{
$this->repo = $repo;
$this->service = $service;
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
//echo "Here you are";
//$data["data"] = "Here you are";
//return view("welcome")->with($data);
$working = "Its Working";
$message = App::make("App\\Modules\\Admin\\Http\\Controllers\\AdminController")->create($working);
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
}
AdminController.php
<?php
namespace App\Modules\Admin\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use App\Modules\Admin\Http\Requests\AdminRequest;
use App\Modules\Admin\Repositories\AdminRepository;
use App\Modules\Admin\Services\AdminService;
class AdminController extends Controller
{
/**
* #var AdminRepository
*/
protected $repo;
/**
* #var AdminService
*/
protected $service;
/**
* AdminController constructor.
*
* #param AdminRepository $repo
* #param AdminService $service
*/
public function __construct(AdminRepository $repo, AdminService $service)
{
$this->repo = $repo;
$this->service = $service;
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create($working)
{
die(print_r($working));
//
}
}
Here is the error I am getting.
Class 'App\Modules\TestModule\Http\Controllers\App' not found
Try the following:
$message = Illuminate\Support\Facades\App::make("App\\Modules\\Admin\\Http\\Controllers\\AdminController")->create($working);
As App does not exist in the current namespace, you have to call it from where it is defined.