I am trying to update a model in Laravel and I have missed the following error
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime
value: '2020-03-08 02:46:51' for column
deportetotal.news.updated_at at row 1 (SQL: update news set
views = 1, news.updated_at = 2020-03-08 02:46:51 where id =
86)
My class model look like that
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Jenssegers\Date\Date;
class News extends Model
{
protected $guarded=[];
public function getImage(){
return $this->hasOne('App\Image', 'id', 'image' );
}
public function tags(){
return $this->belongsToMany('App\Tag', 'news_tags', 'news_id', 'tags_id');
}
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
public function author()
{
return $this->belongsTo('App\User', 'user_id');
}
public function comments(){
return $this->hasMany('App\Comment', 'news_id');
}
public function getImagePathAttribute(){
return Storage::disk('public')->url($this->image);
}
public function getPublishDateAttribute(){
return new Date($this->pubDate);
}
}
And the fucntion controller
public function show($slug){
$news=News::where('slug',$slug)->first();
$news->views++;
$news->save();
return view('web.news.show', compact('news'));
}
Related
I have this problem, I try to connect movies and casts through morpToMany, but I get a strange error, I don't understand what I'm doing wrong.
Movie
public function casts()
{
return $this->morphToMany(Cast::class, 'movie_casts');
}
Cast
public function movies()
{
return $this->morphToMany(Movie::class, 'movie_casts');
}
MovieCast
public function movies()
{
return $this->morphedByMany(Movie::class, 'movie_casts');
}
public function casts()
{
return $this->morphedByMany(Cast::class, 'movie_casts');
}
movie_casts table
$table->unsignedBigInteger('cast_id')->nullable();
$table->foreign('cast_id')->references('id')->on('casts');
$table->unsignedBigInteger('movie_id')->nullable();
$table->foreign('movie_id')->references('id')->on('movies');
Controller
public function moviesList()
{
$movies = Movie::all();
foreach($movies as $movie){
$moviesWithCast = $movie->casts;
};
return response()->json(
$moviesWithCast,
200
);
}
Getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'movie_casts.movie_casts_id' in 'field list'
You are not using a morph relationship, it's a simple M2M relationship :
Movie
public function casts()
{
return $this->belongsToMany(Cast::class, 'movie_casts');
}
Cast
public function movies()
{
return $this->belongsToMany(Movie::class, 'movie_casts');
}
why would you take the morph approach ?
I would like to know what is the difference between edit($id) and edit(Model $model), because I tested both and this function see the code below
public function editModel(Todolist $modelTodo)
{
return view('todos.edit')->with('modelTodo', $modelTodo);
}
# Oubien
public function edit($id)
{
$todo = Todolist::find($id);
return view("todos.edit")->with('todo', $todo);
}
I need how to show data in another table like MySQL join or something like that
MySQL example
My Code
Model usuarios
class Usuario extends Model {
protected $table = 'usuarios';
protected $primaryKey = 'idusuarios';
protected $filliable = [
'cedula', 'nombre', 'tele1', 'tele2', 'correo', 'direccion',
'user_name', 'user_pass', 'fecha_ingreso', 'usu_idrol'
];
public function Usuario() {
return $this->hasOne('app\Roles','idrole','usu_idrol','desc_rol');
}
const CREATED_AT = NULL;
const UPDATED_AT = NULL;
}
Model Roles
class Roles extends Model {
protected $table ='roles';
protected $primarykey = 'idrole';
protected $filliable = ['desc_rol'];
public function Roles() {
return $this->belongsTo('app\Usuario', 'usu_idrol', 'idrole');
}
}
Controller usuarios
public function index(Request $request) {
if (!$request->ajax()) return redirect('/');
$usuarios = Usuario::all();
return $usuarios;
}
View usuarios
that's what I need
Try this in the controller that is returning data to your vue instance
//get all the users from the database (in your controller)
//you need to create a new array so as to easily map the role in the returned results
return Usuario::with('Usuario')->get()->map(function($role) {
return [
'field1' => $role->field1,
'field2' => $role->field2,
'field3' => $role->field3,
'field4' => $role->field4,
'field5' => $role->field5,
'rol' => $role->Usuario->desc_role
];
});
//Client Model
protected $fillable = [
'client_name', 'plan_id', 'client_type'
];
public function plan(){
return $this->belongsTo(Plan::class, 'plan_id', 'id');
}
// Plan Model
protected $fillable = [
'price'
];
public function clients()
{
return $this->hasMany(Client::class);
}
I want to get total price of all clients where client_type = something
I would do it like this:
function getClientTypeTotal($clientType)
{
return Client::where('client_type', $clientType)->get()->sum(function ($client) {
return $client->plan->price;
})
}
I create a core class named MY_Model that extends CI_Model. In this class, I create a method chaining to get all record with pagination like this :
// Take record with paging.
public function get_all_paged()
{
// get argument that passed
$args = func_get_args();
// get_all_paged($offset)
if (count($args) < 2) {
$this->get_real_offset($args[0]);
$this->db->limit($this->_per_page, $this->_offset);
}
// get_all_paged(array('status' => '1'), $offset)
else {
$this->get_real_offset($args[1]);
$this->db->where($args[0])->limit($this->_per_page, $this->_offset);
}
// return all record
return $this->db->get($this->_tabel)->result();
}
So , I just used like this on my controller,
for example
public function index($offset = NULL) {
$karyawan = $this->karyawan->get_all_paged($offset); //get all
}
I am realy confuse to get all record using join, I know join in CI like this :
public function get_all_karyawan() {
$this->db->select('tb_1 , tb_2');
$this->db->from('tb_1');
$this->db->join('tb_2', "where");
$query = $this->db->get();
return $query->result();
}
How to make it into chain in MY_Model?
Any help it so appreciated ...
The good thing in query builder, you can chain your db methods, till get(). So you can define, selects, where queries, limits in different ways.
For example:
public function category($category)
{
$this->db->where('category_id', $category);
return $this;
}
public function get_posts()
{
return $this->db->get('posts')->result();
}
And you can get all posts:
$this->model->get_posts();
Or by category:
$this->model->category(2)->get_posts();
So upon this, in your model:
public function get_all_karyawan() {
$this->db->select('tb_1 , tb_2');
$this->db->join('tb_1', "where");
// Here you make able to chain the method with this
return $this;
}
In your controller:
public function index($offset = NULL) {
$karyawan = $this->karyawan->get_all_karyawan()->get_all_paged($offset);
}