Questions about Using laravel with several models / MySQL tables - php

I'm new with laravel + frameworks and i'm having a few issues.I read the laravel documentation and tried asking for support on their forums - no results/
In my current setup - I followed the laravel documentation & code
I have 1 Model : User
I have 1 table in the db : users
Everything works fine for the login / registration parts etc....
However now i need to create several new tables & maybe some more models :
I searched the web and there are no tutorials or anything else to guide me how to do it.
Any help would be greatly appreciated .
Tried :
In Models -
created : profile.php
class Profile extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'profile';
}
It doesn't seem to work , I'm wondering if i have to link the file or something in the laravel framework etc..
P.s : I used artisan and migrations to create the user table
I am now creating the tables directly in the database but i have no idea how to access them such as :
in the user table , i can use :
Auth::user()->username - to get the username
however in the new table (profile) -
I have absolutely no idea how to access it using laravel code.

Have you read over the documentation http://laravel.com/docs/eloquent?
Create a new file inside of ../app/models/
../app/models/Profile.php
** note the file matches the classname
inside the model:
class Profile extends Eloquent {
protected $table = 'profile';
}
Then in your controller or route closure access the model via
$profile = Profile::find(1);
or
$profile = App::make('Profile');
$profile->about = 'About the user';
$profile->save();

Related

Specific question Laravel Database Homepage

I started using Laravel few days before.
I'm actually struggling with a problem, I created a homepage and I want to replace some text of the page with content from my database.
So how do I create a model/controller, and after that I will make an admin panel, so I can edit them.
The only tutorials/docs I see are for making a form/post to create users
Example
In basic php it's easy you just do a pdo connection and then a fetch and you use your date as you want. How do you do it in laravel ?
To fetch data from the DB in Laravel can be done in 1 of two ways, 1. using a Model (the best way) or using a Query Builder, which is much more familiar to those migrating from pure PHP.
Using a Model
Create a model using php artisan make:Model (change Model with a name of your choosing) then open the model once created (found in app/Http/Models) and add the following under use HasFactory;:
protected $table = 'your_table_name';
protected $primaryKey = 'id'; // This is the column you usually set to PRIMARY
public $timestamps = true;
protected $fillable = [
'table_column',
'table_column',
];
To use the Model, import it into your Controller file like so use App\Models\Model; and then use it as so:
$flights = Flight::where('destination', 'Paris')->get();
Learn more about Models in Laravel here.
Using a Query Builder, not best practice
Import the DB facade in the controller like so use DB; then call upon it like so:
$db = DB::table('users')->where('name', 'John')->first();
Learn more about the Query Builder here.
I hope this helps, if not let me know how I can assist further.

Laravel : Search with posted parameters

I am new to Laravel so please be patient with me.
i have to implement search functionality in Laravel. i am able to send form data to the search controller and receiving it as well
public function search_results(Request $request){
if ($request->has('gender')) {
echo $request->gender;
}
.....
}
Now when i am following tutorials on the web i am getting such examples :
public function filter(Request $request, User $user)
{
// Search for a user based on their name.
if ($request->has('name')) {
$user->where('name', $request->input('name'))->get();
}
}
or
public function index()
{
$users = User::where('status', 1)
->where('is_banned', 0)
->get(['name']);
dd($users);
}
now i am not sure from where this User:: or User $user is being added, and what should i do in my scenario.
Your suggestions would be helpful ..
Thanks
The $user represents an object belonging to the model(User::) associated with a table(users) in the mvc structure. If you want to "Search for a user based on their name." That means you already have a users table in database , and a User model .
You can use eloquent queries to retrieve data from database.
A User.php file should be generated when you create a new laravel project with composer. You can check for that.
So when you declare a variable like this :
$user = User::first();
You are actually getting the first element in 'users' table by using your User model.
If you can configure your User model and users table correctly , you should be able to get all records from your users table like this :
$users = User::all();
Then you can filter it like :
$users = $users->where('gender','male') // where('gender',$request->gender) in your situation
If you dont have a users table or a model , you can look to the document about how to create models using artisan commands Laravel API Doc. Generating Model Classes

Eloquent save() not working with PHP & MySQL

I hope you're having a great week.
I've been meddling with PHP and Silex. MySQL is the db. I am trying to create a post route & controller. I am using Eloquent as an ORM tool here, but it is not working and worse there is no log or a trace about any error. This is quite strange. I know that MySQL assumes that the created_at and updated_at fields exist in the tables. Although they do exist, I cannot get it working. I am open for anything here. I'd appreciate any help in fixing this.
Below you can find the code snippets...
This is the Model class
<?php
namespace DemoApp\Models;
class Apple extends \Illuminate\Database\Eloquent\Model
{
}
And this is the post route in index.php
$app->post('/apple', function(Request $request) use($app) {
$_apple = $request->get('apple');
$apple = new Apple();
$apple->body = $_apple;
$apple->user_id = 1;
$apple->save();
return new Response('Apple created.', 200);
});
PS: created_at and updated_at fields are present in the model table.
Peace...

Laravel routing & database MySQL different output

I am studying Laravel Framework for few hours and i am trying to do what's being done in the tutorial i am watching. I am executing a query through routes.php and it's giving me a different output.
My database has only 1 table and it is named 'customer' and i have a model named 'customer' and a controller named 'CustomerController'
My routes.php code is this
Route::get('customer', function() {
$customer = FirstLaravelApplication\Customer::find(1);
echo '<pre>';
print_r($customer);
But the localhost is giving me an error and it says i don't have any 'customers' table, it automatically added a letter 's' in the end of the table instead of 'customer' only. i really don't have any 'customers' table i don't know why it is passing the wrong name of the table but my code only says 'customer'.
i would appreciate any help! Thanks all!
Laravel/Eloquent ORM uses this convention, as do many ORMs. They pluralize table names.
Open up the Customer.php model and add in:
class Customer extends Model {
// Add this
protected $table = 'customer';
However, it's usually easier to stick with the framework's conventions.
https://laravel.com/docs/5.3/eloquent#eloquent-model-conventions

Kohana 3.2 reuse model to insert data

I'm currently working on a project that uses Kohana 3.2.
I haven't used the framework so I'm kind of a "beginner" though I've been using Symfony2 for quite some time.
Let's say I have a "task". This "task" can be assigned to multiple "users" at the same time.
This is achieved by entering the same "task" with different user_id. So I thought it shouldn't be a problem to reuse the model by simply to change the column's data and then calling $task->create().
For some reason this isn't working right and the code returns:
Kohana_Exception [ 0 ]: Cannot create task model because it is already loaded.
My current code is:
Model:
class Model_Task extends ORM{
protected $_table_name = 'task';
protected $_primary_key = 'task_id';
}
Code:
$task = ORM::factory('task');
$task->task = "some random task you need to finish.";
foreach($users as $user){
$task->user_id=$user;
$task->create();
}
Am I doing something wrong or you simply can't reuse models in Kohana?
You are using the same task object for all users, thus after first iteration task will be loaded after calling create method. Change your code to:
foreach($users as $user)
{
$task = ORM::factory('task');
$task->user_id = $user;
$task->create();
}

Categories